我有两个实体:PlayerProfileEntity 和 UserInfoEntity 我加入了 userInfoEntity 和 PlaterProfileEntity,并将我的记录保存在数据库中,如下所示:
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
Criteria crit = session.createCriteria(PlayerProfileEntity.class);
player.setUserId(new UserInfoEntity());
player.getUserId().setAddress(user.getUserId().getAddress());
session.save(player);
tr.commit();
我在我的 PlayerProfileEntity 类中使用了这个关联
@ManyToOne (fetch = FetchType.LAZY)
@Cascade({CascadeType.REFRESH})
@JoinColumn(name="userid")
我收到此错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.shared.entity.UserInfoEntity
注意:如果我使用 CascadeType.All,我会收到此错误:
Cannot add or update a child row: a foreign key constraint fails
知道我该如何解决这个问题
谢谢