使用JPA与Hibernate实现:entityManager.remove - 无法正常工作 [英] Using JPA with Hibernate implementation: entityManager.remove - not working

查看:302
本文介绍了使用JPA与Hibernate实现:entityManager.remove - 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Hibernate 4.1.4.Final 实现使用JPA。我的问题是,我不能得到 EntityManager#remove()工作。所有其他:更新,插入,选择操作工作正常,除了这一个。



我的 persistence.xml 文件:




 <持久性单位名称= myEntityManager > 
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> core.entity.Answer< / class>
< class> core.entity.BaseEntity< / class>
< exclude-unlisted-classes> true< / exclude-unlisted-classes>
< validation-mode> NONE< / validation-mode>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< / properties>
< / persistence-unit>



我的 实体:( BaseEntity类只包含主键 - ID)

  @Entity 
@Table(name =ANSWER)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Answer BaseEntity {

@Column(name =ANSWER_VALUE )
private String answerValue;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =QUESTION_ID,nullable = false)
private问题问题;

// overrided equals,hascode,toString
//所有getter和setters
}

在调用时:

  public void remove(T entity){
this .entityManager.remove(this.entityManager.merge(实体));
}

还试过:

  this.entityManager.remove(实体); 

和:

  T ref = entityManager.getReference(entityClass,primaryKey); 
entityManager.remove(ref);

不运气:(它不会删除的记录答案数据库。

我使用Spring配置来配置实体管理器,如下所示:

 < bean id =persistenceUnitManagerclass =org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager> 
< property name =persistenceXmlLocations>
< list>
< value> classpath *:META-INF / persistence.xml< /值>
< / list>
< / property>
< property name =defaultDataSourceref =dataSource/>
< / bean>

< bean id =entityManagerFactoryclass =org.springframework.orm .jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitManagerref =persistenceUnitManager/>
< property name =persistenceUnitNamevalue =myEntityManager/>
< / bean>

< bean id =tra nsactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

< tx:annotation-driven proxy-target-class =truetransaction-manager =transactionManager/>

remove 在方法注释为 @Transactional 注释。所以它不应该是交易问题。



我启用了Hibernate SQL日志记录,所有其他日志记录。我没有在任何地方看到 delete 查询会被执行或者任何错误。



的选择。请咨询。



编辑 可能这也有帮助:

>

我从其他实体获得答案列表。答案是这样定义的:

  @OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy =evaluationPart )
私人列表<答案> answers = new LinkedList<>();

调用如下所示:

 列表与LT;应答和GT; answers = evaluationPart.getAnswers(); 
if(answers!= null&& answers.size()> 0){
for(Answer answer:answers){
answerFacade.remove(answer); //这会调用enetityManager.remove


code $ <$ $ p

解决方案

我可能没有这个主题,但这些可能是问题所在。

第一个原因 -
没有在此条目中定义的cascadeType

  @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = QUESTION_ID,可为空= false)
私人问题问题;

第二个原因(更多建议)



由于Answer的链表已映射到Question对象,因此不建议直接从列表中删除单个对象。更好地从链接列表中删除元素,然后更新/合并问题对象,该对象将在应答表上自动执行删除操作。



希望这有助于: p>

I am using JPA over Hibernate 4.1.4.Final implementation. My problem is that I can't get EntityManager#remove() working. All other: update, insert, select operation are working just fine except this one.

My persistence.xml file:

<persistence-unit name="myEntityManager">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>core.entity.Answer</class>
    <class>core.entity.BaseEntity</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <validation-mode>NONE</validation-mode>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

My Answer entity: (BaseEntity class just holds the primary key - ID)

@Entity
@Table(name = "ANSWER")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Answer extends BaseEntity {

    @Column(name = "ANSWER_VALUE")
    private String answerValue;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "QUESTION_ID", nullable = false)
    private Question question;                

    //overrided equals, hascode, toString 
    // all getters and setters
}

When calling:

public void remove(T entity) {
    this.entityManager.remove(this.entityManager.merge(entity));
}

Also tried:

this.entityManager.remove(entity);

And:

T ref = entityManager.getReference(entityClass, primaryKey);
entityManager.remove(ref);

No luck :( it is not deleting the record of Answer from the database.

I am using Spring configuration to configure the entity manager like this:

<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath*:META-INF/persistence.xml</value>
        </list>
    </property>
    <property name="defaultDataSource" ref="dataSource"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
    <property name="persistenceUnitName" value="myEntityManager"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>

remove is called in the method which is annotated with @Transactional annotation. So it shouldn't be transactions issue.

I have enabled Hibernate SQL logging, all the other logging. I am not seeing anywhere that delete query would be executed or any error at all.

And I am really out of options here. Please advice.

EDIT

Maybe this will also help:

I am getting the list of answers from the other entity. The answers are defined like this:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "evaluationPart")
private List<Answer> answers = new LinkedList<>();

Calling remove like this:

List<Answer> answers = evaluationPart.getAnswers();
if (answers != null && answers.size() > 0) {
    for (Answer answer : answers) {
        answerFacade.remove(answer); //This calls enetityManager.remove
    }
}

解决方案

I may be out of the topic but these can be the issues for the problem

First Reason - No cascadeType defined in this entry

 @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "QUESTION_ID", nullable = false)
    private Question question;  

Second Reason (More of a suggestion)

As the linkedlist of Answer is mapped to the Question object, deleting a single object from the list directly is not recommended as a good practice. Better remove the element from the linked list and then update/merge the question object which will perform the remove operation automatically on the Answer table.

Hope this helps :)

这篇关于使用JPA与Hibernate实现:entityManager.remove - 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆