01-hibernate annotations: Class-level annotations, @Entity, @Table, @Embeddable

Source: Internet
Author: User

@Entity

@Entity: Mapping entity classes

@Entity (name= "TableName")

Name: optional, corresponds to a table in the database and can be omitted if the table name is the same as the entity class name.

Note: You must specify the primary key attribute of the entity class when using @entity.

First step: Create the Entity class:

Add @Entity Annotations to the class name, respectively. Add a @Id annotation to the Get method that is the property of the primary key in the class.

 Packagecom.entity;Importjava.util.Date;Importjavax.persistence.Entity;/*JPA PRIMARY Key*/Importjavax.persistence.Id;/*Student entity class*/@Entity (name= "Students") Public classStudents {Private intSID; PrivateString sname;//name    PrivateString gender;//Sex    PrivateDate birthday; PrivateString Major;//Professional    PrivateString address;//Address         PublicStudents () {} PublicStudents (intsid, String sname, String gender, Date birthday, String major, string address) {        //super ();         This. Sid =SID;  This. sname =sname;  This. Gender =gender;  This. Birthday =birthday;  This. Major =Major;  This. Address =address; } @Id  Public intGetSID () {returnSID; }     Public voidSetsid (intSID) {         This. Sid =SID; }     PublicString Getsname () {returnsname; }     Public voidsetsname (String sname) { This. sname =sname; }     PublicString Getgender () {returngender; }     Public voidSetgender (String gender) { This. Gender =gender; }     PublicDate Getbirthday () {returnbirthday; }     Public voidsetbirthday (Date birthday) { This. Birthday =birthday; }     PublicString getmajor () {returnMajor; }     Public voidSetmajor (String major) { This. Major =Major; }     PublicString getaddress () {returnaddress; }     Public voidsetaddress (String address) { This. Address =address; }    }

The second step: Configure this class in the configuration.

The third step: Write the Test class:

 Packagecom.entity;ImportJava.util.EnumSet;ImportOrg.hibernate.boot.Metadata;Importorg.hibernate.boot.MetadataSources;ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder;ImportOrg.hibernate.service.ServiceRegistry;ImportOrg.hibernate.tool.hbm2ddl.SchemaExport;ImportOrg.hibernate.tool.schema.TargetType;Importorg.junit.Test; Public classteststudents { @Test  Public voidTestschemaexport () {//Create a Service registration objectServiceregistry Serviceregistry =Newstandardserviceregistrybuilder (). Configure (). build (); //Create a metadata objectMetadata Metadata =Newmetadatasources (serviceregistry). Buildmetadata (); //Create a Schemaexport objectSchemaexport export =NewSchemaexport ();    Export.create (Enumset.of (targettype.database), metadata); }}

After debugging, the tables are normally generated in the database.

@Table

@Table (name= "", catalog= "", Schema= "")

@Entity used in conjunction, can only be labeled at the class definition of the entity class, representing the information of the database table corresponding to the entity.

Name: optional, map table name, default table name and entity name are the same, only need to specify table name in case of inconsistency.

Catalog: Optional, the catalog name, the default is: Catalog ("").

Schema (Schema): Optional, representing the schema name, default: Schema ("").

Table Annotation instance: changes to the previous entity are as follows:

Name: Before the table name, Schema: default to the database name used

@Embeddable

Indicates that a non-entity class can be embedded in another entity class as an attribute.

is equivalent to the component property that replaces the previous single-pen operation .

The first step is to build a non-entity class and add @embeddable annotations before it.

 Packagecom.entity;Importjavax.persistence.Embeddable; @Embeddable /*represents an embedded class that acts as a property in another entity class*/ Public classAddress {PrivateString postcode;//Postal Code    PrivateString address;//Address    PrivateString Phone;  PublicAddress () {} PublicString Getpostcode () {returnpostcode; }     Public voidSetpostcode (String postcode) { This. Postcode =postcode; }     PublicString getaddress () {returnaddress; }     Public voidsetaddress (String address) { This. Address =address; }     PublicString Getphone () {returnphone; }     Public voidSetphone (String phone) { This. Phone =phone; }    }

Step Two: Add the object of this class to the entity class and add the Set,get method.

Step Three: Test:

Discovery builds successfully as required.

01-hibernate annotations: Class-level annotations, @Entity, @Table, @Embeddable

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.