网上资料良莠不齐。终于找到
如http://docs.jboss.org/hibernate/shards/3.0/reference/en/html_single/中介绍的。
public static SessionFactory createSessionFactory() {
Configuration prototypeConfig = new Configuration().configure("hibernate1.xml");}
简单的注意一下hibernate1.xml、hibernate2.xml只有数据库连接的信息,没有<mapping resource="com/ha/entity/Student.hbm.xml" />这种要注释,因为自动根据shardConfigs去找。
hibernate1.xml:
<hibernate-configuration>
<session-factory name="HibernateSessionFactory1" >
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/new
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hbm2ddl.auto">validate</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.shard_id">0</property>
<property name="hibernate.shard.enable_cross_shard_relationship_checks">
true
</property>
<!-- <mapping resource="com/ha/entity/Student.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
hibernate2.xml:
<hibernate-configuration>
<session-factory name="HibernateSessionFactory" >
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3307/dome
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hbm2ddl.auto">validate</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.shard_id">6</property>
<property name="hibernate.shard.enable_cross_shard_relationship_checks">
true
</property>
<!-- <mapping resource="com/ha/entity/Teacher.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
搞定了。哈哈。
测试:
SessionFactory sf=createSessionFactory();
Session session= sf.openSession();
List<Student> list=session.createQuery("from Student s").list();
System.out.println("size="+list.size());
有结果了。over。