我有一个 Employee
使用以下数据成员初始化:
@Id
private int empId;
private String empName;
private String department;
private String baseLocation;
@OneToOne
@JoinColumn(name="address_id")
private Address address;
和一个 Address
班级:
@Id
private int address_id;
private String city;
private String pincode;
当我储存 Employee
对象放入数据库中 Address
对象存储为 null
,即使 Address
对象作为参数传递。
我正在使用jparepository和springboot。我的方法如下
@Autowired
EmployeeServiceImpl empService;
public static void main(String[] args) {
SpringApplication.run(EmployeeManagementApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Address address = new Address(5,"pun","591237");
Employee emp = new Employee(11, "Abc", "cse", "pune", address);
System.out.println("ghii");
try {
service.insertAddress(address);
empService.AddEmployee(emp);
//invoke service layer method to insert Customer
System.out.println("Records are successfully added..");
System.out.println("reading");
Employee emploee = empService.getEmployee(11);
System.out.println(emploee.getDepartment());
}
catch(Exception e) {
System.out.println("in exception");
e.printStackTrace();
}
请帮我把这些对象存入数据库。
1条答案
按热度按时间kdfy810k1#
尝试
更多关于级联的信息:这里
upd:详细类(使用lombok表示实体):
输出: