Hibernate / JPA - EntityNotFoundException: Unable to find with id
<p>I have a scenario where two entities have @ManyToOne relation.The app uses both Spring data jpa and Hibernate. I am trying to save the child entity through Spring data jpa repo's save() method and the other through hibernate's getSession().save(). Its ending up with entity not found exception. After the first save, its not inserting the record in DB because of which the later save is failing. Looking for a solution to make it work!</p>

<p>SampleCode:</p>

<pre><code>EntityA a  = new EntityA();
entityADao.save(); //Not inserting the record

EntityB b = new EntityB();
b.setEntityA(a);
b.save(); //Throwing entityA not found exception.
</code></pre>

<p>Test Class:</p>

<pre><code>@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-application-context-dao.xml" })
@Transactional(propagation= Propagation.REQUIRES_NEW)
public class EntityTest{

    @Autowired
    private EntityADao entityADao;

    private EntityBDao entityBDao;


    @Autowired
    private SessionFactory sessionFactory;

    @PostConstruct
    public void orderDaoTest()
    {
        subject = new EntityBDaoImpl (sessionFactory);
        System.setProperty("SERVER_TYPE", "junit"); // need to do this
    }

    @Test
    public void testListByEntityB()
    {

        EntityA a  = new EntityA();
        a.setId("123");
        entityADao.save(); //Not inserting the record

        EntityB b = new EntityB();
        b.setEntityA(a);
        b.save(); //Throwing entityA not found exception with id "123". 

        List&lt;EntityB&gt; bList = entityBDao.listById(a.getId());

    }


}
</code></pre>

<p>Thanks,
Preethi</p>