Hibernate Shards 数据的水平、垂直切割(一)- Hibernate测试环境
生活随笔
收集整理的這篇文章主要介紹了
Hibernate Shards 数据的水平、垂直切割(一)- Hibernate测试环境
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
準備
需要用到的東西如下,下載這幾個項目最新的release版本:
1. Hibernate Core,這里用的版本為3.5.0-final
2. Hibernate Shards,這里用的版本為3.0.0-Beta2
3. MySql java connector,這里用的版本為5.1.12
4. slf4j,這里用的版本為1.5.11
5. 安裝并啟動mysql服務,在mysql中建立數據庫hbshards,在里面如下建立測試用的contact表: CREATE TABLE contact ( ID varchar(8) NOT NULL, LOGIN_ID varchar(18) NOT NULL, PASSWORD varchar(12) NOT NULL, NAME varchar(40) NOT NULL, EMAIL varchar(40) NOT NULL, PRIMARY KEY (ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 6. 設置好java環境變量
這里用于這個測試的CLASSPATH設置如下
.;
D:\Work\research\Java\Hibernate-Test\lib\antlr-2.7.6.jar;
D:\Work\research\Java\Hibernate-Test\lib\dom4j-1.6.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\jta-1.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\javassist-3.9.0.GA.jar;
D:\Work\research\Java\Hibernate-Test\lib\commons-collections-3.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\slf4j-nop-1.5.11.jar;
D:\Work\research\Java\Hibernate-Test\lib\slf4j-api-1.5.11.jar;
D:\Work\research\Java\Hibernate-Test\lib\mysql-connector-java-5.1.12-bin.jar;
D:\Work\research\Java\Hibernate-Test\lib\hibernate3.jar;
Hibernate測試項目
用Eclipse建一個java項目Hibernate-Test,在項目文件夾下面建立lib目錄
將hibernate3.jar放入lib目錄中,把Hibernate發布包中lib\required目錄下除了slf4j-api-1.5.8.jar之外的其他jar文件放入lib目錄中
將mysql java connector發布包中的mysql-connector-java-5.1.12-bin.jar放入lib目錄
將slf4j發布包中的slf4j-api-1.5.11.jar、slf4j-nop-1.5.11.jar放入lib目錄
項目從lib目錄中引用hibernate3.jar
在src目錄下添加測試用的實體類ContactEntity: public class ContactEntity {private String _id;private String _name;private String _email;private String _loginId;private String _password;
public ContactEntity(){}public ContactEntity(String id, String loginId, String password,String name, String email){this._id = id;this._loginId = loginId;this._password = password;this._name = name;this._email = email;} public String getId(){return this._id;}public void setId(String id){this._id=id;}public String getEMail(){return this._email;}public void setEMail(String email){this._email=email;}public String getName(){return this._name;}public void setName(String name){this._name=name;}public String getLoginId(){return this._loginId;}public void setLoginId(String loginId){this._loginId=loginId;}public String getPassword(){return this._password;}public void setPassword(String password){this._password=password;}public String toString(){return "{ Id=\"" + this._id + "\""+ ", LoginId=\"" + this._loginId + "\""+ ", Name=\"" + this._name + "\""+ ", EMail=\"" + this._email + "\" }";} } 測試實體的映射文件ContactEntity.hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping><class name="ContactEntity" table="CONTACT"><id name="Id" column="ID" ><generator class="assigned"/></id> <property name="EMail" column="EMAIL" /><property name="Name" column="NAME" /><property name="LoginId" column="LOGIN_ID" /><property name="Password" column="PASSWORD" /></class> </hibernate-mapping> 測試用的類: import java.util.Iterator; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
public class Main {public static void main(String[] args) {String loginId = "RicCC@cnblogs.com";String password = "123";if(args!=null && args.length==2){loginId = args[0];password = args[1];}HibernateTest(loginId, password);}private static void HibernateTest(String loginId, String password){SessionFactory factory = null;Session session = null;Transaction transaction = null;List contacts = null;Iterator it = null;try{factory = new Configuration().configure().buildSessionFactory();session = factory.openSession();transaction = session.beginTransaction();
System.out.println("===Create Contacts==="); ContactEntity c = new ContactEntity("01111111","RicCC@cnblogs.com", "123", "Richie", "RicCC@cnblogs.com");session.save(c);c = new ContactEntity("91111111","a@cnblogs.com", "123", "AAA", "a@cnblogs.com");session.save(c);c = new ContactEntity("81111111","b@cnblogs.com", "123", "BBB", "b@cnblogs.com");session.save(c);c = new ContactEntity("31111111","c@cnblogs.com", "123", "CCC", "c@cnblogs.com");session.save(c);session.flush();
System.out.println("\n===Login Test===");contacts = session.createQuery("from ContactEntity where LoginId=:loginId").setString("loginId", loginId).list();if(contacts.isEmpty())System.out.println("contact " + loginId + " not found!");else{c = (ContactEntity)contacts.get(0);if(c.getPassword()==password)System.out.println("user " + loginId + " login successful");else System.out.println("password is incorrect, login failed!");}
System.out.println("\n===Delete Contacts===");contacts = session.createQuery("from ContactEntity").list();it = contacts.iterator();while(it.hasNext()){session.delete(it.next());}transaction.commit();}catch(Exception e){if(transaction!=null) transaction.rollback();System.out.println(e.getMessage());}finally{if(session!=null) session.close();if(factory!=null) factory.close();}} } Hibernate配置文件hibernate.cfg.xml: <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost/hbshards</property><property name="connection.username">root</property><property name="connection.password">dev</property><property name="connection.pool_size">10</property><property name="show_sql">true</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="hbm2ddl.auto">validate</property><mapping resource="ContactEntity.hbm.xml" /> </session-factory> </hibernate-configuration> 項目結構如下圖:
?? ?
運行測試
用Eclipse編譯class文件,將Main.class、ContactEntity.class以及hibernate.cfg.xml、ContactEntity.hbm.xml 拷貝到lib目錄中,運行Main.class結果如下:
?? ?
需要用到的東西如下,下載這幾個項目最新的release版本:
1. Hibernate Core,這里用的版本為3.5.0-final
2. Hibernate Shards,這里用的版本為3.0.0-Beta2
3. MySql java connector,這里用的版本為5.1.12
4. slf4j,這里用的版本為1.5.11
5. 安裝并啟動mysql服務,在mysql中建立數據庫hbshards,在里面如下建立測試用的contact表: CREATE TABLE contact ( ID varchar(8) NOT NULL, LOGIN_ID varchar(18) NOT NULL, PASSWORD varchar(12) NOT NULL, NAME varchar(40) NOT NULL, EMAIL varchar(40) NOT NULL, PRIMARY KEY (ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 6. 設置好java環境變量
這里用于這個測試的CLASSPATH設置如下
.;
D:\Work\research\Java\Hibernate-Test\lib\antlr-2.7.6.jar;
D:\Work\research\Java\Hibernate-Test\lib\dom4j-1.6.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\jta-1.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\javassist-3.9.0.GA.jar;
D:\Work\research\Java\Hibernate-Test\lib\commons-collections-3.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\slf4j-nop-1.5.11.jar;
D:\Work\research\Java\Hibernate-Test\lib\slf4j-api-1.5.11.jar;
D:\Work\research\Java\Hibernate-Test\lib\mysql-connector-java-5.1.12-bin.jar;
D:\Work\research\Java\Hibernate-Test\lib\hibernate3.jar;
Hibernate測試項目
用Eclipse建一個java項目Hibernate-Test,在項目文件夾下面建立lib目錄
將hibernate3.jar放入lib目錄中,把Hibernate發布包中lib\required目錄下除了slf4j-api-1.5.8.jar之外的其他jar文件放入lib目錄中
將mysql java connector發布包中的mysql-connector-java-5.1.12-bin.jar放入lib目錄
將slf4j發布包中的slf4j-api-1.5.11.jar、slf4j-nop-1.5.11.jar放入lib目錄
項目從lib目錄中引用hibernate3.jar
在src目錄下添加測試用的實體類ContactEntity: public class ContactEntity {private String _id;private String _name;private String _email;private String _loginId;private String _password;
public ContactEntity(){}public ContactEntity(String id, String loginId, String password,String name, String email){this._id = id;this._loginId = loginId;this._password = password;this._name = name;this._email = email;} public String getId(){return this._id;}public void setId(String id){this._id=id;}public String getEMail(){return this._email;}public void setEMail(String email){this._email=email;}public String getName(){return this._name;}public void setName(String name){this._name=name;}public String getLoginId(){return this._loginId;}public void setLoginId(String loginId){this._loginId=loginId;}public String getPassword(){return this._password;}public void setPassword(String password){this._password=password;}public String toString(){return "{ Id=\"" + this._id + "\""+ ", LoginId=\"" + this._loginId + "\""+ ", Name=\"" + this._name + "\""+ ", EMail=\"" + this._email + "\" }";} } 測試實體的映射文件ContactEntity.hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping><class name="ContactEntity" table="CONTACT"><id name="Id" column="ID" ><generator class="assigned"/></id> <property name="EMail" column="EMAIL" /><property name="Name" column="NAME" /><property name="LoginId" column="LOGIN_ID" /><property name="Password" column="PASSWORD" /></class> </hibernate-mapping> 測試用的類: import java.util.Iterator; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
public class Main {public static void main(String[] args) {String loginId = "RicCC@cnblogs.com";String password = "123";if(args!=null && args.length==2){loginId = args[0];password = args[1];}HibernateTest(loginId, password);}private static void HibernateTest(String loginId, String password){SessionFactory factory = null;Session session = null;Transaction transaction = null;List contacts = null;Iterator it = null;try{factory = new Configuration().configure().buildSessionFactory();session = factory.openSession();transaction = session.beginTransaction();
System.out.println("===Create Contacts==="); ContactEntity c = new ContactEntity("01111111","RicCC@cnblogs.com", "123", "Richie", "RicCC@cnblogs.com");session.save(c);c = new ContactEntity("91111111","a@cnblogs.com", "123", "AAA", "a@cnblogs.com");session.save(c);c = new ContactEntity("81111111","b@cnblogs.com", "123", "BBB", "b@cnblogs.com");session.save(c);c = new ContactEntity("31111111","c@cnblogs.com", "123", "CCC", "c@cnblogs.com");session.save(c);session.flush();
System.out.println("\n===Login Test===");contacts = session.createQuery("from ContactEntity where LoginId=:loginId").setString("loginId", loginId).list();if(contacts.isEmpty())System.out.println("contact " + loginId + " not found!");else{c = (ContactEntity)contacts.get(0);if(c.getPassword()==password)System.out.println("user " + loginId + " login successful");else System.out.println("password is incorrect, login failed!");}
System.out.println("\n===Delete Contacts===");contacts = session.createQuery("from ContactEntity").list();it = contacts.iterator();while(it.hasNext()){session.delete(it.next());}transaction.commit();}catch(Exception e){if(transaction!=null) transaction.rollback();System.out.println(e.getMessage());}finally{if(session!=null) session.close();if(factory!=null) factory.close();}} } Hibernate配置文件hibernate.cfg.xml: <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost/hbshards</property><property name="connection.username">root</property><property name="connection.password">dev</property><property name="connection.pool_size">10</property><property name="show_sql">true</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="hbm2ddl.auto">validate</property><mapping resource="ContactEntity.hbm.xml" /> </session-factory> </hibernate-configuration> 項目結構如下圖:
?? ?
運行測試
用Eclipse編譯class文件,將Main.class、ContactEntity.class以及hibernate.cfg.xml、ContactEntity.hbm.xml 拷貝到lib目錄中,運行Main.class結果如下:
?? ?
轉載于:https://www.cnblogs.com/RicCC/archive/2010/04/09/hibernate-shards-1-hbtest.html
總結
以上是生活随笔為你收集整理的Hibernate Shards 数据的水平、垂直切割(一)- Hibernate测试环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 应用虚拟化的五大理由
- 下一篇: C# 时间函数(几个常用时间,程序运行计