Hibernate annotation Mapping Relationship detailed

Source: Internet
Author: User
Tags database issues

There are two ways to work with relational mappings in Hibernate. One is to use the traditional XML to deal with their mapping, and the other is based on the annotation annotation method. I personally like to use annotation, I think this way seems more concise, greatly reduce the workload. The annotation package is not required to be introduced in Hibernate4 because the annotation package is already integrated.

Before we go into hibernate annotation, let's think about it. There are several key points that are necessary when creating a table in a database. Table name, field name, field type, constraint. From these points, let's take a look at how the entity class of a Hibernate entity model uses annotation to represent several of the key points we want.

@Entity @Table (name="Tbl_user")//--->table the name of the table in the specified database Public classUser {PrivateUUID UserID; PrivateString UserName; PrivateString name; PrivateString PassWord; PrivateDate Worktime; Privaterole role;  Public enumRole {Waiter, shopkeeper, cook} @Id @GenericGenerator (name="Generator", strategy ="UUID")//---> Indicates that the primary key is automatically generated and the policy is to generate the GUID@GeneratedValue (generator ="Generator") @Column (name="UserID", length= -)//--->column the name of the table column property in the specified database     PublicUUID GetUserID () {returnUserID; }     Public voidSetuserid (UUID userID) { This. UserID =UserID; } @Column (Name="UserName", length= -, nullable=false)     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; } @Column (Name="name", length=Ten, nullable=false)      PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; } @Column (Name="PassWord", length= -, nullable=false)     PublicString GetPassword () {returnPassWord; }     Public voidSetPassword (String passWord) { This. PassWord =PassWord; } @Column (Name="Worktime", nullable=false)     PublicDate Getworktime () {returnWorktime; }     Public voidsetworktime (Date worktime) { This. Worktime =Worktime; } @Enumerated (enumtype.string)//---> Use @enumerated annotations. This annotation allows you to select an enumeration type while converting the enumeration type to a string insert database@Column (name="role", columndefinition="varchar (default ' waiter ')")//--->columndefinition to set constraints on the properties of a field     PublicRole Getrole () {returnrole; }     Public voidsetrole (role role) { This. Role =role; }}

We tend to feel a lot of confusion when building maps, because you have to think about database issues while thinking about how the entity class should handle it. In fact, you can think of, some annotation is used to generate the database of these annotation are not many, modeling should first write them up. And then to deal with their mapping relationships.

Common database annotation are: (stand in the database angle)

@Table--processing table

@Column--processing fields

@JoinColumn--handling foreign keys

Here is the relationship between food and food category (FoodType) (from the perspective of the entity Class)

Obviously a food can only correspond to one category (regardless of special circumstances), so if we want to use a food to get a foodtype data. We can add a foodtype to the class.

A foodtype can have multiple food, then we can add a set<food> to the class.

@Entity @table (name= "Tbl_foodtype") Public classFoodType {PrivateUUID Foodtypeid; PrivateString name; @Id @GenericGenerator (Name= "Generator", strategy = "UUID") @GeneratedValue (generator= "Generator") @Column (name= "Foodtypeid", length=20)       PublicUUID Getfoodtypeid () {returnFoodtypeid; }     Public voidSetfoodtypeid (UUID foodtypeid) { This. Foodtypeid =Foodtypeid; } @Column (Name= "Name", length=20,nullable=false)       PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    PrivateSet<food>foodlist; @OneToMany (Mappedby= "FoodType")//establish a one-to-many relationship, FoodType is a property in the food class     PublicSet<food>getfoodlist () {returnfoodlist; }     Public voidSetfoodlist (set<food>foodlist) {         This. foodlist =foodlist; }    }

@Entity @table (name= "Tbl_food") Public classFood {PrivateUUID Foodid; PrivateString URL; PrivateDouble Price; PrivateString name; PrivateDeleted isDeleted; @Id @GenericGenerator (Name= "Generator", strategy = "UUID") @GeneratedValue (generator= "Generator") @Column (name= "Foodid", length = 20)     PublicUUID Getfoodid () {returnFoodid; }     Public voidSetfoodid (UUID foodid) { This. Foodid =Foodid; } @Column (Name= "url", length = 100)     PublicString GetUrl () {returnURL; }     Public voidseturl (String url) { This. url =URL; } @Column (Name= "Price", columndefinition = "float default 0")     PublicDouble GetPrice () {returnPrice ; }     Public voidSetprice (Double price) { This. Price =Price ; } @Column (Name= "Name", nullable =false)     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Enumerated (enumtype.string) @Column (name= "IsDeleted", columndefinition = "varchar (default ' NO ')", nullable =false)     PublicDeleted getisdeleted () {returnisDeleted; }     Public voidsetisdeleted (Deleted isDeleted) { This. isDeleted =isDeleted; }    PrivateFoodType FoodType; @ManyToOne @JoinColumn (Name= "Foodtypeid")//---> Specify the name of the database foreign key     PublicFoodType Getfoodtype () {returnFoodType; }     Public voidSetfoodtype (FoodType foodtype) { This. FoodType =FoodType; }    }

Observant you will find that the above is a one-to-many, many-to-one situation. In the hibernate mapping relationship, there are many-to-many, one-on, and so on.

One-to-many, many-to-one nature is exactly the same, nothing more than to change the set<object> of a party into an Object.

As for many-to-many, they are often modeled as two pairs of one-to-many, many-to-a models. So you just have to learn the above, relationship mapping you learned 80%.

Now that the mapping is almost complete, there is a problem with the loading of the data ()

Hibernate annotation Mapping Relationship detailed

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.