Home> Java> javaTutorial> body text

Understanding @MappedSuperclass in JPA

PHPz
Release: 2024-08-25 18:00:36
Original
968 people have browsed it

Entendendo @MappedSuperclass em JPA

JPA (Java Persistence API) provides various annotations to map Java classes to database tables. One such useful annotation is @MappedSuperclass, which is used to designate a class whose properties must be inherited by other entity classes, but which is not an entity itself. Let's explore the usefulness of this annotation through a practical example involving classes such as Vehicle, Car and Motorcycle.

What is @MappedSuperclass?

The @MappedSuperclass annotation is used to indicate that a class should not be an independent entity, but that its attributes should be inherited by other classes that are entities. This is useful when you want to share common attributes between multiple entities without creating a separate table for the base class.

Main Features:

  1. - The class annotated with @MappedSuperclass is not an entity.
  2. - It is not possible to execute queries directly on the @MappedSuperclass class.
  3. - Subclasses that extend the @MappedSuperclass class are mapped to individual tables in the database, but inherit fields from the base class.

Practical Example

Let's create an example with a class hierarchy for Vehicle, Car and Motorcycle, where Vehicle is the superclass.

1. Base Class:Vehicle

import javax.persistence.MappedSuperclass; @MappedSuperclass public abstract class Veiculo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String marca; private String modelo; private int ano; // Getters e Setters }
Copy after login
e
  • The Vehicle class is annotated with @MappedSuperclass.
  • The Vehicle class has the id field annotated with @id and @GeneratedValue. This unique identifier will be inherited by all subclasses, ensuring that each entity derived from Vehicle has an id field.
  • It defines three common attributes: brand, model and year.

2. Subclass:Car

import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "carro") public class Carro extends Veiculo { private int quantidadePortas; // Getters e Setters }
Copy after login
e
  • The Car class inherits the attributes of the Vehicle class.
  • It is annotated with @Entity and mapped to a table called car.

3. Subclass:Motorcycle

import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "motocicleta") public class Motocicleta extends Veiculo { private boolean temSidecar; // Getters e Setters }
Copy after login
e
  • The Motorcycle class also inherits the attributes of the Vehicle class.
  • It is annotated with @Entity and mapped to a table called motorcycle.

Table Mapping

With the above classes, JPA will create the following tables in the database:

  1. Car Table: Contains columns for make, model, year and quantity of doors.
  2. Tablemotorcycle: Contains columns for make, model, year and temSidecar.

The Vehicle table does not exist in the database, as the Vehicle class is just a superclass and not an entity.

Benefits of using@MappedSuperclass

  • Identifier Centralization: The id field is managed in the superclass. All entities derived fromVehicleshare the same identification scheme.
  • Code Reuse: Common attributes can be centralized in a superclass, avoiding duplication in subclasses.
  • Ease of Maintenance: Changes to common attributes can be made in one place.
  • Cohesive Data Model: Subclasses share the same structure, which makes the model easier to manipulate and understand.

Considerations

If you want the superclass to also be an entity (e.g. for direct queries), use the @Inheritance inheritance strategy instead of @MappedSuperclass.
@MappedSuperclass is ideal for situations where the base class does not need to be persisted as an individual entity, but its properties are relevant to multiple entities.

Conclusion

The @MappedSuperclass annotation is a powerful tool for creating reusable class hierarchies in JPA. In the example above, we managed to centralize the common attributes in Vehicle and, at the same time, maintain the flexibility and independence of the Car and Motorcycle entities. This approach promotes a cleaner, more modular design, especially in systems with multiple entities that share similar characteristics.

The above is the detailed content of Understanding @MappedSuperclass in JPA. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!