Richie

Sometimes at night when I look up at the stars, and see the whole sky just laid out there, don't you think I ain't remembering it all. I still got dreams like anybody else, and ever so often, I am thinking about how things might of been. And then, all of a sudden, I'm forty, fifty, sixty years old, you know?

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结果如下:
    

posted on 2010-04-09 22:44  riccc  阅读(5334)  评论(0编辑  收藏  举报

导航