Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing JPA with Hibernate implementation: entityManager.remove - not working
    primarykey
    data
    text
    <p>I am using JPA over <code>Hibernate 4.1.4.Final</code> implementation. My problem is that I can't get <code>EntityManager#remove()</code> working. All other: update, insert, select operation are working just fine except this one.</p> <p>My <code>persistence.xml</code> file:</p> <p> </p> <pre><code>&lt;persistence-unit name="myEntityManager"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;class&gt;core.entity.Answer&lt;/class&gt; &lt;class&gt;core.entity.BaseEntity&lt;/class&gt; &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt; &lt;validation-mode&gt;NONE&lt;/validation-mode&gt; &lt;properties&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt; &lt;property name="hibernate.show_sql" value="true"/&gt; &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre> <p></p> <p>My <code>Answer</code> entity: (BaseEntity class just holds the primary key - ID)</p> <pre><code>@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 } </code></pre> <p>When calling:</p> <pre><code>public void remove(T entity) { this.entityManager.remove(this.entityManager.merge(entity)); } </code></pre> <p>Also tried:</p> <pre><code>this.entityManager.remove(entity); </code></pre> <p>And:</p> <pre><code>T ref = entityManager.getReference(entityClass, primaryKey); entityManager.remove(ref); </code></pre> <p>No luck :( it is not deleting the record of <code>Answer</code> from the database.</p> <p>I am using Spring configuration to configure the entity manager like this:</p> <pre><code>&lt;bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt; &lt;property name="persistenceXmlLocations"&gt; &lt;list&gt; &lt;value&gt;classpath*:META-INF/persistence.xml&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="defaultDataSource" ref="dataSource"/&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitManager" ref="persistenceUnitManager"/&gt; &lt;property name="persistenceUnitName" value="myEntityManager"/&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt; &lt;/bean&gt; &lt;tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/&gt; </code></pre> <p><code>remove</code> is called in the method which is annotated with <code>@Transactional</code> annotation. So it shouldn't be transactions issue.</p> <p>I have enabled Hibernate SQL logging, all the other logging. I am not seeing anywhere that <code>delete</code> query would be executed or any error at all.</p> <p>And I am really out of options here. Please advice.</p> <p><strong>EDIT</strong></p> <p>Maybe this will also help:</p> <p>I am getting the list of answers from the other entity. The answers are defined like this:</p> <pre><code>@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "evaluationPart") private List&lt;Answer&gt; answers = new LinkedList&lt;&gt;(); </code></pre> <p>Calling remove like this:</p> <pre><code>List&lt;Answer&gt; answers = evaluationPart.getAnswers(); if (answers != null &amp;&amp; answers.size() &gt; 0) { for (Answer answer : answers) { answerFacade.remove(answer); //This calls enetityManager.remove } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload