Hibernate: how to map one-to-many with another database? -
have 2 classes: user , message.
1 user can have several messages.
i.e. association one-to-many.
users table in first database , messages table in second database.
how can map them?
my users , messages tables mapping:
<hibernate-mapping> <class name="com.example.hibernate.user" table="users" lazy="false"> <id name="xid" type="int" column="xid" > <generator class="increment"/> </id> <set name="messages" inverse="true" table="messages"> <key> <column name="xsin_id" not-null="true" /> </key> <one-to-many class="com.example.hibernate.message" /> </set> </class> </hibernate-mapping> <hibernate-mapping> <class name="com.example.hibernate.message" table="messages" lazy="false"> <id name="id" type="int" column="xmsgbox" > <generator class="increment"/> </id> <many-to-one name="user" class="com.example.hibernate.user"> <column name="xsin_id" not-null="true" /> </many-to-one> </class> </hibernate-mapping>
i have 2 *.cfg.xml files these classes mapped.
my test code snipplet:
session session = hibernateutiluser.getsession(); string sql_query ="from user user"; query query = session.createquery(sql_query); user user = null; for(iterator it=query.iterate();it.hasnext();){ user=(user)it.next(); break; } set<message> messages = user.getmessages(); assertnotnull(messages);
i error: association references unmapped class: com.example.hibernate.message
p.s. hibernateutiluser class:
public class hibernateutiluser { private static sessionfactory sf; private static session session; private hibernateutil() {} public static sessionfactory getsessionfactory() { if (sf == null) { sf = new configuration().configure("hibernateuser.cfg.xml").buildsessionfactory(); } return sf; } public static session getsession() { if (session == null || session.isopen() == false) { session = getsessionfactory().opensession(); } return session; } public session opensession() { return sf.opensession(); } public static void close(){ if (sf != null) sf.close(); sf = null; } }
this solution seems working link text
you need use 1 sessionfactory only. then, in mapping files classes, use schema or catalog attributes. example: code: <class name="test1" table="test1" catalog="..."> <class name="test2" table="test2" catalog="..."> of course assumes databases lives in same mysql server.
Comments
Post a Comment