oneToMany连接时如何避免持久保存新对象? [英] How to avoid persisting new object when oneToMany connection?

查看:124
本文介绍了oneToMany连接时如何避免持久保存新对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

returnreason-table中有三行,我不想在那儿有更多行.用户只需选择以下三个原因之一,而我只希望id进入rma-table.现在,每次插入新的rma时,都会在原因表中插入新的一行.我可以取消表之间的关系,但是我想知道是否存在更好的解决方案,以避免在保留rma对象时插入新行?如果我从Rma类中取消了级联类型,那将无济于事.然后我收到一条错误消息,jpa找到了一个未持久的对象或类似的对象.

There is a three lines in returnreason-table and I don't want more lines there. User just choose one of the three reasons and I want only id to rma-table. Now it inserts a new line to reason-table every time when inserting new rma. I can take relation off between the tables but I wonder if there is better solution to avoid inserting new lines when persisting rma object? If I take cascadeType off from the class Rma, it is not helping/working. Then I got an error message, that jpa found an object, which is not persisted or something like that.

如果我取消了层叠类型,则会出现错误消息.全部关闭

java.lang.IllegalStateException:在同步期间,通过未标记为层叠PERSIST的关系找到了一个新对象:com.entity.Returnreason [returnreasonId = null].

java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.entity.Returnreason[ returnreasonId=null ].

public class Rma implements Serializable {
@ManyToOne(cascade = CascadeType.ALL)
private Returnreason returnreasonReturnreasonId;

public class Returnreason implements Serializable {
@OneToMany(mappedBy = "returnreasonReturnreasonId", cascade = CascadeType.ALL)
private Collection<Rma> rmaCollection;

推荐答案

JPA方法要求模型使用托管实体,而您似乎正在尝试将托管实体与上下文之外的内容相关联.这是一种不良做法,并且JPA被要求引发异常,因为它无法告知您打算对非托管实例执行的操作.

JPA methods require that the model use managed entities, while you seem to be attempting to associate a managed entity to something outside the context. This is a bad practice, and JPA is required to throw exceptions as it cannot tell what you intend it to do with the unmanaged instance.

您有两个选择

  1. 阅读您的3个Returnreason并使用这些实体进行合并. 如果确实只有3个,则可以更改缓存选项 因此它们始终在缓存中,以便进行em.find操作 不必点击数据库.
  2. 将Rma类中的映射删除到Returnreason类,然后 将returnreasonReturnreasonId字段映射为基本映射.你 然后可以直接在Rma实体中设置值
  1. Read in your 3 Returnreason and use those entities for merging. If there really are only ever 3, you can change your caching options so they are always in the cache, so that an em.find operation doesn't have to hit the database.
  2. Remove the mapping in your Rma class to the Returnreason class and map the returnreasonReturnreasonId field as a basic mapping. You then can set the value in the Rma entity directly

第一个选项可能是最常用的,并且确实是必需的,因为无论何时每次添加新的Rma实例,您都应该一直维护Returnreason rmaCollection.似乎您只需要设置Rma.returnreasonReturnreasonId,但这是java对象,您的应用程序负责维护双向关系的双方.

The first option is probably the most frequently used, and really is required since you should have been maintaining the Returnreason rmaCollection anyway everytime you add a new Rma instance. While it may seem like you only need to set the Rma.returnreasonReturnreasonId, these are java objects, and your application is responsible for maintaining both sides of bidirectional relationships.

这篇关于oneToMany连接时如何避免持久保存新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆