Hibernate (7) component ing and multi-to-one ing, hibernate multiple-to-one
I. Component ing
Configure component ing with annotations:
Husband Is A ing Class. wife is a part of this class (the property cannot be the same as the property in husband. Do not write the Entity annotation or have a primary key)
Husband class: (add annotation @ Embedded on the getWife () method)
package cn.orlion.hibernate.model;import javax.persistence.Embedded;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Husband { private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Embedded public Wife getWife() { return wife; } public void setWife(Wife wife) { this.wife = wife; }}
Wife class:
package cn.orlion.hibernate.model;public class Wife { private String wifename; public String getWifename() { return wifename; } public void setWifename(String wifename) { this.wifename = wifename; }}
Table creation statement:
create table Husband ( id integer not null auto_increment, name varchar(255), wifename varchar(255), primary key (id) )
2. Multi-to-one ing
1. Multi-to-one unidirectional ing
Annotation mode configuration: (A group has multiple users, each user corresponds to a group, and the user is multiple parties, and the group is one)
Group class:
Package cn. orlion. hibernate. model; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. id; import javax. persistence. table;/** a group has multiple users. Each user corresponds to a group, with multiple users and one group */@ Entity @ Table (name = "t_group ") public class Group {private int id; private String name; @ Id @ GeneratedValue public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name ;}}
User class:
package cn.orlion.hibernate.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_user")public class User { private int id; private String name; private Group group; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToOne public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; }}
SQL statement generated by hibernate:
create table t_group ( id integer not null auto_increment, name varchar(255), primary key (id) )create table t_user ( id integer not null auto_increment, name varchar(255), group_id integer, primary key (id) )alter table t_user add constraint FK_e5f24mh6aryt9hsy99oydps6g foreign key (group_id) references t_group (id)