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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With