5

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
    }
}
7
  • And your other Create/Update methods all work ok and persist as expected? are those methods in the same class and using the same setup? Commented Mar 28, 2013 at 10:52
  • Yes. Everything else is working, everything is the same for other operations. Commented Mar 28, 2013 at 10:55
  • @rhinds I've updated the question. Maybe you'd be able to see the problem now. Commented Mar 28, 2013 at 12:21
  • any errors in the server logs Commented Mar 28, 2013 at 12:24
  • No, nothing related to errors at all, no errors, just usual hibernate stuff and SQL.. Commented Mar 28, 2013 at 12:27

1 Answer 1

4

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

No cascade type is defined because I don't want this object to be cascaded in any way :) that's fine. Your second reason gave a way to the solution. Yes I don't need to call entityManager.remove if I am removing it from the list, so that worked. Thanks. Still can't fully understand why hibernate did not clear the persistence context. Maybe because the objects were lazy loaded...
Just had the same issue, it would be nice if Hibernate would at least yell at you for removing a list item by calling em.remove(ref). Oh well, lesson learned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.