How to avoid Hibernate generating two queries for an update with OneToMany?

旧巷老猫 提交于 2019-12-13 01:12:18

问题


I'm facing the same situation as in this question, that has no useful answer.

When I add a new element to the many part of my one-to-many relation, Hibernate generates two queries, one to insert and one to update the foreign key to the parent.

Why does it need the second query for? Isn't the parent's id set in the insert? Is there any way of avoiding this?

    Hibernate:
        /* insert mydomain.LanguageKnowledge */
            insert
            into
                languageKnowledge
                (language_fk, level_fk, personId_fk)
            values
                (?, ?, ?)
    Hibernate:
        /* create one-to-many row mydomain.Person.offeredLanguages */
        update
            languageKnowledge
        set
            personId_fk=?
        where
            id=?



    public class LanguageKnowledge {

        @Id
        @GeneratedValue(strategy = IDENTITY)
        private Integer id;

        @Enumerated(STRING)
        @Column(name = "language_fk")
        private LanguageIso639_3 language;

        @Enumerated(STRING)
        @Column(name = "level_fk")
        private LanguageLevel level;

        protected LanguageKnowledge() {
        }
    }


    public class Person {

        @Id
        @GeneratedValue(strategy = IDENTITY)
        private Integer id;

        @OneToMany(fetch = EAGER, cascade = {ALL}, orphanRemoval = true)
        @JoinColumn(name = "personId_fk", referencedColumnName = "id", nullable = false)
        private final Set<LanguageKnowledge> offeredLanguages = new HashSet<>();

        public Person(Set<LanguageKnowledge> offeredLanguages) {
            addOfferedLanguages(offeredLanguages);
        }

        protected Person() {
        }

        public void addOfferedLanguages(Set<LanguageKnowledge> offeredLanguages) {
            this.offeredLanguages.addAll(offeredLanguages);
        }

        public void removeOfferedLanguages(Set<LanguageKnowledge> offeredLanguagesToRemove) {
            this.offeredLanguages.removeAll(offeredLanguagesToRemove);
        }
    }

回答1:


The association is uni-directional, so Person is the owning side (because it's the only side).

Make the association bidirectional and make LanguageKnowledge the association owner. That way you will avoid redundant updates because the foreign key values will be specified as part of insert statements for LanguageKnowledge.



来源:https://stackoverflow.com/questions/35223952/how-to-avoid-hibernate-generating-two-queries-for-an-update-with-onetomany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!