Java Code Examples for org.hibernate.cfg.Configuration#buildMappings()

The following examples show how to use org.hibernate.cfg.Configuration#buildMappings() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void testJoinedSubclassAndEntityNamesOnly() {
	Configuration cfg = new Configuration();

	try {
		cfg.addResource( getBaseForMappings() + "extendshbm/entitynames.hbm.xml" );

		cfg.buildMappings();

		assertNotNull( cfg.getClassMapping( "EntityHasName" ) );
		assertNotNull( cfg.getClassMapping( "EntityCompany" ) );

	}
	catch ( HibernateException e ) {
		e.printStackTrace();
		fail( "should not fail with exception! " + e );

	}
}
 
Example 2
public void testEntityNamesWithPackageFailureExpected() {
	Configuration cfg = new Configuration();
	try {
		cfg.addResource( getBaseForMappings() + "extendshbm/packageentitynames.hbm.xml" );

		cfg.buildMappings();

		assertNotNull( cfg.getClassMapping( "EntityHasName" ) );
		assertNotNull( cfg.getClassMapping( "EntityCompany" ) );

	}
	catch ( HibernateException e ) {
		e.printStackTrace();
		fail( "should not fail with exception! " + e );

	}
}
 
Example 3
public void testUnionSubclass() {
	Configuration cfg = new Configuration();

	try {
		cfg.addResource( getBaseForMappings() + "extendshbm/unionsubclass.hbm.xml" );

		cfg.buildMappings();

		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );

	}
	catch ( HibernateException e ) {
		e.printStackTrace();
		fail( "should not fail with exception! " + e );

	}
}
 
Example 4
public void testEntityIncludeResolution() {
	// Parent.hbm.xml contains the following entity include:
	//		<!ENTITY child SYSTEM "classpath://org/hibernate/test/util/dtd/child.xml">
	// which we are expecting the Hibernate custom entity resolver to be able to resolve
	// locally via classpath lookup.
	Configuration cfg = new Configuration();
	cfg.addResource( "org/hibernate/test/util/dtd/Parent.hbm.xml" );
	cfg.buildMappings();
}
 
Example 5
public void testNwaitingForSuper() {
	Configuration cfg = new Configuration();

	try {
		cfg.addResource( getBaseForMappings() + "extendshbm/Customer.hbm.xml" );
		assertNull(
				"cannot be in the configuration yet!",
				cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" )
		);
		cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" );
		assertNull(
				"cannot be in the configuration yet!",
				cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" )
		);
		cfg.addResource( getBaseForMappings() + "extendshbm/Person.hbm.xml" );

		cfg.buildMappings();

		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) );
		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );


	}
	catch ( HibernateException e ) {
		e.printStackTrace();
		fail( "should not fail with exception! " + e );

	}

}
 
Example 6
Source Project: unitime   File: HibernateUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void fixSchemaInFormulas(Configuration cfg) throws ClassNotFoundException {
	cfg.buildMappings();
	Class dialect = Class.forName(cfg.getProperty("dialect"));
	String schema = cfg.getProperty("default_schema");
	for (Iterator i=cfg.getClassMappings();i.hasNext();) {
        PersistentClass pc = (PersistentClass)i.next();
        for (Iterator j=pc.getPropertyIterator();j.hasNext();) {
            Property p = (Property)j.next();
            for (Iterator k=p.getColumnIterator();k.hasNext();) {
                Selectable c = (Selectable)k.next();
                if (c instanceof Formula) {
                    Formula f = (Formula)c;
                    boolean updated = false;
                    if (schema != null && f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%")>=0) {
                        f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
                        sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
                    }
                    if (f.getFormula()!=null && (f.getFormula().indexOf("%TRUE%")>=0 || f.getFormula().indexOf("%FALSE%")>=0)) {
                    	if (isPostgress(dialect)) {
                    		f.setFormula(f.getFormula().replaceAll("%TRUE%", "'t'").replaceAll("%FALSE%", "'f'"));
                    	} else {
                    		f.setFormula(f.getFormula().replaceAll("%TRUE%", "1").replaceAll("%FALSE%", "0"));
                    	}
                    }
                    if (updated)
                    	sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
                }
            }
        }
    }
}