如何将Hibernate用于单个数据库中的两种不同模式 [英] how to use Hibernate for two different schemas in a single database

查看:158
本文介绍了如何将Hibernate用于单个数据库中的两种不同模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据库中使用两种不同的模式,每种模式都有相同的一组表,但数据不同。如何使用hibernate并指向两种不同的模式。我是hibernate的新手。请向我提供链接。



提前致谢

解决方案

您可以在为您的实体定义表时通过 schema 元素指定它。


$


@Table(name =TABLE_NAME,schema =SCHEMA_NAME)

否则,您可以使用单独的 EntityManager 指向相应的模式&然后使用相同的实体,因为它们的结构相似。




编辑:您可以为每个架构&然后从它构建 SessionFactory ,下面是它的一些伪代码。

  SessionFactory sf_1 = new Configuration()。configure(schema1config.cfg.xml)。buildSessionFactory(); 
SessionFactory sf_2 = new Configuration()。configure(schema2config.cfg.xml)。buildSessionFactory();

session_1 = sf_1.openSession(); // - 类似的其他

您可以参考此链接以获取更多详细信息以映射多个模式,但它不是特定于hibernate的。


I want to use two different schema in database, each schema has same set of Tables but data differs. How to use hibernate and point to two different schema.I am new to the hibernate.Please provide me the links.

Thanks in advance

解决方案

You can specify it by schema element while defining table for your entity.

@Table(name="TABLE_NAME", schema="SCHEMA_NAME")

Else, you can use separate EntityManager pointing to respective schema & then use the same entity, as their structure is similar.


Edit : You can have separate configuration files for each schema & then build SessionFactory from it, below is some pseudo-code for it.

SessionFactory sf_1 = new  Configuration().configure("schema1config.cfg.xml").buildSessionFactory();
SessionFactory sf_2 = new Configuration().configure("schema2config.cfg.xml").buildSessionFactory();

session_1 = sf_1.openSession();  //-- Similarly for other

You can refer this link for further details to map multiple schema, but it isn't hibernate specific.

这篇关于如何将Hibernate用于单个数据库中的两种不同模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆