[英]Hibernate- OneToMany Relationship
@Entity
@Table(name= "employee1100")
public class Employee {
@Id
@Column(name="eid")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="ename")
private String name;
@OneToMany(mappedBy="employee",cascade=CascadeType.ALL)
private Set<Laptop> laptop;
@Entity
@Table(name="laptop1100")
public class Laptop {
@Id
@Column(name="laptop")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="lapdetails")
private String details;
@ManyToOne
@JoinColumn(name="empId")
private Employee employee;
Laptop lap1=new Laptop();
Laptop lap2=new Laptop();
lap1.setDetails("Lenovo");
lap2.setDetails("HP");
Employee e1=new Employee();
lap1.setEmployee(e1);
lap2.setEmployee(e1);
Set<Laptop> laptop=new HashSet<Laptop>();
laptop.add(lap1);
laptop.add(lap2);
e1.setName("Rahul");
e1.setLaptop(laptop);
session.save(e1);
我只能看到此处插入了一个与员工记录相关的笔记本电脑对象,有什么问题吗?
通常,当您使用 Set 接口时,您应该覆盖 hashCode() 和 equals() 方法。 您是否尝试过 List 而不是 Set 接口?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.