package com.bjsxt.hibernate;
import javax.persistence.Embedded;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinColumns;import javax.persistence.ManyToOne;import javax.persistence.OneToOne;import javax.persistence.PrimaryKeyJoinColumn;import javax.persistence.Table;
@Entity@Table(name="t_user")public class User { @Id @GeneratedValue private int id; private String name; @ManyToOne @JoinColumn(name ="groupId") //多加了一列 表示 某个user 属于那个group 多对一 多的一方 加字段 private Group group; public int getId() { return id; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
``````````````````````````````````````````````````````````````````````
package com.bjsxt.hibernate;
import javax.persistence.Embedded;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinColumns;import javax.persistence.OneToOne;import javax.persistence.PrimaryKeyJoinColumn;import javax.persistence.Table;
@Entity@Table(name="t_group")public class Group { @Id @GeneratedValue private int id; private String name; 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; }}
转载于:https://www.cnblogs.com/lize1215/p/7641508.html
相关资源:(4)Hibernate中的many-to-one和one-to-many关系