我有三个实体 employee 、 info 和 address ,我想通过使用 hibernate 和 jpa 注释使用 onetoone 映射映射具有 info 和 address 实体的 employee 实体。
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int empId;
private String name;
private String email;
@OneToOne(mappedBy = "emp", cascade = CascadeType.ALL)
private Info info;
@OneToOne(mappedBy = "emp", cascade = CascadeType.ALL)
private Address address;
//getter and setter
@Entity
public class Info {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int infoId;
private String gender;
private String phone;
@OneToOne
@JoinColumn(name = "emp_id")
private Employee emp;
//getter and setter
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addrId;
private String street;
private String city;
private String country;
@OneToOne
@JoinColumn(name = "emp_id")
private Employee emp;
//getter and setter
public class OneToOneApp {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("o2omapping.cfg.xml");
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee =new Employee();
employee.setName("Iqra");
employee.setEmail("[email protected]");
Address address = new Address();
address.setStreet("Manda Road");
address.setCity("Allahabad");
address.setCountry("India");
Info info = new Info();
info.setGender("FeMale");
info.setPhone("12334567");
employee.setInfo(info);
info.setEmp(employee);
address.setEmp(employee);
session.save(employee);
transaction.commit();
session.close();
}
}
改变表信息 添加约束 FKdjqy16j48ffa2x19eqgx3o8f0 外键 (emp_id) 员工 (empId) 休眠: 插入 进入 员工 (电子邮件,姓名) 价值观 (?,?) 休眠: 插入 进入 信息 (emp_id, 性别, 电话) 价值观 (?,?,?)
数据未插入地址表 为什么??
看起来您已经正确设置了 Employee、Info 和 Address 实体之间的一对一映射。但是在
OneToOneApp
main 方法中,在保存员工之前,您还没有设置员工的地址。
要解决此问题,您需要在保存员工之前在
OneToOneApp
main 方法中添加以下行:
employee.setAddress(address);
这是更新后的
OneToOneApp
主要方法:
public class OneToOneApp {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("o2omapping.cfg.xml");
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee =new Employee();
employee.setName("Iqra");
employee.setEmail("[email protected]");
Address address = new Address();
address.setStreet("Manda Road");
address.setCity("Allahabad");
address.setCountry("India");
Info info = new Info();
info.setGender("FeMale");
info.setPhone("12334567");
employee.setInfo(info);
info.setEmp(employee);
employee.setAddress(address); // Add this line
address.setEmp(employee);
session.save(employee);
transaction.commit();
session.close();
}
}
现在,当您运行更新后的应用程序时,员工的地址也应该插入到 Address 表中。
他说的是实话。您已经在具有一对一关系依赖关系的客户类中设置了地址对象,但是您还没有设置它的信息。您应该在
employee.setAddress()
, employee.setInfo()
. 这行之后添加