Search in sources :

Example 86 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project dhis2-core by dhis2.

the class AbstractPropertyIntrospectorService method getPropertiesFromHibernate.

protected Map<String, Property> getPropertiesFromHibernate(Class<?> klass) {
    updateJoinTables();
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(klass);
    // is class persisted with hibernate
    if (classMetadata == null) {
        return new HashMap<>();
    }
    Map<String, Property> properties = new HashMap<>();
    SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
    MetadataImplementor metadataImplementor = HibernateMetadata.getMetadataImplementor();
    if (metadataImplementor == null) {
        return new HashMap<>();
    }
    PersistentClass persistentClass = metadataImplementor.getEntityBinding(klass.getName());
    Iterator<?> propertyIterator = persistentClass.getPropertyClosureIterator();
    while (propertyIterator.hasNext()) {
        Property property = new Property(klass);
        property.setRequired(false);
        property.setPersisted(true);
        property.setOwner(true);
        org.hibernate.mapping.Property hibernateProperty = (org.hibernate.mapping.Property) propertyIterator.next();
        Type type = hibernateProperty.getType();
        property.setName(hibernateProperty.getName());
        property.setCascade(hibernateProperty.getCascade());
        property.setCollection(type.isCollectionType());
        property.setSetterMethod(hibernateProperty.getSetter(klass).getMethod());
        property.setGetterMethod(hibernateProperty.getGetter(klass).getMethod());
        if (property.isCollection()) {
            CollectionType collectionType = (CollectionType) type;
            CollectionPersister persister = sessionFactoryImplementor.getCollectionPersister(collectionType.getRole());
            property.setOwner(!persister.isInverse());
            property.setManyToMany(persister.isManyToMany());
            property.setMin(0d);
            property.setMax(Double.MAX_VALUE);
            if (property.isOwner()) {
                property.setOwningRole(collectionType.getRole());
                property.setInverseRole(roleToRole.get(collectionType.getRole()));
            } else {
                property.setOwningRole(roleToRole.get(collectionType.getRole()));
                property.setInverseRole(collectionType.getRole());
            }
        }
        if (SingleColumnType.class.isInstance(type) || CustomType.class.isInstance(type) || ManyToOneType.class.isInstance(type)) {
            Column column = (Column) hibernateProperty.getColumnIterator().next();
            property.setUnique(column.isUnique());
            property.setRequired(!column.isNullable());
            property.setMin(0d);
            property.setMax((double) column.getLength());
            property.setLength(column.getLength());
            if (TextType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax((double) Integer.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            } else if (IntegerType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax((double) Integer.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            } else if (LongType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax((double) Long.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            } else if (DoubleType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax(Double.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            }
        }
        if (ManyToOneType.class.isInstance(type)) {
            property.setManyToOne(true);
            property.setRequired(property.isRequired() && !property.isCollection());
            if (property.isOwner()) {
                property.setOwningRole(klass.getName() + "." + property.getName());
            } else {
                property.setInverseRole(klass.getName() + "." + property.getName());
            }
        } else if (OneToOneType.class.isInstance(type)) {
            property.setOneToOne(true);
        }
        properties.put(property.getName(), property);
    }
    return properties;
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) HashMap(java.util.HashMap) ManyToOneType(org.hibernate.type.ManyToOneType) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IntegerType(org.hibernate.type.IntegerType) CustomType(org.hibernate.type.CustomType) CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) IntegerType(org.hibernate.type.IntegerType) TextType(org.hibernate.type.TextType) LongType(org.hibernate.type.LongType) SingleColumnType(org.hibernate.type.SingleColumnType) OneToOneType(org.hibernate.type.OneToOneType) SetType(org.hibernate.type.SetType) DoubleType(org.hibernate.type.DoubleType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Column(org.hibernate.mapping.Column) DoubleType(org.hibernate.type.DoubleType) CollectionType(org.hibernate.type.CollectionType) PersistentClass(org.hibernate.mapping.PersistentClass) OneToOneType(org.hibernate.type.OneToOneType)

Example 87 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project jbosstools-hibernate by jbosstools.

the class ClassMetadataFacadeTest method createSampleEntityPersister.

private TestEntityPersister createSampleEntityPersister() {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    builder.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    ServiceRegistry serviceRegistry = builder.build();
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory();
    RootClass rc = new RootClass(null);
    Table t = new Table("foobar");
    rc.setTable(t);
    Column c = new Column("foo");
    t.addColumn(c);
    ArrayList<Column> keyList = new ArrayList<>();
    keyList.add(c);
    t.createUniqueKey(keyList);
    SimpleValue sv = new SimpleValue(metadata);
    sv.setNullValue("null");
    sv.setTypeName(Integer.class.getName());
    sv.addColumn(c);
    rc.setEntityName("foobar");
    rc.setIdentifier(sv);
    return new TestEntityPersister(rc, sfi, metadata);
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) ArrayList(java.util.ArrayList) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 88 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project jbosstools-hibernate by jbosstools.

the class Cfg2HbmToolFacadeTest method testGetPropertyTag.

public void testGetPropertyTag() throws Exception {
    RootClass rc = new RootClass(null);
    Property p = new Property();
    MetadataImplementor m = (MetadataImplementor) Proxy.newProxyInstance(FACADE_FACTORY.getClassLoader(), new Class[] { MetadataImplementor.class }, new TestInvocationHandler());
    SimpleValue sv = new SimpleValue(m);
    sv.setTypeName("foobar");
    p.setValue(sv);
    p.setPersistentClass(rc);
    rc.setVersion(p);
    IProperty property = new AbstractPropertyFacade(FACADE_FACTORY, p) {
    };
    Assert.assertEquals("version", cfg2HbmToolFacade.getTag(property));
}
Also used : RootClass(org.hibernate.mapping.RootClass) AbstractPropertyFacade(org.jboss.tools.hibernate.runtime.common.AbstractPropertyFacade) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) PersistentClass(org.hibernate.mapping.PersistentClass) Property(org.hibernate.mapping.Property) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 89 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project jbosstools-hibernate by jbosstools.

the class ClassMetadataFacadeTest method createSampleEntityPersister.

private TestEntityPersister createSampleEntityPersister() {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    builder.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    ServiceRegistry serviceRegistry = builder.build();
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory();
    RootClass rc = new RootClass(null);
    Table t = new Table("foobar");
    rc.setTable(t);
    Column c = new Column("foo");
    t.addColumn(c);
    ArrayList<Column> keyList = new ArrayList<>();
    keyList.add(c);
    t.createUniqueKey(keyList);
    SimpleValue sv = new SimpleValue(metadata);
    sv.setNullValue("null");
    sv.setTypeName(Integer.class.getName());
    sv.addColumn(c);
    rc.setEntityName("foobar");
    rc.setIdentifier(sv);
    return new TestEntityPersister(rc, sfi, metadata);
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) ArrayList(java.util.ArrayList) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 90 with MetadataImplementor

use of org.hibernate.boot.spi.MetadataImplementor in project jbosstools-hibernate by jbosstools.

the class Cfg2HbmToolFacadeTest method testGetPropertyTag.

public void testGetPropertyTag() throws Exception {
    RootClass rc = new RootClass(null);
    Property p = new Property();
    MetadataImplementor m = (MetadataImplementor) Proxy.newProxyInstance(FACADE_FACTORY.getClassLoader(), new Class[] { MetadataImplementor.class }, new TestInvocationHandler());
    SimpleValue sv = new SimpleValue(m);
    sv.setTypeName("foobar");
    p.setValue(sv);
    p.setPersistentClass(rc);
    rc.setVersion(p);
    IProperty property = new AbstractPropertyFacade(FACADE_FACTORY, p) {
    };
    Assert.assertEquals("version", cfg2HbmToolFacade.getTag(property));
}
Also used : RootClass(org.hibernate.mapping.RootClass) AbstractPropertyFacade(org.jboss.tools.hibernate.runtime.common.AbstractPropertyFacade) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) PersistentClass(org.hibernate.mapping.PersistentClass) Property(org.hibernate.mapping.Property) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) SimpleValue(org.hibernate.mapping.SimpleValue)

Aggregations

MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)113 MetadataSources (org.hibernate.boot.MetadataSources)80 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)62 Test (org.junit.Test)56 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)52 SimpleValue (org.hibernate.mapping.SimpleValue)32 RootClass (org.hibernate.mapping.RootClass)31 PersistentClass (org.hibernate.mapping.PersistentClass)27 TestForIssue (org.hibernate.testing.TestForIssue)26 Table (org.hibernate.mapping.Table)23 Test (org.junit.jupiter.api.Test)23 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)22 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)20 Column (org.hibernate.mapping.Column)17 ArrayList (java.util.ArrayList)15 ServiceRegistry (org.hibernate.service.ServiceRegistry)15 File (java.io.File)14 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)14 Configuration (org.hibernate.cfg.Configuration)12 Property (org.hibernate.mapping.Property)11