Package org.hibernate.examples.mapping.associations.onetoone.bidirectionalManyToOne

Examples of org.hibernate.examples.mapping.associations.onetoone.bidirectionalManyToOne.Husband


    }

    @Test
    public void bidirectionalManyToOne() throws Exception {

        Husband husband = new Husband();
        husband.setName("Alex");

        Wife wife = new Wife();
        wife.setName("Bea");

        husband.setWife(wife);
        wife.setHusband(husband);
        em.persist(husband);
        em.persist(wife);
        em.flush();
        em.clear();

        husband = em.find(Husband.class, husband.getId());
        assertThat(husband).isNotNull();
        assertThat(husband.getWife()).isNotNull();
        em.clear();

        wife = em.find(Wife.class, wife.getId());
        assertThat(wife).isNotNull();

        husband = wife.getHusband();
        assertThat(husband).isNotNull();

        Wife bea2 = new Wife();
        em.persist(bea2);
        bea2.setName("Still Bea");

        husband.setWife(bea2);
        wife.setHusband(null);
        bea2.setHusband(husband);

        em.persist(husband);
        em.persist(bea2);
        em.flush();
        em.clear();


        husband = em.find(Husband.class, husband.getId());
        assertThat(husband).isNotNull();
        assertThat(husband.getWife()).isNotNull();
        assertThat(husband.getWife().getHusband()).isEqualTo(husband);

        em.clear();

        wife = em.find(Wife.class, wife.getId());
        assertThat(wife).isNotNull();
        assertThat(wife.getHusband()).isNull();
        em.remove(wife);

        bea2 = em.find(Wife.class, bea2.getId());
        assertThat(bea2).isNotNull();

        husband = bea2.getHusband();
        assertThat(husband).isNotNull();

        bea2.setHusband(null);
        husband.setWife(null);
        em.remove(husband);
        em.remove(wife);
        em.flush();
    }
View Full Code Here

TOP

Related Classes of org.hibernate.examples.mapping.associations.onetoone.bidirectionalManyToOne.Husband

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.