问题描述
User类定义:@OneToMany(mappedBy = "user", cascade=CascadeType.ALL, fetch = FetchType.LAZY)Set<Pet> pets = null;Pet类定义:@ManyToOne()@JoinColumn(name = "ower_id")User user = null;数据库的user表和pet表通过外键ower_id关联打印出的sql语句:Hibernate: /* insert edu.ustc.pet.entity.User */ insert into user (name) values (?)Hibernate: /* insert edu.ustc.pet.entity.Pet */ insert into pet (name, ower_id) values (?, ?)为什么pet表的ower_id值总是空???(应该是user的id值啊)
解决方案
user.setPets(pets)for (Pet pet : pets) { pet.setUser(user);}这样就行啊
时间: 2024-09-28 16:21:17