我希望在数据库中使用两个不同的模式,每个模式有相同的表格集但数据不同。如何使用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(); //-- Similarly for other
您可以参考此链接以了解如何映射多个模式,但它不是特定于Hibernate的。
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="hibernate.connection.url">jdbc:db2://localhost:50000/TEST</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pwd</property>
<property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">auto </property>
<mapping class="com.test.db2procedure.User"/>
<mapping class="com.test.db2procedure.User1"/>
</session-factory>
</hibernate-configuration>
@Entity
@Table(name="SCHEMA.USER") ////Here you can specify your schema name. Here my schema name is schema
public class User implements Serializable {
private String city;
private String firstname;
enter code here
@Id
@Column(name="ID")
private String id;
private String lastname;
public User() {
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
}
@Entity
@Table(name="SCHEMA1.USER") //Here you can specify your schema name. Here my schema name is schema1
public class User1 implements Serializable {
private String city;
private String firstname;
@Id
@Column(name="ID")
private String id;
private String lastname;
public User1() {
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
}
为了测试这个:
public class Test{
public static void main(String args[]){
SessionFactory factory ;
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
List<User> user=new ArrayList<User>();
factory = cfg.buildSessionFactory();
Session session = factory.openSession();
String hql = "select u2.city from User u1,User1 u2 where u1.id=u2.id";
Query query = session.createQuery(hql);
List results = query.list();
System.out.println("User City: "+results.get(0).toString());
}
}
@Table(name="schema1.table")
@SecondaryTables({
@SecondaryTable(name = "schema2.table")
})
@Column( name = "col1", table = "schema2.table")
@Table(name="TABLE_NAME", catalog="SCHEMA_NAME")
通过这个案例,您可以处理多个模式。
hibernate.reveng.xml
中):<hibernate-reverse-ingineering>
<schema-selection math-catalog="DataBaseName" />
<table-filter match-schema="FirstSchema" match-name="table-name1" />
<table-filter match-schema="SecondSchema" match-name="table-name2" />
</hibernate-reverse-ingineering>