Search in sources :

Example 1 with Parent

use of com.vladmihalcea.hibernate.model.bag.Parent in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateBagDuplicateTest method test.

@Test
public void test() {
    final Long parentId = cleanAndSaveParent();
    Parent parent = transactionTemplate.execute(new TransactionCallback<Parent>() {

        @Override
        public Parent doInTransaction(TransactionStatus transactionStatus) {
            Parent parent = loadParent(parentId);
            Child child1 = new Child();
            child1.setName("child1");
            Child child2 = new Child();
            child2.setName("child2");
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            entityManager.flush();
            if (parent.getChildren().size() == 4) {
                LOG.error("Duplicates rows generated!");
            }
            return parent;
        }
    });
    //assertEquals(2, parent.getChildren().size());
    if (parent.getChildren().size() == 4) {
        LOG.error("Duplicates rows generated!");
    }
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            assertEquals(4, loadParent(parentId).getChildren().size());
            return null;
        }
    });
}
Also used : Parent(com.vladmihalcea.hibernate.model.bag.Parent) TransactionStatus(org.springframework.transaction.TransactionStatus) Child(com.vladmihalcea.hibernate.model.bag.Child) Test(org.junit.Test)

Example 2 with Parent

use of com.vladmihalcea.hibernate.model.bag.Parent in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateBagDuplicateTest method testFixByPersistingChild.

@Test
public void testFixByPersistingChild() {
    final Long parentId = cleanAndSaveParent();
    Parent parent = transactionTemplate.execute(new TransactionCallback<Parent>() {

        @Override
        public Parent doInTransaction(TransactionStatus transactionStatus) {
            Parent parent = loadParent(parentId);
            Child child1 = new Child();
            child1.setName("child1");
            Child child2 = new Child();
            child2.setName("child2");
            entityManager.persist(child1);
            entityManager.persist(child2);
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            parent.getChildren().size();
            return parent;
        }
    });
    assertEquals(2, parent.getChildren().size());
}
Also used : Parent(com.vladmihalcea.hibernate.model.bag.Parent) TransactionStatus(org.springframework.transaction.TransactionStatus) Child(com.vladmihalcea.hibernate.model.bag.Child) Test(org.junit.Test)

Aggregations

Child (com.vladmihalcea.hibernate.model.bag.Child)2 Parent (com.vladmihalcea.hibernate.model.bag.Parent)2 Test (org.junit.Test)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2