aspose file tools
The moose likes Object Relational Mapping and the fly likes Multiple config file in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Multiple config file in hibernate" Watch "Multiple config file in hibernate" New topic

Multiple config file in hibernate

yogesh kalaskar

Joined: Jan 24, 2007
Posts: 6
I want to keep 2 config files as i have 2 database systems to work with.

The classes of mapping files in hb1.cfg.xml are referring to the classes in hb2.cfg.xml as they have many to one relationship between them.

But in session factory i can configure only one xx.cfg.xml file. It results into org.hibernate.MappingException: An association from the table XXX refers to an unmapped class: YYY.

Any idea how to resolve it.

Mapping file in hb1.cfg.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="com.sys.objectmodel">
<class name="Service" table="Service">
<id name="serviceId" type="long" column="ServiceId" >
<generator class="assigned"/>
<property name="serviceDescription" column="ServiceDescription" />
<property name="price" column="Price" />

<many-to-one name="product" class="Product" column="ProductId"/>

<set name="userSet" table="User_Service">
<key column="ServiceId"/>
<many-to-many column="UserId"

<set name="ciResourceSet" table="ServiceUtilizes">
<key column="ServiceId"/>
<many-to-many column="ResourceId"

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD//EN"




<property name="hibernate.connection.driver_class"></property>

<property name="hibernate.connection.url">jdbc:mysql://</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.connection.pool_size">10</property>

<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<mapping resource="hbm\Service.hbm.xml"/>

As you can see Product.hbm.xml is not present in hb1.cfg.xml. Consider it is in hb2.cfg.xml
Is it possible to solve such scenario in hibernate
[ November 26, 2008: Message edited by: yogesh kalaskar ]
Christian Gossart
Ranch Hand

Joined: Mar 13, 2008
Posts: 34
Never tried this before.
A SessionFactory object is responsible for a particular database, so in your case, you'll have 2 SessionFactory objects to deal with.
Using programmatic configuration, you can load your mapping files one by one (with the Configuration object), and then obtain a session factory.

If SF1 depends on mappings declared in SF2 (Service depends on Product), maybe building SF1 programmatically and loading both Product.hbm.xml and Service.hbm.xml would do the trick, assuming the database managed by SF1 has the corresponding synonyms to access the tables in the database managed by SF2.

But considering this Hibernate Forum, I doubt my answer would work.

Keep us posted.

Christian Gossart
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17260

First I am moving this to the ORM forum.

But you shouldn't be putting class mappings in your cfg.xml. They belong in a class.hbm files. One for each class that you are mapping.

Then in the hibernate.cfg.xml file you point to the hbm files. So you can have two hibernate.cfg.xml files point to the same .hbm mapping file.

The stuff you should put into the .cfg.xml file is just the Connection information and a list of your .hbm mapping files or the list or package of your Annotated Classes.

I recommend reading the documentation, looking at tutorials, etc.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Multiple config file in hibernate