Search in sources :

Example 81 with Metadata

use of org.hibernate.boot.Metadata in project tutorials by eugenp.

the class HibernateSessionUtil method makeSessionFactory.

private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) {
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    metadataSources.addAnnotatedClass(User.class);
    Metadata metadata = metadataSources.buildMetadata();
    return metadata.getSessionFactoryBuilder().build();
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata)

Example 82 with Metadata

use of org.hibernate.boot.Metadata in project giftcard-demo-series by AxonIQ.

the class PrimaryJpaConfig method main.

/**
 **********************************************************************
 * Simple utility to generate an initial version of the DDL
 ***********************************************************************
 */
public static void main(String[] args) {
    Map<String, Object> settings = new HashMap<>();
    settings.put("hibernate.dialect", PostgreSQL94Dialect.class);
    settings.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class);
    settings.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class);
    StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().applySettings(settings).build();
    MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
    metadataSources.addAnnotatedClass(TokenEntry.class);
    metadataSources.addAnnotatedClass(CardSummary.class);
    Metadata metadata = metadataSources.buildMetadata();
    SchemaExport schemaExport = new SchemaExport((MetadataImplementor) metadata);
    schemaExport.setFormat(true);
    schemaExport.setDelimiter(";");
    schemaExport.create(Target.SCRIPT);
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 83 with Metadata

use of org.hibernate.boot.Metadata in project back2school by polimi-mt-acg.

the class DatabaseHandler method truncateDatabase.

public void truncateDatabase() {
    Session session = getInstance().getNewSession();
    session.beginTransaction();
    LOGGER.info("Executing truncateDatabase");
    Metadata metadata = new MetadataSources(registry).buildMetadata();
    // shutoff the foreign key checks
    session.createNativeQuery("SET FOREIGN_KEY_CHECKS = 0").executeUpdate();
    // Iterate over namespaces
    for (Namespace namespace : metadata.getDatabase().getNamespaces()) {
        if (namespace == null) {
            LOGGER.warning("Found null namespace");
            break;
        }
        for (Table table : namespace.getTables()) {
            if (table == null) {
                LOGGER.warning("Found null table");
                break;
            }
            String tableName = table.getName();
            if (!tableName.equals("hibernate_sequence")) {
                String query = String.format("TRUNCATE TABLE `%s`", tableName);
                LOGGER.info("Executing query: " + query);
                session.createNativeQuery(query).executeUpdate();
            }
        }
    }
    // turn on again the foreign key checks
    session.createNativeQuery("SET FOREIGN_KEY_CHECKS = 1").executeUpdate();
    // reset hibernate auto increment reference indexes
    session.createNativeQuery("UPDATE hibernate_sequence SET next_val = 1").executeUpdate();
    session.getTransaction().commit();
    session.close();
    LOGGER.info("Database truncated");
}
Also used : Table(org.hibernate.mapping.Table) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Namespace(org.hibernate.boot.model.relational.Namespace) Session(org.hibernate.Session)

Example 84 with Metadata

use of org.hibernate.boot.Metadata in project ignite by apache.

the class HibernateL2CacheSelfTest method startHibernate.

/**
 * Starts Hibernate.
 *
 * @param accessType Cache access type.
 * @param igniteInstanceName Ignite instance name.
 * @return Session factory.
 */
private SessionFactory startHibernate(org.hibernate.cache.spi.access.AccessType accessType, String igniteInstanceName) {
    StandardServiceRegistryBuilder builder = registryBuilder();
    for (Map.Entry<String, String> e : hibernateProperties(igniteInstanceName, accessType.name()).entrySet()) builder.applySetting(e.getKey(), e.getValue());
    // Use the same cache for Entity and Entity2.
    builder.applySetting(REGION_CACHE_PROPERTY + ENTITY2_NAME, ENTITY_NAME);
    StandardServiceRegistry srvcRegistry = builder.build();
    MetadataSources metadataSources = new MetadataSources(srvcRegistry);
    for (Class entityClass : getAnnotatedClasses()) metadataSources.addAnnotatedClass(entityClass);
    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (!entityBinding.isInherited())
            ((RootClass) entityBinding).setCacheConcurrencyStrategy(accessType.getExternalName());
    }
    for (org.hibernate.mapping.Collection collectionBinding : metadata.getCollectionBindings()) collectionBinding.setCacheConcurrencyStrategy(accessType.getExternalName());
    return metadata.buildSessionFactory();
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) Map(java.util.Map) HashMap(java.util.HashMap) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 85 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class CorrectnessTestCase method buildMetadata.

private Metadata buildMetadata(StandardServiceRegistry registry) {
    MetadataSources metadataSources = new MetadataSources(registry);
    for (Class entityClass : getAnnotatedClasses()) {
        metadataSources.addAnnotatedClass(entityClass);
    }
    Metadata metadata = metadataSources.buildMetadata();
    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
        if (!entityBinding.isInherited()) {
            ((RootClass) entityBinding).setCacheConcurrencyStrategy(accessType.getExternalName());
        }
    }
    // Collections don't have integrated version, these piggyback on parent's owner version (for DB).
    // However, this version number isn't extractable and is not passed to cache methods.
    AccessType collectionAccessType = accessType == AccessType.NONSTRICT_READ_WRITE ? AccessType.READ_WRITE : accessType;
    for (Collection collectionBinding : metadata.getCollectionBindings()) {
        collectionBinding.setCacheConcurrencyStrategy(collectionAccessType.getExternalName());
    }
    return metadata;
}
Also used : RootClass(org.hibernate.mapping.RootClass) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Collection(org.hibernate.mapping.Collection) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) AccessType(org.hibernate.cache.spi.access.AccessType) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

Metadata (org.hibernate.boot.Metadata)148 MetadataSources (org.hibernate.boot.MetadataSources)124 Test (org.junit.Test)103 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)73 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)56 PersistentClass (org.hibernate.mapping.PersistentClass)46 TestForIssue (org.hibernate.testing.TestForIssue)31 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)18 Session (org.hibernate.Session)14 HashMap (java.util.HashMap)13 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)13 Property (org.hibernate.mapping.Property)12 RootClass (org.hibernate.mapping.RootClass)12 Map (java.util.Map)11 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 SessionFactory (org.hibernate.SessionFactory)10 Collection (org.hibernate.mapping.Collection)10 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)10 ServiceRegistry (org.hibernate.service.ServiceRegistry)8 Before (org.junit.Before)8