Now you can define
Author,
Category and
Invoice entities in a more succinct way. To see an example you have the new code for
Category here:
package com.yourcompany.invoicing.model;
import jakarta.persistence.*;
import lombok.*;
@Entity @Getter @Setter
public class Category extends Identifiable {
@Column(length=50)
String description;
}
The refactoring is plain vanilla.
Category now extends from
Identifiable and we have removed the
oid property. This way, not only is your code shorter, but it's more elegant, because you declare that your class is identifiable (the what, not the how), and you removed from your business logic class code that is somewhat technical.
Apply this same refactoring to the
Author and
Invoice entities. Moreover, from now on you'll extend most of your entities from the
Identifiable mapped superclass.
We have created our own
Identifiable class to see the advantage of using mapped superclasses, however OpenXava provides for you a ready to use
Identifiable class that you can find in the
org.openxava.model package. So in your next project you don't need to write the
Identifiable class again, just use the one included in OpenXava.
Summary
You have learned that a mapped superclass is a regular class with JPA mapping annotations that you can use as a base class for your entities. Moreover, you have learned how to use mapped superclasses to simplify your code.