Java for Web Learning Notes (109): Again on the inheritance __java of entity Mapping (2) Entity

Source: Internet
Author: User
Tags inheritance

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.