简体   繁体   English

没有双向实体的休眠关系问题

[英]Hibernate relationship issues on no bidirectional entities

Hi there I have an entity 嗨,我有一个实体

OfferItem with this two relationship manyToOne OfferItem与这两个关系manyToOne

     @ManyToOne
@JoinColumn(name = "offer_id", nullable = false, insertable = false, updatable = false)
@XmlTransient
@Getter
@Setter
private Offer offer;

@ManyToOne
@JoinColumn(name = "item_id", nullable = false, insertable = false, updatable = false)
@XmlTransient
@Getter
@Setter
private Item item;

And the foreign keys configuration on the database 并在数据库上配置外键

             <addForeignKeyConstraint baseTableName="offer_item"
                             baseColumnNames="item_id"
                             constraintName="item_offer_item_fk"
                             referencedTableName="item"
                             onDelete="CASCADE" onUpdate="CASCADE"
                             referencedColumnNames="id"/>

    <addForeignKeyConstraint baseTableName="offer_item"
                             baseColumnNames="offer_id"
                             constraintName="offers_offer_item_fk"
                             onDelete="CASCADE" onUpdate="CASCADE"
                             referencedColumnNames="id"
                             referencedTableName="offer"/>

the relationship with offer is bidireccional, so on the Offer entity I have this relationship with OfferItem 与要约的关系是双向的,所以在要约实体上,我与OfferItem有这种关系

          @OneToMany(orphanRemoval = true, cascade = {CascadeType.ALL})
@JoinColumn(name = "offer_id", nullable = false)
@XmlElementWrapper
@Getter
private List<OfferItem> offerItems = new ArrayList<>();

On Item I dont have the bidireccional relationship since is not necessary. 在项目上,我没有投标关系,因为没有必要。

Now the problem is, that when I made the set of Item on OfferItem, and I add the offerItem on the OfferItems list of offer, and I save Offer. 现在的问题是,当我在OfferItem上创建商品集时,我将offerItem添加到商品的OfferItems列表上,然后保存商品。 I can see how the offerItem id is generated because is persisted on cascade. 我可以看到offerItem id是如何生成的,因为它在级联上保持不变。 But the link between the offerItem and item is lost. 但是offerItem和item之间的链接丢失了。

I just try to set the item on OfferItem and save the entity with his own service, but nothing. 我只是尝试在OfferItem上设置项目,并使用他自己的服务保存实体,但是什么也没有。 I cannot persist this link between them. 我无法在他们之间保持这种联系。

Any idea or suggestion. 任何想法或建议。

Regards. 问候。

Remove insertable = false, updatable = false from join columns. 从连接列中删除insertable = false, updatable = false Also, the mapping for offerItems is wrong. 另外, offerItems的映射是错误的。 It should be 它应该是

@OneToMany(orphanRemoval = true, cascade = {CascadeType.ALL}, mappedBy = "offer")
@XmlElementWrapper
@Getter
private List<OfferItem> offerItems = new ArrayList<>();

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

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