简体   繁体   English

保存惰性子项导致:TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例

[英]Saving lazy child is causing: TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

I'm experimenting with lazy loading entities in hibernate but could not get over the error above. 我正在休眠中尝试延迟加载实体,但无法克服上面的错误。 I'm basically playing with 2 entities: User and Role: 我基本上是在玩两个实体:用户和角色:

public class User implements Serializeable {
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "T_USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    private Set<Role> roles = new HashSet<Role>();
}

public class Role implements Serializeable { }

And then I have an action bean that creates the User entity, let's called this approach 1: 然后,我有一个操作bean创建了User实体,我们称这种方法为1:

user.getRoles().add(roleService.findByName(RolesEnum.ADMIN.toString()));
em.persist(user);

And then I tried to persist the user entity first before setting the roles and updating but it also failed: 然后我尝试在设置角色和更新之前先保留用户实体,但是它也失败了:

userService.create(user);
User user = userService.findById(user.getId());
Set<Role> roles = new HashSet<Role>();
roles.add(roleService.findByName(RolesEnum.MEMBER.toString()));
user.setRoles(roles);
userService.update(user, true);

Note that: 注意:

  • userService.create = persist userService.create =坚持
  • userService.update = merge userService.update =合并
  • MEMBER role already exists in roles table 角色表中已存在成员角色
  • I also tried fetching the roles: entity.getRoles(); 我还尝试获取角色:entity.getRoles();

Below is the complete error log: 以下是完整的错误日志:

java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.Role org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1374)

This problem is solved by binding the entity manager to conversation scope and using seam-solder. 通过将实体管理器绑定到对话范围并使用seam-solder可以解决此问题。

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
public @interface TempJpa { }

@ExtensionManaged
@ConversationScoped
@Produces
@PersistenceUnit(unitName="tempDataSource")
@TempJpa
private EntityManagerFactory emf;

@Inject
@TempJpa
protected EntityManager em;

But the next question is why? 但是下一个问题是为什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TransientObjectException:对象引用了一个未保存的临时实例-在执行合并时在刷新之前保存该临时实例 - TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing when I am doing merge TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - TransientObjectException - object references an unsaved transient instance - save the transient instance before flushing TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing org.hibernate.TransientObjectException:object 引用了一个未保存的瞬态实例 - 在刷新之前保存瞬态实例: - org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例 - object references an unsaved transient instance - save the transient instance before flushing 对象引用一个未保存的瞬态实例,在刷新之前保存瞬态实例: - Object references an unsaved transient instance save the transient instance before flushing: hibernate.TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例-休眠升级后 - hibernate.TransientObjectException:object references an unsaved transient instance-save the transient instance before flushing-after hibernate upgrade 保存实体时JPA错误,对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例 - JPA error while saving entity, object references an unsaved transient instance - save the transient instance before flushing 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM