The use of @MappedSuperclass

Source: Internet
Author: User

Entity class Baseentity.java

 Packagecom.rock.cft.hibernate;Importjava.util.Date;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;ImportJavax.persistence.MappedSuperclass;//@MappedSuperclass used on top of the parent class. This callout is added when the class is definitely a parent class. If you change to @entity, after inheritance, multiple classes inherit, only one table is generated, not multiple inheritance, and multiple tables are generated@MappedSuperclass Public Abstract classbaseentity {PrivateInteger ID;//Database primary Key PrivateDate CreationTime;//creation Time PrivateDate Modificationtime;//Modification Time@Id @GeneratedValue (Strategy=Generationtype.auto) PublicInteger getId () {returnID;}  Public voidsetId (Integer id) { This. ID =ID;}  PublicDate GetCreationTime () {returnCreationTime;}  Public voidsetcreationtime (Date creationtime) { This. CreationTime =CreationTime;}  PublicDate Getmodificationtime () {returnModificationtime;}  Public voidsetmodificationtime (Date modificationtime) { This. Modificationtime =Modificationtime;} }
Entity class Test_no1.java
 PackageCom.rock.cft.test.model;Importjava.io.Serializable;Importjava.util.Date;ImportJavax.persistence.Column;Importjavax.persistence.Entity;Importjavax.persistence.Table;Importcom.rock.cft.hibernate.BaseEntity; @Entity @table (name= "Test_no2") Public classTest_no1extendsBaseEntityImplementsSerializable {PrivateString name;Private intAge ;  PublicString GetName () {returnname;}  Public voidsetName (String name) { This. Name =name;}  Public intGetage () {returnAge ;}  Public voidSetage (intAge ) {   This. Age =Age ;} }
Entity class Test_no2.java
 PackageCom.rock.cft.test.model;Importjava.io.Serializable;Importjava.util.Date;ImportJavax.persistence.Column;Importjavax.persistence.Entity;Importjavax.persistence.Table;Importcom.rock.cft.hibernate.BaseEntity; @Entity @table (name= "Test_no2") Public classTest_no2extendsBaseEntityImplementsSerializable {PrivateDate Testbri; PrivateString Testadr;  PublicDate Gettestbri () {returnTestbri;}  Public voidSettestbri (Date testbri) { This. Testbri =Testbri;}  PublicString Gettestadr () {returnTestadr;}  Public voidSettestadr (String testadr) { This. Testadr =Testadr;} } 
This results in the generation of tables only: Test_no1, Test_no2 two tables, and both tables contain ID, CreationTime, modificationtime three properties
But if you change the @mappedsuperclass to @entity, you're going to create a baseentity table.

The use of @MappedSuperclass

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.