Search in sources :

Example 1 with Address

use of org.hibernate.test.onetoone.formula.Address in project hibernate-orm by hibernate.

the class NonEncapsulatedCompositeIdResultSetProcessorTest method testCompositeIdWithKeyManyToOne.

@Test
public void testCompositeIdWithKeyManyToOne() throws Exception {
    final String personId = "John Doe";
    Person p = new Person();
    p.setName(personId);
    final Address a = new Address();
    a.setPerson(p);
    p.setAddress(a);
    a.setType("HOME");
    a.setStreet("Main St");
    a.setState("Sweet Home Alabama");
    a.setZip("3181");
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(p);
    t.commit();
    s.close();
    final EntityPersister personPersister = sessionFactory().getEntityPersister(Person.class.getName());
    final EntityPersister addressPersister = sessionFactory().getEntityPersister(Address.class.getName());
    {
        final List results = getResults(addressPersister, new Callback() {

            @Override
            public void bind(PreparedStatement ps) throws SQLException {
                ps.setString(1, personId);
                ps.setString(2, "HOME");
            }

            @Override
            public QueryParameters getQueryParameters() {
                QueryParameters qp = new QueryParameters();
                qp.setPositionalParameterTypes(new Type[] { addressPersister.getIdentifierType() });
                qp.setPositionalParameterValues(new Object[] { a });
                qp.setOptionalObject(a);
                qp.setOptionalEntityName(addressPersister.getEntityName());
                qp.setOptionalId(a);
                qp.setLockOptions(LockOptions.NONE);
                return qp;
            }
        });
        assertEquals(1, results.size());
        Object result = results.get(0);
        assertNotNull(result);
    }
    // test loading the Person (the entity with normal id def, but mixed composite fk to Address)
    {
        final List results = getResults(personPersister, new Callback() {

            @Override
            public void bind(PreparedStatement ps) throws SQLException {
                ps.setString(1, personId);
            }

            @Override
            public QueryParameters getQueryParameters() {
                QueryParameters qp = new QueryParameters();
                qp.setPositionalParameterTypes(new Type[] { personPersister.getIdentifierType() });
                qp.setPositionalParameterValues(new Object[] { personId });
                qp.setOptionalObject(null);
                qp.setOptionalEntityName(personPersister.getEntityName());
                qp.setOptionalId(personId);
                qp.setLockOptions(LockOptions.NONE);
                return qp;
            }
        });
        assertEquals(1, results.size());
        Object result = results.get(0);
        assertNotNull(result);
    }
    //		CardField cardFieldWork = ExtraAssertions.assertTyping( CardField.class, result );
    //		assertEquals( cardFieldGotten, cardFieldWork );
    // clean up test data
    s = openSession();
    s.beginTransaction();
    s.createQuery("delete Address").executeUpdate();
    s.createQuery("delete Person").executeUpdate();
    s.getTransaction().commit();
    s.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Address(org.hibernate.test.onetoone.formula.Address) Transaction(org.hibernate.Transaction) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) QueryParameters(org.hibernate.engine.spi.QueryParameters) Person(org.hibernate.test.onetoone.formula.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with Address

use of org.hibernate.test.onetoone.formula.Address in project hibernate-orm by hibernate.

the class KeyManyToOneWalkingTest method testWalkingKeyManyToOneGraphs.

@Test
public void testWalkingKeyManyToOneGraphs() {
    // Address has a composite id with a bi-directional key-many to Person
    final EntityPersister ep = (EntityPersister) sessionFactory().getClassMetadata(Address.class);
    MetamodelGraphWalker.visitEntity(new LoggingAssociationVisitationStrategy(), ep);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Address(org.hibernate.test.onetoone.formula.Address) Test(org.junit.Test)

Aggregations

EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Address (org.hibernate.test.onetoone.formula.Address)2 Test (org.junit.Test)2 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 QueryParameters (org.hibernate.engine.spi.QueryParameters)1 Person (org.hibernate.test.onetoone.formula.Person)1