Hibernate在事务提交时删除集合元素

时间:2016-11-25 12:01:32

标签: java hibernate

我无法找出为什么hibernate会在事务提交后尝试删除集合元素。我甚至没有要求它这样做。

有什么想法吗?

技术详情发布在下面。

Hibernate方言:

    org.hibernate.dialect.SQLServerDialect

实体类:

    @Entity
    @Table(name = "View_m_activity", schema = "model")
    public class SourceActivity {
        private Set<SourceActivityAttrOut> outputAttributes;

        private Long ID;

        @Id
        public Long getID() {
            return ID;
        }

        public void setID(Long ID) {
            this.ID = ID;
        }

        @ElementCollection(fetch = FetchType.EAGER)
        @CollectionTable(name = "View_m_activity_attr_out", schema = "model", joinColumns = @JoinColumn(name = "activity_id"))
        public Set<SourceActivityAttrOut> getOutputAttributes() {
            return outputAttributes;
        }

        public void setOutputAttributes(Set<SourceActivityAttrOut> outputAttributes) {
            this.outputAttributes = outputAttributes;
        }
    }

集合元素实体类:

    @Embeddable
    public class SourceActivityAttrOut {
        private String code;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }

执行代码:

    @Transactional
    public void readSource(SourcePool sourcePool) {
        sessionFactory.getCurrentSession().createQuery("from SourceActivity", SourceActivity.class).list();
    }

记录信息:

Hibernate: 
    select
        sourceacti0_.ID as ID1_0_ 
    from
        model.View_m_activity sourceacti0_

Hibernate: 
    select
        outputattr0_.activity_id as activity1_1_0_,
        outputattr0_.code as code2_1_0_ 
    from
        model.View_m_activity_attr_out outputattr0_ 
    where
        outputattr0_.activity_id=?

Hibernate: 
    select
        outputattr0_.activity_id as activity1_1_0_,
        outputattr0_.code as code2_1_0_ 
    from
        model.View_m_activity_attr_out outputattr0_ 
    where
        outputattr0_.activity_id=?

Hibernate: 
    select
        outputattr0_.activity_id as activity1_1_0_,
        outputattr0_.code as code2_1_0_ 
    from
        model.View_m_activity_attr_out outputattr0_ 
    where
        outputattr0_.activity_id=?

Hibernate: 
    select
        outputattr0_.activity_id as activity1_1_0_,
        outputattr0_.code as code2_1_0_ 
    from
        model.View_m_activity_attr_out outputattr0_ 
    where
        outputattr0_.activity_id=?

Hibernate: 
    delete 
    from
        model.View_m_activity_attr_out 
    where
        activity_id=?

1 个答案:

答案 0 :(得分:1)

问题的原因描述为here

很快,为了解决这个问题,我不得不重写hashcode和SourceActivityAttrOut的等号。