Search in sources :

Example 1 with MetamodelImpl

use of org.hibernate.metamodel.internal.MetamodelImpl in project hibernate-orm by hibernate.

the class QueryBuilderTest method testEqualityComparisonEntityConversion.

@Test
public void testEqualityComparisonEntityConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Address address = new Address("Street Id", "Fake Street", "Fake City", "Fake State", "Fake Zip");
    Phone phone1 = new Phone("1", "555", "0001", address);
    Phone phone2 = new Phone("2", "555", "0002", address);
    Phone phone3 = new Phone("3", "555", "0003", address);
    Phone phone4 = new Phone("4", "555", "0004");
    List<Phone> phones = new ArrayList<Phone>(3);
    phones.add(phone1);
    phones.add(phone2);
    phones.add(phone3);
    address.setPhones(phones);
    em.persist(address);
    em.persist(phone4);
    em.getTransaction().commit();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    EntityType<Phone> Phone_ = mm.entity(Phone.class);
    CriteriaQuery<Phone> cquery = cb.createQuery(Phone.class);
    Root<Phone> phone = cquery.from(Phone.class);
    ComparisonPredicate predicate = (ComparisonPredicate) cb.equal(phone.get(Phone_.getSingularAttribute("address", Address.class)), address);
    cquery.where(predicate);
    List<Phone> results = em.createQuery(cquery).getResultList();
    assertEquals(3, results.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) Address(org.hibernate.jpa.test.metamodel.Address) Phone(org.hibernate.jpa.test.metamodel.Phone) ArrayList(java.util.ArrayList) ComparisonPredicate(org.hibernate.query.criteria.internal.predicate.ComparisonPredicate) Test(org.junit.Test)

Example 2 with MetamodelImpl

use of org.hibernate.metamodel.internal.MetamodelImpl in project hibernate-orm by hibernate.

the class QueryBuilderTest method testTypeConversion.

@Test
public void testTypeConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    EntityType<Product> Product_ = mm.entity(Product.class);
    // toFloat
    CriteriaQuery<Float> floatQuery = cb.createQuery(Float.class);
    Root<Product> product = floatQuery.from(Product.class);
    floatQuery.select(cb.toFloat(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
    em.createQuery(floatQuery).getResultList();
    // toDouble
    CriteriaQuery<Double> doubleQuery = cb.createQuery(Double.class);
    product = doubleQuery.from(Product.class);
    doubleQuery.select(cb.toDouble(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
    em.createQuery(doubleQuery).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test)

Example 3 with MetamodelImpl

use of org.hibernate.metamodel.internal.MetamodelImpl in project hibernate-orm by hibernate.

the class MetadataTest method testBuildingMetamodelWithParameterizedCollection.

@Test
@SuppressWarnings({ "unchecked" })
public void testBuildingMetamodelWithParameterizedCollection() {
    Metadata metadata = new MetadataSources().addAnnotatedClass(WithGenericCollection.class).buildMetadata();
    SessionFactoryImplementor sfi = (SessionFactoryImplementor) metadata.buildSessionFactory();
    MetamodelImpl metamodel = new MetamodelImpl(sfi, ((MetadataImplementor) metadata).getTypeConfiguration());
    metamodel.initialize((MetadataImplementor) metadata, JpaMetaModelPopulationSetting.IGNORE_UNSUPPORTED);
    sfi.close();
}
Also used : MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Test(org.junit.Test)

Example 4 with MetamodelImpl

use of org.hibernate.metamodel.internal.MetamodelImpl in project dhis2-core by dhis2.

the class TableNameToEntityMapping method extractTableNames.

public void extractTableNames(final Class<?> modelClazz) {
    final MetamodelImpl metamodel = (MetamodelImpl) sessionFactory.getMetamodel();
    final EntityPersister entityPersister = metamodel.entityPersister(modelClazz);
    for (int i = 0; i < entityPersister.getPropertyTypes().length; i++) {
        Type type = entityPersister.getPropertyTypes()[i];
        if (type.isCollectionType()) {
            CollectionType collectionType = (CollectionType) type;
            CollectionPersister collectionPersister = metamodel.collectionPersister(collectionType.getRole());
            if (collectionPersister instanceof BasicCollectionPersister) {
                BasicCollectionPersister bc = (BasicCollectionPersister) collectionPersister;
                String tableName = bc.getTableName();
                tableNameToEntity.computeIfAbsent(tableName, s -> new ArrayList<>()).add(new Object[] { modelClazz, collectionType.getRole() });
            }
        }
    }
    if (entityPersister instanceof SingleTableEntityPersister) {
        String tableName = ((SingleTableEntityPersister) entityPersister).getTableName();
        tableNameToEntity.computeIfAbsent(tableName, s -> new ArrayList<>()).add(new Object[] { modelClazz });
    } else {
        throw new IllegalArgumentException(modelClazz + " does not map to a single table.");
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SingleTableEntityPersister(org.hibernate.persister.entity.SingleTableEntityPersister) EntityPersister(org.hibernate.persister.entity.EntityPersister) ListIterator(java.util.ListIterator) SessionFactory(org.hibernate.SessionFactory) SingleTableEntityPersister(org.hibernate.persister.entity.SingleTableEntityPersister) CollectionType(org.hibernate.type.CollectionType) Autowired(org.springframework.beans.factory.annotation.Autowired) Profile(org.springframework.context.annotation.Profile) ArrayList(java.util.ArrayList) List(java.util.List) EntityType(javax.persistence.metamodel.EntityType) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) TreeMap(java.util.TreeMap) Map(java.util.Map) BasicCollectionPersister(org.hibernate.persister.collection.BasicCollectionPersister) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Type(org.hibernate.type.Type) CollectionType(org.hibernate.type.CollectionType) EntityType(javax.persistence.metamodel.EntityType) Type(org.hibernate.type.Type) BasicCollectionPersister(org.hibernate.persister.collection.BasicCollectionPersister) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) BasicCollectionPersister(org.hibernate.persister.collection.BasicCollectionPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionType(org.hibernate.type.CollectionType) SingleTableEntityPersister(org.hibernate.persister.entity.SingleTableEntityPersister) ArrayList(java.util.ArrayList)

Example 5 with MetamodelImpl

use of org.hibernate.metamodel.internal.MetamodelImpl in project hibernate-orm by hibernate.

the class QueryBuilderTest method testConstructor.

@Test
public void testConstructor() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    CriteriaQuery<Customer> cquery = cb.createQuery(Customer.class);
    Root<Customer> customer = cquery.from(Customer.class);
    EntityType<Customer> Customer_ = customer.getModel();
    cquery.select(cb.construct(Customer.class, customer.get(Customer_.getSingularAttribute("id", String.class)), customer.get(Customer_.getSingularAttribute("name", String.class))));
    TypedQuery<Customer> tq = em.createQuery(cquery);
    tq.getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test)

Aggregations

MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)8 Test (org.junit.Test)6 EntityManager (javax.persistence.EntityManager)5 CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)5 ArrayList (java.util.ArrayList)2 SessionFactory (org.hibernate.SessionFactory)2 Product (org.hibernate.jpa.test.metamodel.Product)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 ComparisonPredicate (org.hibernate.query.criteria.internal.predicate.ComparisonPredicate)2 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 Date (java.util.Date)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 EntityType (javax.persistence.metamodel.EntityType)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1