Hibernate:如何与另一个数据库进行一对多映射?

发布于 2024-10-02 21:32:42 字数 2466 浏览 0 评论 0原文


我有两个类:用户和消息。
一个用户可以拥有多条消息。
即这是一对多关联。
我的用户表位于第一个数据库中,消息表位于第二个数据库中。
我怎样才能映射它们?


我的用户和消息表映射:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2024-10-09 21:32:42

尝试在表属性中给出数据库名称,例如 table="[your another DB Name]..messages"

<set name="messages" inverse="true" table="AnotherDB..messages"> 
        <key> 
            <column name="xsin_id" not-null="true" /> 
        </key> 
        <one-to-many class="com.example.hibernate.Message" /> 
     </set>  

Try giving DB name in table proeprty, like table="[your another DB Name]..messages"

<set name="messages" inverse="true" table="AnotherDB..messages"> 
        <key> 
            <column name="xsin_id" not-null="true" /> 
        </key> 
        <one-to-many class="com.example.hibernate.Message" /> 
     </set>  
德意的啸 2024-10-09 21:32:42

这个解决方案似乎有效
链接文本

You need to use one SessionFactory only. Then, in the mapping files for your classes, use the schema or catalog attributes. For example:

Code:
<class name="Test1" table="Test1" catalog="...">

<class name="Test2" table="Test2" catalog="...">


This of course assumes that the databases lives in the same MySQL server.

This solution seems working
link text

You need to use one SessionFactory only. Then, in the mapping files for your classes, use the schema or catalog attributes. For example:

Code:
<class name="Test1" table="Test1" catalog="...">

<class name="Test2" table="Test2" catalog="...">


This of course assumes that the databases lives in the same MySQL server.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文