-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to know if a Configuration has already any class mapping
PostPosted: Tue Jun 19, 2012 5:12 pm 
Newbie

Joined: Mon Jun 18, 2012 9:05 pm
Posts: 1
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.


Top
 Profile  
 
 Post subject: Re: How to know if a Configuration has already any class mapping
PostPosted: Tue Jun 26, 2012 12:16 pm 
Newbie

Joined: Sun Jan 29, 2006 9:27 pm
Posts: 10
I did some experiments and suspect that this:

Code:
    Configuration configuration = new Configuration();
    configuration.configure("/detectamapping/hibernate.cfg.xml");


It's not enough for hibernate to read the config XML. Only after this two lines configuration.getClassMappings() will not work. If you call getClassMappings before buildSessionFactory it will return no mappings but if you call it after building the session factory it will return the mapped classes from the XML.

For example, this will not work:

Code:
    Configuration configuration = new Configuration();
    configuration.configure("/detectamapping/hibernate.cfg.xml");

    Iterator itt = configuration.getClassMappings(); // WILL NOT WORK, RETURNS EMPTY ITERATOR

    while (itt.hasNext()) {
      Object object = (Object) itt.next();
      System.err.println(object);
    }

    SessionFactory sessionFactory = configuration.buildSessionFactory();


But this will work OK:

Code:
    Configuration configuration = new Configuration();
    configuration.configure("/detectamapping/hibernate.cfg.xml");

    SessionFactory sessionFactory = configuration.buildSessionFactory();

    Iterator itt = configuration.getClassMappings(); // AFTER buildSessionFactory

    while (itt.hasNext()) {
      Object object = (Object) itt.next();
      System.err.println(object);
    }


I think you can also call to buildMappings to avoid building the session this way:

Code:
    Configuration configuration = new Configuration();
    configuration.configure("/detectamapping/hibernate.cfg.xml");

    configuration.buildMappings(); // LOADS THE MAPPING FROM THE XML

    Iterator itt = configuration.getClassMappings(); // WORKS AFTER buildMappings, NO NEED TO buildSessionFactory

    while (itt.hasNext()) {
      Object object = (Object) itt.next();
      System.err.println(object);
    }

    SessionFactory sessionFactory = configuration.buildSessionFactory(); // DO THIS LATER


Hope it helps...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.