Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate:如何将一个数据库映射到另一个数据库?_Hibernate_Hibernate Mapping - Fatal编程技术网

Hibernate:如何将一个数据库映射到另一个数据库?

Hibernate:如何将一个数据库映射到另一个数据库?,hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,我有两个类:用户和消息 一个用户可以有多条消息 也就是说,这是一对多的关联 My users表位于第一个数据库中,messages表位于第二个数据库中 我如何绘制它们的地图 我的用户和消息表映射: <hibernate-mapping> <class name="com.example.hibernate.User" table="users" lazy="false"> <id name="xId" type="int" column="xid"


我有两个类:用户和消息
一个用户可以有多条消息
也就是说,这是一对多的关联
My users表位于第一个数据库中,messages表位于第二个数据库中
我如何绘制它们的地图


我的用户和消息表映射:

<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>

这个解决方案似乎有效

您只需要使用一个SessionFactory。然后,在类的映射文件中,使用模式或目录属性。例如:
代码:
当然,这假设数据库位于同一个MySQL服务器中。

尝试在表proeprty中指定DB名称,如table=“[your other DB name]…messages”



我们为两个数据库提供了两个不同的会话,因此我想为不同的数据库表配置多对一关系。pms数据库中的projects表和admin数据库中的commonlookupdetails表,您能帮助我如何在多对一关系中映射这两个表吗?它显示了一个错误关联引用未映射类:com.pb.oas.pms.data.hibernate.commonlookupdetails您能帮助我吗Jothi女士
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);
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;
    }
}
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.
<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>