我想在數據庫中使用兩種不同的模式,每種模式具有相同的一組表,但數據不同。如何使用hibernate並指向兩種不同的schema.I是hibernate的新手。請向我提供鏈接。如何在單個數據庫中使用兩種不同模式的Hibernate
在此先感謝
我想在數據庫中使用兩種不同的模式,每種模式具有相同的一組表,但數據不同。如何使用hibernate並指向兩種不同的schema.I是hibernate的新手。請向我提供鏈接。如何在單個數據庫中使用兩種不同模式的Hibernate
在此先感謝
您可以通過schema
元素而定義的實體表指定。
@Table(名稱= 「TABLE_NAME」,模式= 「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
您可以參考this link進一步的細節映射多個模式,但不休眠具體。
使用此示例(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>
在您的配置文件:
<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());
}
}
在上面的Test.class執行結果集來自架構和schema1
我們可以在這些表格之間創建一對一嗎? –
結合使用Hibernate才能配置hibernate.cfg.xml文件兩個模式。我不想使用JPA。如果配置完成,那麼,如何分別從每個模式查詢數據。你可以發佈一個示例代碼。 – shreekanth
@shreekanth您可以擁有多個配置文件,請參閱編輯部分以獲取更多詳細信息。 –
我們可以在這些表格之間創建一對一嗎? –