I'm working to improvement a framework that work with hibernate. this framework use a hibernate.cfg.xml file, that has properties and mapping of Annotated Class.
Right now I want to separate the load of the properties from the mapping. the properties will be in the side of the developer and the mapping in the core of the framework. So when I load the hibernate.cfg.xml, I want to know if the configuration has already a class mapping. it's that right, launched an exception.
I try out some of the methods that are in the Configuration interface, but I must be using it wrong.
Here it is the main:
Code:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class main {
public static void main(String[] args){
Configuration cfg;
cfg = new Configuration();
cfg.configure();
if(cfg.getTableMappings().hasNext())
System.out.println("Class mapping found");
if(cfg.getClassMappings().hasNext())
System.out.println("Class mapping found");
SessionFactory sfc = cfg.buildSessionFactory();
Session sess = sfc.openSession();
if(sess.isOpen());
System.out.println("Open");
sess.close();
sfc.close();
}
}
Here the hibernate.cfg.xml:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password">12345</property>
<property name="hibernate.connection.pool_size">5</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="c3p0.initialPoolSize">5</property>
<property name="c3p0.minPoolSize">5</property>
<property name="c3p0.maxPoolSize">20</property>
<property name="c3p0.maxIdleTime">1200</property>
<property name="c3p0.idleConnectionTestPeriod">1200</property>
<property name="c3p0.maxStatements">50</property>
<property name="c3p0.automaticTestTable">C3P0TestTable</property>
<property name="c3p0.acquireRetryAttempts">30</property>
<property name="c3p0.acquireIncrement">3</property>
<mapping class="clazz.ModelA" />
</session-factory>
</hibernate-configuration>
and here ModelA:
Code:
package clazz;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Proxy;
@Entity
@Table(name = "t_modela")
@Proxy(lazy = false)
public class ModelA {
private int id;
private String name;
private int age;
// --------------------------------------------------------------------------------
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// --------------------------------------------------------------------------------
public ModelA() {
// Empty
}
// --------------------------------------------------------------------------------
}
Excuse me for my terrible English (not my native language), and thanks in advance.