Note: The entity mentioned in the article refers to the class annotated by @ entity.
In JPA, object relationship ing usually corresponds to an object corresponding to a table, and there is no relationship between the two entities. If the two entities are inherited, how can we map them?
Objects in JPA support inheritance ing, multi-State Association, and multi-state query. Abstract classes and specific classes can both be entities, and can be annotated using @ entity, mapped to entities, and the query is encapsulated into an entity. Entity classes can inherit non-entity classes, and non-entity classes can also inherit entity classes.
The inheritance ing of JPA has the following situations:
I. Entity classes inherit abstract (specific) entity classes
An abstract class can be specified as an object. The only difference between an abstract object and a specific object is that the abstract object cannot be directly instantiated. Abstract Entities can be mapped into one entity and can be used as query targets.
The abstract object class uses the @ entity annotation or represents an object in the XML descriptor.
This type of ing is relatively complex. I will write an article to illustrate it later. I will not talk about it here.
Ii. ed superclasses)
An object can be inherited from a superclass. This superclass provides persistence entity State (I .e., attribute or field) and ing information, but it is not an entity. In general, this type of super-class ing aims to define the State and ing information shared by multiple objects.
The ing superclass are different from entities. They cannot be queried. Therefore, they cannot be passed as parameters to entitymanager or Query Interfaces for operations. The persistence relationship of the ing superclass must be one-way.
An abstract class or a specific class can be used as a ing superclass. You can use the @ mappedsuperclass annotation (or mapped-superclass XML descriptor element) to specify the ing superclass.
Ing superclasses do not generate separate tables. Their ing information acts on the entity classes inherited from them.
The ing superclass can be mapped like an object class, but its ing will act on the object class inherited from it, because it does not have a separate table. When used as a subclass, the inherited ing information applies to the corresponding table of the subclass. Subclass can overwrite the ing information of ing superclass through @ attributeoverride and associationoverride annotations or corresponding XML descriptor elements. Here is an example:
Package COM. mikan; import Java. io. serializable; import javax. persistence. column; import javax. persistence. generatedvalue; import javax. persistence. generationtype; import javax. persistence. ID; import javax. persistence. mappedsuperclass; @ mappedsuperclasspublic class employee implements serializable {Private Static final long serialversionuid =-7671099980281525370l; @ ID @ generatedvalue (Strategy = generationtype. identity) protected integer empid; @ columnprotected string name; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. table; @ entity @ table (name = "ft_emp") public class fulltimeemployee extends employee {Private Static final long serialversionuid = 9115429216382631425l; // inherit the empid and name attributes mapped to the superclass @ columnprivate double salary; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. table; @ entity @ table (name = "pt_emp") public class parttimeemployee extends employee {Private Static final long serialversionuid =-61223473745158301_l; // inherit the empid and name attributes of the ing superclass @ column (name = "hourly_wage") Private float hourlywage; // getter/setter method}
In this example, "employee" is a ing superclass. It includes two fields (that is, the preceding state) and corresponding ing information, but these ing information are inherited by the subclass entities fulltimeemployee and parttimeemployee, it does not generate the corresponding table. Only ft_emp and pt_emp tables are generated. The fields in these two tables include the fields of the subclass and the fields inherited from the ing superclass. The fields in the ft_emp table are empid, name, and salary, while those in the pt_emp table are empid, name, and hourly_wage.
Iii. entity class inheritance non-entity class
An object class can inherit a non-entity superclass. This non-entity superclass can be a specific class or an abstract class.
This non-entity superclass is only used as an inheritance action, and its state is not persistent. All States inherited from non-entity superclass (that is, attributes or fields) are not persisted in the object subclass, and the entity manager does not manage these statuses. All Annotations on non-entity superclasses are ignored. Non-entity superclass cannot be passed as parameters to entitymanager or Query Interfaces for operations.
Here is an example:
public class Cart {protected Integer operationCount; // transient statepublic Cart() {operationCount = 0;}public Integer getOperationCount() {return operationCount;}public void incrementOperationCount() {operationCount++;}}@Entitypublic class ShoppingCart extends Cart {Collection<Item> items = new Vector<Item>();public ShoppingCart() {super();}@OneToManypublic Collection<Item> getItems() {return items;}public void addItem(Item item) {items.add(item);incrementOperationCount();}}
The table corresponding to shoppingcart does not contain the operationcount field.
JPA Object Inheritance ing