hibernate jpa 实现连接多个数据库

原创
2015/03/18 17:17
阅读数 914

最近使用jpa进行数据迁移,由于跨数据库,就直接使用jpa来做。

先放上配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
    <persistence-unit name="old" transaction-type="RESOURCE_LOCAL">
        <class>net.mllw.exports.entity.old.TBaseGoodsBrand</class>
        <class>net.mllw.exports.entity.old.TBaseGoodsClass</class>
        <class>net.mllw.exports.entity.old.TBaseGoodsInfo</class>
        <class>net.mllw.exports.entity.old.TBaseGoodsLable</class>
        <class>net.mllw.exports.entity.old.TBaseGoodsPicture</class>
        <class>net.mllw.exports.entity.old.TBaseGoodsRelativeClass</class>
        <class>net.mllw.exports.entity.old.TBaseGoodsRelativeLable</class>
        <class>net.mllw.exports.entity.old.TGoodsConsult</class>
        <class>net.mllw.exports.entity.old.TGoodsEvaluation</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.connection.url"
                      value="jdbc:mysql:xxx"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="xxx"/>
            <property name="hibernate.connection.password" value="xxx"/>
        </properties>
    </persistence-unit>
    <persistence-unit name="new" transaction-type="RESOURCE_LOCAL">
        <class>net.mllw.exports.entity.wen.TGGoods</class>
        <class>net.mllw.exports.entity.wen.TGGoodsChannel</class>
        <class>net.mllw.exports.entity.wen.TGGoodsCombination</class>
        <class>net.mllw.exports.entity.wen.TGGoodsCustomer</class>
        <class>net.mllw.exports.entity.wen.TGGoodsPicture</class>
        <class>net.mllw.exports.entity.wen.TGGoodsPrice</class>
        <class>net.mllw.exports.entity.wen.TGProduct</class>
        <class>net.mllw.exports.entity.wen.TGProductProperty</class>
        <class>net.mllw.exports.entity.wen.TGPromotion</class>
        <class>net.mllw.exports.entity.wen.TGPromotionCombination</class>
        <class>net.mllw.exports.entity.wen.TGPromotionCustomer</class>
        <class>net.mllw.exports.entity.wen.TGPromotionFavour</class>
        <class>net.mllw.exports.entity.wen.TGPromotionPicture</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.connection.url"
                      value="jdbc:mysql:xxx"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="xxx"/>
            <property name="hibernate.connection.password" value="xxx"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

然后在代码里使用如下代码就可以了。


EntityManagerFactory factory = Persistence.();
EntityManager manager = factory.createEntityManager();


EntityManagerFactory factorynew = Persistence.();
EntityManager managernew = factorynew.createEntityManager();

....

factorynew.close();
factory.close();


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部