"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Understanding @MappedSuperclass in JPA

Understanding @MappedSuperclass in JPA

Published on 2024-09-02
Browse:893

Entendendo @MappedSuperclass em JPA

JPA (Java Persistence API) provides several annotations for mapping 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

}
  • 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

}
  • 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
}
  • 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. Table car: Contains columns for make, model, year and quantityDoors.
  2. Table motorcycle: 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 from Vehicle share 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: The 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.

Release Statement This article is reproduced at: https://dev.to/gregoriohd/entendendo-mappedsuperclass-em-jpa-4310?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3