java 在 Hibernate 实现中使用 JPA:entityManager.remove - 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15679620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using JPA with Hibernate implementation: entityManager.remove - not working
提问by user2219247
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.
我在Hibernate 4.1.4.Final
实现上使用 JPA 。我的问题是我无法开始EntityManager#remove()
工作。所有其他:更新、插入、选择操作都工作得很好,除了这个。
My persistence.xml
file:
我的persistence.xml
文件:
<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)
我的Answer
实体:(BaseEntity 类只保存主键 - 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.
不走运:(它不会Answer
从数据库中删除 的记录。
I am using Spring configuration to configure the entity manager like this:
我正在使用 Spring 配置来配置实体管理器,如下所示:
<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.
remove
在用@Transactional
注解注解的方法中调用。所以应该不是交易问题。
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.
我已启用 Hibernate SQL 日志记录,以及所有其他日志记录。我没有看到任何地方delete
会执行查询或任何错误。
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:
像这样调用 remove:
List<Answer> answers = evaluationPart.getAnswers();
if (answers != null && answers.size() > 0) {
for (Answer answer : answers) {
answerFacade.remove(answer); //This calls enetityManager.remove
}
}
采纳答案by Code2Interface
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.
由于 Answer 的链表映射到 Question 对象,因此不推荐直接从列表中删除单个对象作为一种好习惯。最好从链表中删除元素,然后更新/合并问题对象,这将在答案表上自动执行删除操作。
Hope this helps :)
希望这可以帮助 :)