[英]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.