一:看例子
二:知识点
org.hibernate.TransientObjectException: object references an unsaved transient instance -
save the transient instance before flushing: com.hibernate.model.UserGroup
User u = new User();
u.setName("liuyanliang");
UserGroup g = new UserGroup();
g.setName("haha");
u.setGroup(g);
Session session = factory.getCurrentSession();
session.beginTransaction();
session.save(u);
session.getTransaction().commit();
默认情况下,save user的时候,不会自动关联user跟group的关系
需要在映射关系中加上级联操作
@ManyToOne(cascade = CascadeType.ALL)
private UserGroup group;
默认不产生任何级联
CascadeType.ALL
CascadeType.MERGE
CascadeType.PERSISTENCE
CascadeType.REFRESH
CascadeType.REMOVE
时间: 2024-11-05 02:59:41