@Entity public class User { private int id; // @Column(length=10) // 注解在此处则表中字段为name private String name; public User() {} @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(length=10) // 注解在此处则表中字段为name1 public String getName1() { return name; } public void setName1(String name) { this.name = name; } }
@Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column(length=10,nullable=false) private String name; @OneToOne(optional=false,cascade=CascadeType.ALL) @JoinColumn(name="pid_id") private PID id; public Person() { this.name = "lzz"; } public Person(String name) { this.name = name; } // getter and setter }
@Entity public class PID { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String pid; @OneToOne(mappedBy="pid",cascade={CascadeType.PERSIST ,CascadeType.MERGE,CascadeType.REFRESH} ,optional=false)//指定为关系的被维护端,这里的optional可以去掉,因为外键不允许为空 private Person person; public PID(String pid) { this.pid = pid; } public PID() { this.pid = "xxx"; } // getter and setter }
@Entity public class Order_ { /** * 双向的一对多关系里面,多的一方为关系的维护端。 关系的维护端负责外键字段(记录)的更新; 关系的被维护端是没有权力更新外键字段(记录)的。 */ @Id @GeneratedValue private int id; @Column(nullable = false) private float amount; /** * 延迟加载必须保证em是开着的 如果是ToMany则默认延迟加载 如果是ToOne则默认是立即加载 * 通过mappedBy声明关系的维护端,这里是指明在OrderItem类里面的哪个属性来维护关系 * 在哪个类出现mappedBy则表明该实体是关系被维护端。 */ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "order") private List<Item> items = new ArrayList<Item>(); // 建立关系维护 public void addOrderItem(Item item) { item.setOrder(this);// 关系维护端orderItem来设置,维护关系 this.items.add(item); amount += item.getPrice(); } // getter and setter }
@Entity public class Item { /** * 双向的一对多关系里面,多的一方为关系的维护端。 关系的维护端负责外键字段(记录)的更新; 关系的被维护端是没有权力更新外键字段(记录)的。 */ @Id @GeneratedValue private int id; @Column(nullable = false) private String name; @Column(nullable = false) private float price; @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false) @JoinColumn(name="order_id") private Order_ order; // getter and setter }
@Entity public class Teacher { @Id @GeneratedValue private Integer id; @Column(length = 30, nullable = false) private String name; // 关系被维护端 @ManyToMany(mappedBy = "teachers", cascade = CascadeType.REFRESH, fetch = FetchType.LAZY) private Set<Student> student = new HashSet<Student>(); // getter and setter }
@Entity public class Student { @Id @GeneratedValue private Integer id; @Column(length=30,nullable=false) private String name; //一般在多对多很少用到级联的操作。 /** * inverseJoinColumns关系被维护端的外键在中间表中的定义 * JoinColumns 关系维护端的外键在中间表中的定义 */ @ManyToMany(cascade=CascadeType.REFRESH) @JoinTable(name="student_teacher" ,inverseJoinColumns=@JoinColumn(name="teacherid") ,joinColumns=@JoinColumn(name="studentid")) private Set<Teacher> teachers=new HashSet<Teacher>(); // getter and setter }
@Entity public class Couple { @EmbeddedId private CouplePK couple; private String address; // getter and setter }
@Embeddable public class CouplePK implements Serializable { private Person husband; private Person wife; public CouplePK() {} //getter and setter }构建好实体后,通过Persistence.createEntityManagerFactory("test")加载hibernate即可创建对应的表。