简体   繁体   English

自动休眠保存关系

[英]Automatically hibernate save relationship

Basically my question is why if I have an Hibernate relationship like this one. 基本上,我的问题是为什么我要拥有像这样的休眠关系。

  @OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name = "candidacy_id", nullable = false)
@XmlElement
@JsonIgnore
@Getter
@Setter
private List<EvaluationSelectionCriteria> evaluationSelectionCriterias = new ArrayList<>();

   @ManyToOne
   @JoinColumn(name = "candidacy_id", nullable = false, insertable = false, updatable = false)
   @XmlTransient
  @Getter
  @Setter
   private Candidacy candidacy;

Why if I do this candidacy.setEvaluationSelectionCriteria(list) automatically this list is persisted in database? 为什么如果我自动执行此candidacy.setEvaluationSelectionCriteria(list),则此列表将保留在数据库中吗?

I would like to use the EvaluationSelectionCriteria as a repository to render a list of "future" EvaluationSelectionCriteria 我想将EvaluationSelectionCriteria用作存储库以呈现“未来” EvaluationSelectionCriteria的列表

Could be because is not Lazy? 可能是因为不是懒惰吗?

More detail explanation 更详细的解释

So would be like I call method a, there I´m get from database entity A then I set a list into A and then I return A in the method but I´m not saving A, when I see the value of the list already have ids!!! 因此,就像我调用方法a一样,我从数据库实体A获取数据,然后将一个列表设置为A,然后在该方法中返回A,但是当我已经看到列表的值时,我没有保存A有身份证!

If you do not want the list to be saved when the parent entity is saved/merged, you should remove or restrict the cascade setting for the relationship: 如果您不希望在保存/合并父实体时保存列表,则应删除或限制关系的级联设置:

@OneToMany
private List<EvaluationSelectionCriteria> evaluationSelectionCriterias

or 要么

@OneToMany(cascade = CascadeType.REMOVE) // or other values from the enum
private List<EvaluationSelectionCriteria> evaluationSelectionCriterias

EDIT : If you want to fetch an entity in a transactional method and modify it, you can restrict the scope of the transaction to the fetching only. 编辑 :如果要在事务方法中获取实体并对其进行修改,则可以将事务的范围限制为仅获取。 Then modify the entity outside the transactional method. 然后在事务方法之外修改实体。 Later, you can merge the detached entity if needed. 以后,如果需要,您可以合并分离的实体。

Since collection attributes are lazy per default you will either need to 由于默认情况下集合属性是惰性的,因此您要么需要

  • access their content while still inside the transactional method - so the collection can be fetched from the DB. 仍在事务方法内部访问它们的内容-可以从数据库中获取集合。 Please note that you will have to call a method on the collection that actualy requires it's content to be loaded, like getCriterias().size() . 请注意,您将必须在集合上调用一个实际上需要加载其内容的方法,例如getCriterias().size()

  • use LEFT JOIN FETCH to load the collection as a side effect of the query. 使用LEFT JOIN FETCH加载集合作为查询的副作用。

I would not modify the FlushMode for the session - while this would probably work, it feels like a kludge - it does not communicate your intent very well. 我不会修改该会话的FlushMode虽然这可能会起作用,但感觉就像是在跳动-它不能很好地传达您的意图。 Explicitly fetching the collection and modifying it outside the transaction expresses your intent better IMO. 显式地获取集合并在事务外部对其进行修改可以更好地表达您的意图。

我找到了解决方案,忘了说我在使用Spring,所以最后我将@Transactional(readOnly = true)添加到我的方法中,而不是在服务类级别。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM