/** * Use the mappings and properties specified in an application resource named <tt>hibernate.cfg.xml</tt>. * * @return this for method chaining * * @throws HibernateException Generally indicates we cannot find <tt>hibernate.cfg.xml</tt> * * @see #configure(String) */ public Configuration configure() throws HibernateException { return configure( StandardServiceRegistryBuilder.DEFAULT_CFG_RESOURCE_NAME ); }
@TransactionAttribute(TransactionAttributeType.NEVER) public void start() { try { Configuration configuration = new Configuration(); configuration = configuration.configure( "hibernate.cfg.xml" ); configuration.addAnnotatedClass( WildFlyDdlEntity.class ); sessionFactory = configuration.buildSessionFactory(); } catch (Throwable ex) { System.err.println( "Initial SessionFactory creation failed." + ex ); throw new ExceptionInInitializerError( ex ); } }
@Test public void testDeclarativeMix() throws Exception { Configuration cfg = new Configuration(); cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); SessionFactory sf = cfg.buildSessionFactory(); assertNotNull( sf ); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); Query q = s.createQuery( "from Boat" ); assertEquals( 0, q.list().size() ); q = s.createQuery( "from Plane" ); assertEquals( 0, q.list().size() ); tx.commit(); s.close(); sf.close(); } @Test
public void start() { Configuration configuration = new Configuration(); configuration = configuration.configure( "hibernate.cfg.xml" ); configuration.addAnnotatedClass( WildFlyDdlEntity.class ); // creating the SF should run schema creation sessionFactory = configuration.buildSessionFactory(); }
@Test public void testHbmWithSubclassExtends() throws Exception { Configuration cfg = new Configuration(); cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); cfg.addClass( Ferry.class ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); SessionFactory sf = cfg.buildSessionFactory(); assertNotNull( sf ); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); Query q = s.createQuery( "from Ferry" ); assertEquals( 0, q.list().size() ); q = s.createQuery( "from Plane" ); assertEquals( 0, q.list().size() ); tx.commit(); s.close(); sf.close(); } @Test
@Test public void testAnnReferencesHbm() throws Exception { Configuration cfg = new Configuration(); cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); cfg.addAnnotatedClass( Port.class ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); SessionFactory sf = cfg.buildSessionFactory(); assertNotNull( sf ); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); Query q = s.createQuery( "from Boat" ); assertEquals( 0, q.list().size() ); q = s.createQuery( "from Port" ); assertEquals( 0, q.list().size() ); tx.commit(); s.close(); sf.close(); } }
@BeforeClassOnce public void init() throws URISyntaxException { config = new Configuration(); URL url = Thread.currentThread().getContextClassLoader().getResource( getHibernateConfigurationFileName() ); config.configure( new File( url.toURI() ) ); String auditStrategy = getAuditStrategy(); if ( auditStrategy != null && !"".equals( auditStrategy ) ) { config.setProperty( EnversSettings.AUDIT_STRATEGY, auditStrategy ); } config.setProperty( Environment.USE_NEW_ID_GENERATOR_MAPPINGS, "true" ); config.setProperty( EnversSettings.USE_REVISION_ENTITY_WITH_NATIVE_ID, "false" ); addProperties( config ); this.initMappings(); serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( config.getProperties() ); sessionFactory = config.buildSessionFactory( serviceRegistry ); }
@Override public AnnotationConfiguration configure(String resource) throws HibernateException { super.configure( resource ); return this; }
@Override public AnnotationConfiguration configure(URL url) throws HibernateException { super.configure( url ); return this; }
@Override public AnnotationConfiguration configure(File configFile) throws HibernateException { super.configure( configFile ); return this; }
@Override public AnnotationConfiguration configure(org.w3c.dom.Document document) throws HibernateException { super.configure( document ); return this; }
@Override public AnnotationConfiguration configure() throws HibernateException { super.configure(); return this; }
@Test public void testPrecedenceHbm() throws Exception { Configuration cfg = new Configuration(); cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); cfg.addAnnotatedClass( Boat.class ); SessionFactory sf = cfg.buildSessionFactory(); assertNotNull( sf ); Session s = sf.openSession(); s.getTransaction().begin(); Boat boat = new Boat(); boat.setSize( 12 ); boat.setWeight( 34 ); s.persist( boat ); s.getTransaction().commit(); s.clear(); Transaction tx = s.beginTransaction(); boat = (Boat) s.get( Boat.class, boat.getId() ); assertTrue( "Annotation has precedence", 34 != boat.getWeight() ); s.delete( boat ); //s.getTransaction().commit(); tx.commit(); s.close(); sf.close(); } @Test
@Test public void testPrecedenceAnnotation() throws Exception { Configuration cfg = new Configuration(); cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); cfg.setProperty( Configuration.ARTEFACT_PROCESSING_ORDER, "class, hbm" ); cfg.addAnnotatedClass( Boat.class ); SessionFactory sf = cfg.buildSessionFactory(); assertNotNull( sf ); Session s = sf.openSession(); s.getTransaction().begin(); Boat boat = new Boat(); boat.setSize( 12 ); boat.setWeight( 34 ); s.persist( boat ); s.getTransaction().commit(); s.clear(); Transaction tx = s.beginTransaction(); boat = (Boat) s.get( Boat.class, boat.getId() ); assertTrue( "Annotation has precedence", 34 == boat.getWeight() ); s.delete( boat ); tx.commit(); s.close(); sf.close(); } @Test
@Test public void testIgnoringHbm() throws Exception { Configuration cfg = new Configuration(); cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); cfg.setProperty( Configuration.ARTEFACT_PROCESSING_ORDER, "class" );
conf.configure(propertyFileName); break;
public SessionFactory buildSessionFactory() { Configuration config = new Configuration().configure(); //H2 version we use behaves differently from H2Dialect in Hibernate so we provide our implementation config.setProperty(Environment.DIALECT, H2LessStrictDialect.class.getName()); config.setProperty(Environment.URL, connectionUrl); config.setProperty(Environment.DRIVER, "org.h2.Driver"); config.setProperty(Environment.USER, "sa"); config.setProperty(Environment.PASS, "sa"); config.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false"); config.setProperty(Environment.USE_QUERY_CACHE, "false"); //Let's validate HBMs against the actual schema config.setProperty(Environment.HBM2DDL_AUTO, "validate"); return config.buildSessionFactory(); } }
@BeforeTest protected void beforeTest() { Configuration configuration = new Configuration() .addAnnotatedClass(Person.class) .addAnnotatedClass(Animal.class) .configure(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings( configuration.getProperties() ) .build(); _sessionFactory = configuration.buildSessionFactory(serviceRegistry); }
@Override public Set<Object> getSingletons() { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Elide elide = new Elide(new ElideSettingsBuilder(new AbstractHibernateStore.Builder(sessionFactory).build()) .withAuditLogger(new Slf4jLogger()) .build()); Set<Object> set = new HashSet<>(); set.add(new JsonApiEndpoint(elide, v -> null)); return set; } }
@Override protected void configure() { DefaultOpaqueUserFunction noUserFn = v -> null; bind(noUserFn).to(DefaultOpaqueUserFunction.class).named("elideUserExtractionFunction"); SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Elide elide = new Elide(new ElideSettingsBuilder(new HibernateStore.Builder(sessionFactory).build()) .withAuditLogger(new Slf4jLogger()) .build()); bind(elide).to(Elide.class).named("elide"); } });