I'm trying to persist two different entities using JPA1, with the Hibernate implementation. The code for this is as shown below:
Parent entity class
@Entity
@Table(name = "parent")
public class Parent implements Serializable {
{...}
private Child child;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "child_id", nullable = "false")
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
Child entity class
@Entity
@Table(name = "child")
public class Child implements Serializable {
private Integer id;
@Id
@Column(name = "child_id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
Test case
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:META-INF/application.xml")
@Transactional
public class ParentTest extends TestCase {
@PersistenceContext
private EntityManager entityManager;
@Test
public void testSave() {
Child child = new Child();
child.setId(1);
Parent parent = new Parent();
parent.setChild(child);
entityManager.persist(parent.getChild());
entityManager.persist(parent); // throws the exception
}
}
Entity manager and transaction on application.xml
<tx:annotation-driven transaction-manager="transactionManager" />
<jee:jndi-lookup id="dataSource" jndi-name="java:/jdbc/myds" expected-type="javax.sql.DataSource" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.mypackage" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter"›
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect>org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
When trying to insert the parent object, hibernate throws a PropertyValueException, saying child is null or transient, even though child was created and persisted before this operation. The odd thing is this only fails on the unit test, and in the real application, with a pre-inserted child, this works just as expected.
PS: I'm pretty aware I could map child with cascade persist, but that's not the idea here. I just want to check if these two work independently.
The problem here is you are persisting the parent table with the values set . When it goes to persist it needs the child table id which has to be persisted already as it was a foreign key and hence it was a not null property references a null value.
@Test
public void testSave() {
Child child = new Child();
child.setId(1);
entityManager.persist(child);
Parent parent = new Parent();
parent.setChild(child);
entityManager.persist(parent);
}
Try this to save the child first and then parent .Else changing the mapping