Hibernate:在同一应用程序中使用两种不同的DataBase模式 [英] Hibernate: Using two different DataBase schemas in the same application
问题描述
上下文
我正在创建一个数据库环境,我想将数据拆分成不同的模式,用于不同的用户组。虽然这些数据库之一应由所有人共享,因为它包含公共实体。
假设数据库:
- DB1 - 常用实体;
- 车轮实体
- DB2 - 组A;
- 汽车实体
- DB3 - 组B;
- 摩托车实体
我有三个不同的项目:
- 项目1:
- li>
- li>
- 项目2:
- 汽车构造函数
- 项目3:
- 摩托车构造函数
问题
我正在尝试从项目/模式(2 ,A)和(3,B)
第一个问题:是可能吗?
第二个:我如何做?
hibernate.cfg.xml
在项目2中配置到
< property name =hibernate.connection.url> jdbc:mysql://99.999.999.99:3306 / DB2< /性>
这必然限制到DB2的所有连接,或者还有另一种添加新连接或工作的方式所有数据库在3306端口,或至少DB1?
将项目2中的project1 的实体映射似乎也没有成功:
< mapping class =com.company.project1.Wheels
package =com.company。 project1.Wheelsresource =com / company / project1 / Wheels.hbm.xml/>
配置
- Eclipse Indigo
- MySql 5.5
- Hibernate 3.0(通过xml映射代替注释)
- Win 7
感谢您的帮助!
p>您可以使用 @Table(catalog =)
指定其所属的数据库,然后也可以跨数据库建立关系。
在您的情况下轮
映射到DB1,汽车
使用catalog属性到DB2和 MotorCycle
到DB3。
我已经将这个解决方案与MySQL和MSSQL一起使用,工作完美。只有这个约束,所有这三个数据库必须在同一个数据库服务器和正在用于访问数据库的用户应该具有适当的权限到所有数据库。
由于此解决方案只是在所有查询中向表添加模式名称。
Context
I'm creating a database environment where I'd like to split data in several different schemas to be used for different groups of users. Although, one of these databases should be shared to everyone due to it contains common entities.
Suppose databases:
- DB1 - Common entities;
- Wheels entity
- DB2 - Group "A";
- Cars entity
- DB3 - Group "B";
- Motorcycles entity
I have three different projects:
- Project 1:
- Wheels bean
- Project 2:
- Cars constructor
- Project 3:
- Motorcycles constructor
Problem
I'm trying to access wheels (Project 1) from projects/schemas (2,"A") and (3,"B")
First question: Is it possible? Second: How can I do it?
hibernate.cfg.xml
in project 2 is configured to
<property name="hibernate.connection.url">jdbc:mysql://99.999.999.99:3306/DB2</property>
This necessarily must restrict all the connections to DB2, or there's another way to add a new connection or work with all databases in 3306 port, or at least DB1?
Mapping the entities from project1 in project 2 seems not to be succeeded too, like:
<mapping class="com.company.project1.Wheels"
package="com.company.project1.Wheels" resource="com/company/project1/Wheels.hbm.xml"/>
Configuration
- Eclipse Indigo
- MySql 5.5
- Hibernate 3.0 (mapping through xml instead annotations)
- Win 7
Thanks for helping!
You can use @Table(catalog="")
to specify database to which they belong to and then also can make relation across database.
in your case Wheel
maps to DB1, Car
to DB2 and MotorCycle
to DB3 using catalog attribute.
i have used this solution with MySQL and MSSQL and works perfectly fine. only constraint this has all three DB has to be in same database server and user which is being used to access db should have appropriate permission to all DB.
As this solution just adds schema name against table in all queries.
这篇关于Hibernate:在同一应用程序中使用两种不同的DataBase模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!