Let's start with a simple case where multiple known table has the same schema and can be mapped to the same entity. Then, in the next study, we'll discuss a complex situation where we might dynamically build tables that have the same schema, if you use JPA.
Assuming that the Test_a,test_b and Test_c tables have the same table structure, that is, the schema is the same, we cannot map it to the same entity because each entity class is mapped to a table. If we don't want to write duplicate code, we can use the inheritance method.
The following approach applies equally to the schemas of these tables, and the differences are reflected by extends. @Inheritance: Table per class
@Entity
@Inheritance (strategy=inheritancetype.table_per_class) public
abstract CLASS abstestentity {
@ ID
private Long ID;
private String name;
.....
}
If there is a test table, we don't need abstract. @Inheritance that this entity schema can be inherited.
@Entity
@Table (name= "test_a") public
Lass Testa extends abstestentity {
}
@Entity
@Table ( Name= "Test_b") public
Lass Testb extends abstestentity {
private String data;
......
}
But there is a problem with this approach if:
@Entity
@Inheritance (strategy=inheritancetype.table_per_class) public
abstract CLASS abstestentity {
@ ID
@GeneratedValue (strategy=generationtype.identity) //will cause an exception
private Long ID;
private String name;
.....
}
This can cause an exception:
caused By:org.hibernate.MappingException:Cannot use identity column key generation with <union-subclass> mapping F Or:cn.wei.flowingflying.chapter22.site.entity.AbsTestEntity at
Org.hibernate.persister.entity.unionsubclassentitypersister.<init> (unionsubclassentitypersister.java:82) At Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown Source) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown Source) at Java.lang.reflect.Constructor.newInstance (Unknown Source) at Org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister (persisterfactoryimpl.java:96) at Org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister (persisterfactoryimpl.java:77) at Org.hibernate.metamodel.internal.MetamodelImpl.initialize (metamodelimpl.java:128) at Org.hibernate.internal.sessionfactoryimpl.<init> (sessionfactoryimpl.java:301) at Org.hibernate.boOt.internal.SessionFactoryBuilderImpl.build (sessionfactorybuilderimpl.java:452) at Org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build (entitymanagerfactorybuilderimpl.java:889 )
... More
The identity builder is not supported in this way. Need special attention. @MappedSuperclass
Our table is passed with Auto_increment, which leads to the inability to use the above way, which we can use @mappedsuperclass.
@MappedSuperclass public
abstract class Abstestentity {
private Long ID;
private String name;
@Id
@GeneratedValue (strategy=generationtype.identity) public
Long GetId () {return
Id;
}
.....
}
RELATED links: My professional Java for WEB applications related articles