Hibernate 保持一对多单向关系时外键为空 上下文
我有两个实体,映射如下:Hibernate 保持一对多单向关系时外键为空 上下文,hibernate,junit,Hibernate,Junit,我有两个实体,映射如下: @Entity public class User { @Id private String mail; @OneToMany(cascade={CascadeType.REMOVE} fetch=FetchType.EAGER) private Set<Food> foods; 我的测试: @Test public void testPersistUserWithFood() throws Exception{ //I create and
@Entity
public class User {
@Id
private String mail;
@OneToMany(cascade={CascadeType.REMOVE} fetch=FetchType.EAGER)
private Set<Food> foods;
我的测试:
@Test
public void testPersistUserWithFood() throws Exception{
//I create and persist the user
User user = new User ("@someone");
Session sess = HibernateUtil.getSessionFactory().openSession();
Transaction tx = sess.beginTransaction();
sess.save(user);
tx.commit();
sess.close();
//Now I create de food and set the relation in object schema
Food foo = new Food();
user.getFoods.add(foo);
//I persist the food in the same way than user, with session, transaction and so on.
//Then I retrieve the user from DB
sess = HibernateUtil.getSessionFactory().openSession();
User userRetrieved =(User) sess.createCriteria(User.class)
.add(Restrictions.idEq("@someone")).uniqueResult();
sess.close();
//Finally assert
assertEquals(user.getFoods(), userRetrieved.getFoods());
}
问题
测试失败,因为调用:userRetrieved.getFoods()返回空列表。
调试后,我看到table Food中的id_User foring列始终为null。
如果关系是oneToMany双向的,则列id_User不为null,因为我在Food类中添加了User属性
谢谢你的帮助
注意:我使用的是Hibernate4.3.8、JUnit4和Java8
userRetrieved.getFoods() return a null list.
这意味着在添加食物时不会更新用户
...
...
//Now I create de food and set the relation in object schema
//I persist the food in the same way than user, with session, transaction and so on.
Session sess = HibernateUtil.getSessionFactory().openSession();
Transaction tx = sess.beginTransaction();
Food foo = new Food();
user.getFoods.add(foo);
sess.update(user); //are you updating User as well like this?
sess.save(foo);
tx.commit();
sess.close();
...
...
另外,如果它不起作用,那么请发布完整的代码,显示您是如何持久化所有实体的。您拥有
id\u User
列null
,因为id
等于null
在新的Food
对象中
Food foo = new Food();
user.getFoods().add(foo);
要解决此问题,您需要将一个新的Foo
对象保存到Hibernate,并为其分配一个id
。然后将保存的Foo
添加到foods
集合中。或者您可以使用cascade=CascadeType.ALL
将新的子对象与父对象一起休眠保存
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private Set<Food> foods;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
私家菜;
谢谢大家,我解决了我的问题,代码如下:
一个实体
试验
总结:
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private Set<Food> foods;
@Entity
public class User {
@Id
private String mail;
@OneToMany(cascade={CascadeType.REMOVE} fetch=FetchType.EAGER)
@JoinColumn(name="id_User")
private Set<Food> foods;
@Entity
public class Food{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Test
public void testPersistUserWithFood() throws Exception{
//I create and persist the user
User user = new User ("@someone");
Session sess = HibernateUtil.getSessionFactory().openSession();
Transaction tx = sess.beginTransaction();
sess.save(user);
tx.commit();
sess.close();
//Now I create de food and set the relation in object schema
Food foo = new Food();
user.getFoods.add(foo);
//I persist the food in the same way than user, with session, transaction and so on.
//Hibernate don´t know that the user contains the food when persist the
// food, because the relationship is unidirectional, therefore:
sess = HibernateUtil.getSessionFactory().openSession();
tx = sess.beginTransaction();
sess.update(user);
tx.commit();
sess.close();
//Then I retrieve the user from DB
sess = HibernateUtil.getSessionFactory().openSession();
User userRetrieved =(User) sess.createCriteria(User.class)
.add(Restrictions.idEq("@someone")).uniqueResult();
sess.close();
//Finally assert
assertEquals(user.getFoods(), userRetrieved.getFoods());
}