Hibernate / JPA - EntityNotFoundException:无法找到id

时间:2016-03-07 06:00:00

标签: hibernate jpa

我有一个场景,其中两个实体具有@ManyToOne关系。该应用程序使用Spring数据jpa和Hibernate。我试图通过Spring数据jpa repo的save()方法保存子实体,另一个通过hibernate的getSession()。save()保存。它最终与实体未找到异常。在第一次保存之后,它没有在DB中插入记录,因为后来的保存失败了。寻找一个解决方案,让它工作!

SampleCode:

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

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

测试类:

@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<EntityB> bList = entityBDao.listById(a.getId());

    }


}

谢谢, Preethi

0 个答案:

没有答案