Search in sources :

Example 31 with ManyToOneType

use of org.hibernate.type.ManyToOneType in project jbosstools-hibernate by jbosstools.

the class TypeFacadeTest method testIsOneToOne.

@Test
public void testIsOneToOne() {
    IType typeFacade = null;
    ClassType classType = new ClassType();
    typeFacade = FACADE_FACTORY.createType(classType);
    Assert.assertFalse(typeFacade.isOneToOne());
    EntityType entityType = new ManyToOneType(null, null);
    typeFacade = FACADE_FACTORY.createType(entityType);
    Assert.assertFalse(entityType.isOneToOne());
    OneToOneType oneToOneType = new OneToOneType(null, null, null, false, null, false, false, null, null);
    typeFacade = FACADE_FACTORY.createType(oneToOneType);
    Assert.assertTrue(oneToOneType.isOneToOne());
}
Also used : EntityType(org.hibernate.type.EntityType) ManyToOneType(org.hibernate.type.ManyToOneType) ClassType(org.hibernate.type.ClassType) IType(org.jboss.tools.hibernate.runtime.spi.IType) OneToOneType(org.hibernate.type.OneToOneType) Test(org.junit.Test)

Example 32 with ManyToOneType

use of org.hibernate.type.ManyToOneType in project jbosstools-hibernate by jbosstools.

the class TypeFacadeTest method testIsEntityType.

@Test
public void testIsEntityType() {
    IType typeFacade = null;
    ClassType classType = new ClassType();
    typeFacade = FACADE_FACTORY.createType(classType);
    Assert.assertFalse(typeFacade.isEntityType());
    EntityType entityType = new ManyToOneType(null, null);
    typeFacade = FACADE_FACTORY.createType(entityType);
    Assert.assertTrue(entityType.isEntityType());
}
Also used : EntityType(org.hibernate.type.EntityType) ManyToOneType(org.hibernate.type.ManyToOneType) ClassType(org.hibernate.type.ClassType) IType(org.jboss.tools.hibernate.runtime.spi.IType) Test(org.junit.Test)

Example 33 with ManyToOneType

use of org.hibernate.type.ManyToOneType in project hibernate-orm by hibernate.

the class AbstractPropertyMapping method getCommonType.

private Type getCommonType(MetadataImplementor metadata, EntityType entityType1, EntityType entityType2) {
    PersistentClass thisClass = metadata.getEntityBinding(entityType1.getAssociatedEntityName());
    PersistentClass otherClass = metadata.getEntityBinding(entityType2.getAssociatedEntityName());
    PersistentClass commonClass = getCommonPersistentClass(thisClass, otherClass);
    if (commonClass == null) {
        return null;
    }
    // Create a copy of the type but with the common class
    if (entityType1 instanceof ManyToOneType) {
        ManyToOneType t = (ManyToOneType) entityType1;
        return new ManyToOneType(t, commonClass.getEntityName());
    } else if (entityType1 instanceof SpecialOneToOneType) {
        SpecialOneToOneType t = (SpecialOneToOneType) entityType1;
        return new SpecialOneToOneType(t, commonClass.getEntityName());
    } else if (entityType1 instanceof OneToOneType) {
        OneToOneType t = (OneToOneType) entityType1;
        return new OneToOneType(t, commonClass.getEntityName());
    } else {
        throw new IllegalStateException("Unexpected entity type: " + entityType1);
    }
}
Also used : SpecialOneToOneType(org.hibernate.type.SpecialOneToOneType) ManyToOneType(org.hibernate.type.ManyToOneType) PersistentClass(org.hibernate.mapping.PersistentClass) OneToOneType(org.hibernate.type.OneToOneType) SpecialOneToOneType(org.hibernate.type.SpecialOneToOneType)

Example 34 with ManyToOneType

use of org.hibernate.type.ManyToOneType in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testSelectClauseImplicitJoinOrderByJoinedProperty.

@Test
@TestForIssue(jiraKey = "HHH-9305")
@SuppressWarnings({ "unchecked" })
public void testSelectClauseImplicitJoinOrderByJoinedProperty() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Zoo zoo = new Zoo();
    zoo.setName("The Zoo");
    zoo.setMammals(new HashMap());
    zoo.setAnimals(new HashMap());
    Mammal plat = new Mammal();
    plat.setBodyWeight(11f);
    plat.setDescription("Platypus");
    plat.setZoo(zoo);
    plat.setSerialNumber("plat123");
    zoo.getMammals().put("Platypus", plat);
    zoo.getAnimals().put("plat123", plat);
    Zoo otherZoo = new Zoo();
    otherZoo.setName("The Other Zoo");
    otherZoo.setMammals(new HashMap());
    otherZoo.setAnimals(new HashMap());
    Mammal zebra = new Mammal();
    zebra.setBodyWeight(110f);
    zebra.setDescription("Zebra");
    zebra.setZoo(otherZoo);
    zebra.setSerialNumber("zebra123");
    otherZoo.getMammals().put("Zebra", zebra);
    otherZoo.getAnimals().put("zebra123", zebra);
    Mammal elephant = new Mammal();
    elephant.setBodyWeight(550f);
    elephant.setDescription("Elephant");
    elephant.setZoo(otherZoo);
    elephant.setSerialNumber("elephant123");
    otherZoo.getMammals().put("Elephant", elephant);
    otherZoo.getAnimals().put("elephant123", elephant);
    s.persist(plat);
    s.persist(zoo);
    s.persist(zebra);
    s.persist(elephant);
    s.persist(otherZoo);
    s.flush();
    s.clear();
    Query q = s.createQuery("select a.zoo from Animal a where a.zoo is not null order by a.zoo.name");
    Type type = q.getReturnTypes()[0];
    assertTrue(type instanceof ManyToOneType);
    assertEquals(((ManyToOneType) type).getAssociatedEntityName(), "org.hibernate.test.hql.Zoo");
    List<Zoo> zoos = (List<Zoo>) q.list();
    assertEquals(3, zoos.size());
    assertEquals(otherZoo.getName(), zoos.get(0).getName());
    assertEquals(2, zoos.get(0).getMammals().size());
    assertEquals(2, zoos.get(0).getAnimals().size());
    assertSame(zoos.get(0), zoos.get(1));
    assertEquals(zoo.getName(), zoos.get(2).getName());
    assertEquals(1, zoos.get(2).getMammals().size());
    assertEquals(1, zoos.get(2).getAnimals().size());
    s.clear();
    s.delete(plat);
    s.delete(zebra);
    s.delete(elephant);
    s.delete(zoo);
    s.delete(otherZoo);
    t.commit();
    s.close();
}
Also used : DiscriminatorType(org.hibernate.persister.entity.DiscriminatorType) ManyToOneType(org.hibernate.type.ManyToOneType) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) Transaction(org.hibernate.Transaction) TransactionUtil2.inTransaction(org.hibernate.testing.transaction.TransactionUtil2.inTransaction) Query(org.hibernate.Query) HashMap(java.util.HashMap) ManyToOneType(org.hibernate.type.ManyToOneType) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 35 with ManyToOneType

use of org.hibernate.type.ManyToOneType in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testSelectClauseImplicitJoin.

@Test
@SuppressWarnings({ "unchecked" })
public void testSelectClauseImplicitJoin() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Zoo zoo = new Zoo();
    zoo.setName("The Zoo");
    zoo.setMammals(new HashMap());
    zoo.setAnimals(new HashMap());
    Mammal plat = new Mammal();
    plat.setBodyWeight(11f);
    plat.setDescription("Platypus");
    plat.setZoo(zoo);
    plat.setSerialNumber("plat123");
    zoo.getMammals().put("Platypus", plat);
    zoo.getAnimals().put("plat123", plat);
    s.persist(plat);
    s.persist(zoo);
    s.flush();
    s.clear();
    Query q = s.createQuery("select distinct a.zoo from Animal a where a.zoo is not null");
    Type type = q.getReturnTypes()[0];
    assertTrue(type instanceof ManyToOneType);
    assertEquals(((ManyToOneType) type).getAssociatedEntityName(), "org.hibernate.test.hql.Zoo");
    zoo = (Zoo) q.list().get(0);
    assertEquals(zoo.getMammals().size(), 1);
    assertEquals(zoo.getAnimals().size(), 1);
    s.clear();
    s.delete(plat);
    s.delete(zoo);
    t.commit();
    s.close();
}
Also used : DiscriminatorType(org.hibernate.persister.entity.DiscriminatorType) ManyToOneType(org.hibernate.type.ManyToOneType) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) Transaction(org.hibernate.Transaction) TransactionUtil2.inTransaction(org.hibernate.testing.transaction.TransactionUtil2.inTransaction) Query(org.hibernate.Query) HashMap(java.util.HashMap) ManyToOneType(org.hibernate.type.ManyToOneType) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ManyToOneType (org.hibernate.type.ManyToOneType)35 Test (org.junit.Test)28 ClassType (org.hibernate.type.ClassType)24 EntityType (org.hibernate.type.EntityType)24 IType (org.jboss.tools.hibernate.runtime.spi.IType)24 OneToOneType (org.hibernate.type.OneToOneType)11 Type (org.hibernate.type.Type)9 ComponentType (org.hibernate.type.ComponentType)7 HashMap (java.util.HashMap)4 Query (org.hibernate.Query)4 Session (org.hibernate.Session)4 Transaction (org.hibernate.Transaction)4 DiscriminatorType (org.hibernate.persister.entity.DiscriminatorType)4 TransactionUtil2.inTransaction (org.hibernate.testing.transaction.TransactionUtil2.inTransaction)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 CollectionType (org.hibernate.type.CollectionType)2 SetType (org.hibernate.type.SetType)2 Element (org.dom4j.Element)1