I am using JPA over implementation Hibernate 4.1.4.Final. My problem is that I can’t work EntityManager#remove(). All the rest: update, insert, selection operation only work perfectly, except for this.
My persistence.xmlfile:
<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 Answerentity: (the BaseEntity class just contains 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;
}
When called:
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 does not delete the record Answerfrom the database.
I am using Spring configuration to configure entity manager as follows:
<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"/>
removecalled in a method that is annotated with an annotation @Transactional. So this should not be a problem.
Hibernate SQL, . , delete .
. , .
, :
. :
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "evaluationPart")
private List<Answer> answers = new LinkedList<>();
:
List<Answer> answers = evaluationPart.getAnswers();
if (answers != null && answers.size() > 0) {
for (Answer answer : answers) {
answerFacade.remove(answer);
}
}