Hibernate:如何与另一个数据库进行一对多映射?
我有两个类:用户和消息。
一个用户可以拥有多条消息。
即这是一对多关联。
我的用户表位于第一个数据库中,消息表位于第二个数据库中。
我怎样才能映射它们?
我的用户和消息表映射:
<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>
我还有 2 个 *.cfg.xml 文件,其中映射了这些类。
我的测试代码片段:
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);
我得到一个错误: 关联引用未映射的类:com.example.hibernate.Message
PS 我的 HibernateUtilUser 类:
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;
}
}
I have 2 classes: User and Message.
One user can have several messages.
I.e. this is association one-to-many.
My users table is in first database and the messages table in the second database.
How can I map them?
My users and 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 also have 2 *.cfg.xml files where these classes are 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 get an error: Association references unmapped class: com.example.hibernate.Message
P.S. my 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在表属性中给出数据库名称,例如 table="[your another DB Name]..messages"
Try giving DB name in table proeprty, like table="[your another DB Name]..messages"
这个解决方案似乎有效
链接文本
This solution seems working
link text