Hibernate Many to Many不保存更改

时间:2016-01-06 13:49:07

标签: java hibernate jpa

我有两个实体Category和Community,它们通过Many To Many注释相互关联,我可以从我的db(postgres)中检索数据,但是当我在Hibernate实体管理器上调用merge方法时,我无法保存更改,什么都没有。

这是分类实体

@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "category_community", joinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id") , inverseJoinColumns = @JoinColumn(name = "community_id", referencedColumnName = "id") )
private List<Community> communities;

这是社区实体映射

@ManyToMany(mappedBy = "communities", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private List<Category> categories;

当我喜欢这个时

Community community = communityService.getById(id);
community.setCategories(newCategories);
entityManager.marge(community);

更改不会在db

中持续存在

persistance.xml

<persistence-unit name="ecclUnit" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/datasources/EcclDS</jta-data-source>
    <jar-file>lib/eccl-common.jar</jar-file>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.use_sql_comments" value="true" />
        <property name="hibernate.generate_statistics" value="true" />
        <property name="hibernate.connection.autocommit" value="true"  /> 

        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.max_size" value="20" />
        <property name="hibernate.c3p0.timeout" value="500" />
        <property name="hibernate.c3p0.max_statements" value="50" />
        <property name="hibernate.c3p0.idle_test_period" value="2000" />
    </properties>
</persistence-unit>

0 个答案:

没有答案