title: javax.persistence.OneToMany.orphanRemoval<>Z
body: <p>I've searched everywhere but I didn't found a good answer.<br>
  I am developping a maven application using Hibernate + Spring + Mysql.<br>
  Here is my application-context.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http:..."&gt;
  
  &lt;bean id='dataSource' class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="url" value="jdbc:mysql://localhost/university"/&gt;
      &lt;property name="username" value="root"/&gt;
      &lt;property name="password" value="admin"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id='sessionFactory' class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;com.university.entities.Course&lt;/value&gt;
              &lt;value&gt;com.university.entities.Student&lt;/value&gt;
              &lt;value&gt;com.university.entities.Teacher&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id='transactionManager' class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref='sessionFactory'/&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;context:annotation-config/&gt;
  
  &lt;context:component-scan base-package="com.university.dao"&gt;&lt;/context:component-scan&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>and Here is my pom.xml :</p>
  
  <pre><code>&lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;spring.version&gt;3.2.4.RELEASE&lt;/spring.version&gt;
      &lt;hibernate.version&gt;3.6.7.Final&lt;/hibernate.version&gt;
      &lt;jsf.version&gt;2.2.4&lt;/jsf.version&gt;
      &lt;slf4j.version&gt;1.7.1&lt;/slf4j.version&gt;
      &lt;primefaces.version&gt;4.0&lt;/primefaces.version&gt;
  &lt;/properties&gt;
  
  
  &lt;dependencies&gt;
  
      &lt;!-- Tests unitaires avec junit --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.10&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Spring 3 dependencies --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- antlr --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.antlr&lt;/groupId&gt;
          &lt;artifactId&gt;antlr&lt;/artifactId&gt;
          &lt;version&gt;3.5.1&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Package entities --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;persistence-api&lt;/artifactId&gt;
          &lt;version&gt;1.0-rev-1&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Hibernate --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-commons-annotations&lt;/artifactId&gt;
          &lt;version&gt;3.2.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- JSF dependencies --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
          &lt;artifactId&gt;jsf-api&lt;/artifactId&gt;
          &lt;version&gt;${jsf.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
          &lt;artifactId&gt;jsf-impl&lt;/artifactId&gt;
          &lt;version&gt;${jsf.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;jstl&lt;/artifactId&gt;
          &lt;version&gt;1.2&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Primefaces dependency --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.primefaces&lt;/groupId&gt;
          &lt;artifactId&gt;primefaces&lt;/artifactId&gt;
          &lt;version&gt;${primefaces.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- slf4j --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;!-- commons --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-collections&lt;/groupId&gt;
          &lt;artifactId&gt;commons-collections&lt;/artifactId&gt;
          &lt;version&gt;3.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
          &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
          &lt;version&gt;1.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-fileupload&lt;/groupId&gt;
          &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;
          &lt;version&gt;1.3&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-io&lt;/groupId&gt;
          &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
          &lt;version&gt;2.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
          &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
          &lt;version&gt;3.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-logging&lt;/groupId&gt;
          &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
          &lt;version&gt;1.1.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-pool&lt;/groupId&gt;
          &lt;artifactId&gt;commons-pool&lt;/artifactId&gt;
          &lt;version&gt;1.5.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-beanutils&lt;/groupId&gt;
          &lt;artifactId&gt;commons-beanutils&lt;/artifactId&gt;
          &lt;version&gt;1.8.3&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- log4j --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;log4j&lt;/groupId&gt;
          &lt;artifactId&gt;log4j&lt;/artifactId&gt;
          &lt;version&gt;1.2.15&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- javassist --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javassist&lt;/groupId&gt;
          &lt;artifactId&gt;javassist&lt;/artifactId&gt;
          &lt;version&gt;3.12.1.GA&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- mysql --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;5.1.6&lt;/version&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.16&lt;/version&gt;
              &lt;dependencies&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.apache.maven.surefire&lt;/groupId&gt;
                      &lt;artifactId&gt;surefire-junit47&lt;/artifactId&gt;
                      &lt;version&gt;2.16&lt;/version&gt;
                  &lt;/dependency&gt;
              &lt;/dependencies&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>But when I try to test a simple DAO as :</p>
  
  <pre><code>package com.university.dao.test;
  import java.util.List;
  import junit.framework.TestCase;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import com.university.dao.StudentDAO;
  import com.university.entities.Student;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:application-context.xml" })
  
  public class StudentDAOTest extends TestCase {
  
  private static Log LOG = LogFactory.getLog(StudentDAOTest.class);
  
  @Autowired
  private StudentDAO studentDAO;
  
  @Test
  public void testFindAll() {
      List&lt;Student&gt; listStudent = studentDAO.findAll();
      LOG.debug(listStudent);
      assertNotNull(listStudent);
  }
  
  @Test
  public void testCreateOrUpdate() {
      Student newStudent = new Student("Abderrahmen ISSA", "07701607");
      studentDAO.createOrUpdate(newStudent);
      assertNotNull(newStudent.getId());
  }
  
  @Test
  public void testFindById() {
      Integer id = 2;
      Student student = studentDAO.findById(id);
      assertNotNull(student);
  }
  
  @Test
  public void testRemove() {
      Student userToRemove = new Student("User To Test Remove", "11111111");
      studentDAO.createOrUpdate(userToRemove);
      Integer id = userToRemove.getId();
  
      studentDAO.remove(userToRemove);
      Student student = studentDAO.findById(id);
      assertNull(student);
      }
  
  }
  </code></pre>
  
  <p>The Student class is :</p>
  
  <pre><code>package com.university.entities;
  // Generated 20 oct. 2013 20:53:03 by Hibernate Tools 3.4.0.CR1
  import java.util.Date;
  import java.util.HashSet;
  import java.util.Set;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import static javax.persistence.GenerationType.IDENTITY;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinTable;
  import javax.persistence.ManyToMany;
  import javax.persistence.Table;
  import javax.persistence.Temporal;
  import javax.persistence.TemporalType;
  /**
   * Student generated by hbm2java
   */
  
  @Entity
  @Table(name = "student", catalog = "university")
  public class Student implements java.io.Serializable {
  
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private Integer id;
  private String name;
  private String mail;
  private Date birthDate;
  private String birthPlace;
  private String cin;
  private Integer mobile;
  private Set&lt;Course&gt; courses = new HashSet&lt;Course&gt;(0);
  
  public Student() {
  }
  
  public Student(String mail, String cin) {
      this.mail = mail;
      this.cin = cin;
  }
  
  public Student(String name, String mail, Date birthDate, String birthPlace,
          String cin, Integer mobile, Set&lt;Course&gt; courses) {
      this.name = name;
      this.mail = mail;
      this.birthDate = birthDate;
      this.birthPlace = birthPlace;
      this.cin = cin;
      this.mobile = mobile;
      this.courses = courses;
  }
  
  @Id
  @GeneratedValue(strategy = IDENTITY)
  @Column(name = "id", unique = true, nullable = false)
  public Integer getId() {
      return this.id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  @Column(name = "name", length = 45)
  public String getName() {
      return this.name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  @Column(name = "mail", nullable = false, length = 45)
  public String getMail() {
      return this.mail;
  }
  
  public void setMail(String mail) {
      this.mail = mail;
  }
  
  @Temporal(TemporalType.DATE)
  @Column(name = "birthDate", length = 10)
  public Date getBirthDate() {
      return this.birthDate;
  }
  
  public void setBirthDate(Date birthDate) {
      this.birthDate = birthDate;
  }
  
  @Column(name = "birthPlace", length = 45)
  public String getBirthPlace() {
      return this.birthPlace;
  }
  
  public void setBirthPlace(String birthPlace) {
      this.birthPlace = birthPlace;
  }
  
  @Column(name = "cin", nullable = false, length = 45)
  public String getCin() {
      return this.cin;
  }
  
  public void setCin(String cin) {
      this.cin = cin;
  }
  
  @Column(name = "mobile")
  public Integer getMobile() {
      return this.mobile;
  }
  
  public void setMobile(Integer mobile) {
      this.mobile = mobile;
  }
  
  @ManyToMany(fetch = FetchType.LAZY)
  @JoinTable(name = "course_has_student", catalog = "university", joinColumns = { @JoinColumn(name = "Student_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "Course_id", nullable = false, updatable = false) })
  public Set&lt;Course&gt; getCourses() {
      return this.courses;
  }
  
  public void setCourses(Set&lt;Course&gt; courses) {
      this.courses = courses;
  }
  
  @Override
  public String toString() {
      return "Student [name=" + name + ", mail=" + mail + ", cin=" + cin
              + "]";
  }
  }
  </code></pre>
  
  <p>The StudentDAOImpl :</p>
  
  <pre><code>package com.university.dao.impl;
  import org.springframework.stereotype.Repository;
  import com.university.dao.StudentDAO;
  import com.university.entities.Student;
  
  @Repository("studentDAO")
  public class StudentDAOImpl extends GenericDAOImpl&lt;Student&gt; implements
      StudentDAO {
  }
  </code></pre>
  
  <p>The GenericDAOImpl :</p>
  
  <pre><code>package com.university.dao.impl;
  import java.lang.reflect.ParameterizedType;
  import java.util.List;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.transaction.annotation.Transactional;
  import com.university.dao.GenericDAO;
  
  @Transactional
  public abstract class GenericDAOImpl&lt;T&gt; implements GenericDAO&lt;T&gt; {
  
  @Autowired
  protected SessionFactory sessionFactory;
  
  /**
   * Unit.
   */
  private final Class&lt;T&gt; entityBeanType;
  
  /**
   * Instantiates a new GenericServiceImpl.
   */
  @SuppressWarnings("unchecked")
  public GenericDAOImpl() {
      this.entityBeanType = (Class&lt;T&gt;) ((ParameterizedType) getClass()
              .getGenericSuperclass()).getActualTypeArguments()[0];
  }
  
  public SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  
  public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
  }
  
  protected final Session getCurrentSession() {
      if (sessionFactory == null) {
          throw new IllegalStateException(
                  "Session Factory has not been set on DAO before usage");
      }
      return sessionFactory.getCurrentSession();
  }
  
  /**
   * Gets the entity bean type.
   * 
   * @return the entity bean type
   */
  public final Class&lt;T&gt; getEntityBeanType() {
      return entityBeanType;
  }
  
  public void createOrUpdate(T entity) {
      getCurrentSession().saveOrUpdate(entity);
  }
  
  @SuppressWarnings("unchecked")
  public List&lt;T&gt; findAll() {
      return getCurrentSession().createQuery(
              "from " + getEntityBeanType().getName()).list();
  }
  
  @SuppressWarnings("unchecked")
  public T findById(Integer id) {
      T entity = (T) getCurrentSession().get(getEntityBeanType(), id);
      return entity;
  }
  
  public void remove(T entity) {
      getCurrentSession().delete(entity);
  }
  }
  </code></pre>
  
  <p>I can run the DAOTest on eclipse well but with mvn test I get :</p>
  
  <blockquote>
    <p>org.springframework.beans.factory.BeanCreationEception : Error
    creating bean with name 'sessionFactory' defined in class path
    resource [application-context.xml] : Invocation of init method failed;
    nested exception is java.lang.NoSuchMethodError :
    javax.persistence.OneToMany.orphanRemoval&lt;>Z.</p>
  </blockquote>
  
  <p>Help please !</p>