List of usage examples for org.hibernate Session getSessionFactory
SessionFactory getSessionFactory();
From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java
License:Apache License
public void testReadPersistenceXmlFile() throws Exception { final Session session = Session.class.cast(em.getDelegate()); final SessionFactory sf = session.getSessionFactory(); assertEquals(2, sf.getAllClassMetadata().size()); assertNotNull(sf.getClassMetadata(Employee.class)); assertNotNull(sf.getClassMetadata(Department.class)); }
From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java
License:Apache License
public void testAddMappingFile() throws Exception { final Session session = Session.class.cast(em.getDelegate()); final SessionFactory sf = session.getSessionFactory(); assertEquals(3, sf.getAllClassMetadata().size()); assertNotNull(sf.getClassMetadata(Employee.class)); assertNotNull(sf.getClassMetadata(Department.class)); assertNotNull(sf.getClassMetadata(Employee2.class)); }
From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java
License:Apache License
public void testAddPersistenceClass() throws Exception { final Session session = Session.class.cast(em.getDelegate()); final SessionFactory sf = session.getSessionFactory(); assertEquals(3, sf.getAllClassMetadata().size()); assertNotNull(sf.getClassMetadata(Employee.class)); assertNotNull(sf.getClassMetadata(Department.class)); assertNotNull(sf.getClassMetadata(Department2.class)); }
From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java
License:Apache License
public void testMappingFileAutoDetection() throws Exception { final Session session = Session.class.cast(em.getDelegate()); final SessionFactory sf = session.getSessionFactory(); assertEquals(5, sf.getAllClassMetadata().size()); assertNotNull(sf.getClassMetadata(Employee.class)); assertNotNull(sf.getClassMetadata(Department.class)); assertNotNull(sf.getClassMetadata(Employee2.class)); assertNotNull(sf.getClassMetadata(Address.class)); assertNotNull(sf.getClassMetadata(Project.class)); }
From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java
License:Apache License
public void testMappingFileAutoDetectionSubPackage() throws Exception { final PersistenceUnitManager pum = PersistenceUnitManager.class .cast(getComponent(PersistenceUnitManager.class)); final EntityManagerFactory emf = pum.getEntityManagerFactory("aaaPersistenceUnit"); final EntityManager em = emf.createEntityManager(); final Session session = Session.class.cast(em.getDelegate()); final SessionFactory sf = session.getSessionFactory(); assertEquals(3, sf.getAllClassMetadata().size()); assertNotNull(sf.getClassMetadata(Dummy2.class)); }
From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java
License:Apache License
public void testPersistenceClassAutoDetection() throws Exception { final Session session = Session.class.cast(em.getDelegate()); final SessionFactory sf = session.getSessionFactory(); assertEquals(4, sf.getAllClassMetadata().size()); assertNotNull(sf.getClassMetadata(Employee.class)); assertNotNull(sf.getClassMetadata(Department.class)); assertNotNull(sf.getClassMetadata(Customer.class)); assertNotNull(sf.getClassMetadata(Department2.class)); }
From source file:org.seasar.hibernate.jpa.PersistenceUnitConfigurationTest.java
License:Apache License
public void testPersistenceClassAutoDetectionSubPackage() throws Exception { final PersistenceUnitManager pum = PersistenceUnitManager.class .cast(getComponent(PersistenceUnitManager.class)); final EntityManagerFactory emf = pum.getEntityManagerFactory("aaaPersistenceUnit"); final EntityManager em = emf.createEntityManager(); final Session session = Session.class.cast(em.getDelegate()); final SessionFactory sf = session.getSessionFactory(); assertEquals(3, sf.getAllClassMetadata().size()); assertNotNull(sf.getClassMetadata(Dummy.class)); }
From source file:org.shept.org.springframework.orm.hibernate3.IgnoreCaseCriteriaBuilder.java
License:Apache License
public Criteria buildCriteria(String entityName, Object exampleEntity, SortDefinition sortDef, Session session) { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity).ignoreCase()); String sortPropRoot = ""; if (sortDef != null & StringUtils.hasText(sortDef.getProperty())) { // alias is needed to support sorting by associated entites properties e.g. assEntity.name // maybe we need to generate a surrogate rootProperty alias name (instead of reusing the given property name) ??? int rootIdx = sortDef.getProperty().indexOf("."); if (rootIdx > 0) { sortPropRoot = sortDef.getProperty().substring(0, rootIdx); executableCriteria.createAlias(sortPropRoot, sortPropRoot); }//from w w w. ja v a 2s . c o m executableCriteria.addOrder( sortDef.isAscending() ? Order.asc(sortDef.getProperty()) : Order.desc(sortDef.getProperty())); } ClassMetadata meta = session.getSessionFactory().getClassMetadata(exampleEntity.getClass()); // add subcriteria // List results = session.createCriteria(Parent.class) // .add( Example.create(parent).ignoreCase() ) // .createCriteria("child") // .add( Example.create( parent.getChild() ) ) // .list(); String[] propNames = meta.getPropertyNames(); for (String propName : propNames) { if (meta.getPropertyType(propName).isEntityType()) { Object propVal = meta.getPropertyValue(exampleEntity, propName, EntityMode.POJO); // Unfortunately the Hibernate criteria implementation isn't perfect, so if we have // both a sort subcriteria and a filter subcriteria then we get errors from hibernate // "org.hibernate.QueryException: duplicate association path" // workaround here is to exclude sorts from the filter [ && !propName.equals(sortPropRoot) ] // which may give unexpected results if (propVal != null && !propName.equals(sortPropRoot)) { executableCriteria.createCriteria(propName).add(Example.create(propVal).ignoreCase()); } } } return executableCriteria; }
From source file:org.springframework.orm.hibernate3.SessionFactoryBuilderSupport.java
License:Apache License
/** * Execute schema creation script, determined by the Configuration object * used for creating the SessionFactory. A replacement for Hibernate's * SchemaExport class, to be invoked on application setup. * <p>Fetch the FactoryBean itself by rather than the exposed * SessionFactory to be able to invoke this method, e.g. via * <pre class="code">//from w ww . j a v a 2s . c o m * LocalSessionFactoryBean sfb = ctx.getBean("&mySessionFactory", LocalSessionFactoryBean.class); * </pre> * or in the case of {@code @Configuration} class usage, register the * SessionFactoryBuilder as a {@code @Bean} and fetch it by type: * <pre class="code"> * SessionFactoryBuilder sfb = ctx.getBean(SessionFactoryBuilder.class); * </pre> * <p>Uses the SessionFactory that this bean generates for accessing a * JDBC connection to perform the script. * @throws DataAccessException in case of script execution errors * @see org.hibernate.cfg.Configuration#generateSchemaCreationScript * @see org.hibernate.tool.hbm2ddl.SchemaExport#create */ public void createDatabaseSchema() throws DataAccessException { logger.info("Creating database schema for Hibernate SessionFactory"); DataSource dataSource = getDataSource(); if (dataSource != null) { // Make given DataSource available for the schema update. SessionFactoryBuilderSupport.configTimeDataSourceHolder.set(dataSource); } try { HibernateTemplate hibernateTemplate = new HibernateTemplate(getSessionFactory()); hibernateTemplate.execute(new HibernateCallback<Object>() { @SuppressWarnings("deprecation") public Object doInHibernate(Session session) throws HibernateException, SQLException { Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect(); String[] sql = configuration.generateSchemaCreationScript(dialect); executeSchemaScript(session.connection(), sql); return null; } }); } finally { if (dataSource != null) { SessionFactoryBuilderSupport.configTimeDataSourceHolder.remove(); } } }
From source file:org.springframework.orm.hibernate3.SessionFactoryBuilderSupport.java
License:Apache License
/** * Execute schema update script, determined by the Configuration object * used for creating the SessionFactory. A replacement for Hibernate's * SchemaUpdate class, for automatically executing schema update scripts * on application startup. Can also be invoked manually. * <p>Fetch the FactoryBean itself by rather than the exposed * SessionFactory to be able to invoke this method, e.g. via * <pre class="code">// w w w.j a v a 2 s . com * LocalSessionFactoryBean sfb = ctx.getBean("&mySessionFactory", LocalSessionFactoryBean.class); * </pre> * or in the case of {@code @Configuration} class usage, register the * SessionFactoryBuilder as a {@code @Bean} and fetch it by type: * <pre class="code"> * SessionFactoryBuilder sfb = ctx.getBean(SessionFactoryBuilder.class); * </pre> * <p>Uses the SessionFactory that this bean generates for accessing a * JDBC connection to perform the script. * @throws DataAccessException in case of script execution errors * @see #setSchemaUpdate * @see org.hibernate.cfg.Configuration#generateSchemaUpdateScript * @see org.hibernate.tool.hbm2ddl.SchemaUpdate */ public void updateDatabaseSchema() throws DataAccessException { logger.info("Updating database schema for Hibernate SessionFactory"); if (this.dataSource != null) { // Make given DataSource available for the schema update. configTimeDataSourceHolder.set(this.dataSource); } try { HibernateTemplate hibernateTemplate = new HibernateTemplate(getSessionFactory()); hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER); hibernateTemplate.execute(new HibernateCallback<Void>() { public Void doInHibernate(Session session) throws HibernateException, SQLException { @SuppressWarnings("deprecation") Connection conn = session.connection(); Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect(); DatabaseMetadata metadata = new DatabaseMetadata(conn, dialect); String[] sql = configuration.generateSchemaUpdateScript(dialect, metadata); executeSchemaScript(conn, sql); return null; } }); } finally { if (dataSource != null) { configTimeDataSourceHolder.remove(); } } }