Horizontal and vertical cutting of Hibernate Shards data (I)-Hibernate test environment

Source: Internet
Author: User
Preparation
You need to download the latest release versions of these projects as follows:
1. Hibernate Core, the version used here is 3.5.0-final
2. Hibernate Shards. The version 3.0.0-Beta2 is used here.
3. MySql java connector, the version used here is 5.1.12
4. slf4j, the version used here is 1.5.11
5. Install and start the mysql service, create the database hbshards in mysql, and create the test contact table as follows:

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. Set java environment variables
The CLASSPATH settings used for this test are as follows:
.;
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 test project
Use Eclipse to create a java project Hibernate-Test, and create the lib directory under the project folder
Put hibernate3.jar into the lib directory, put the Hibernate release package lib \ required directory in addition to the slf4j-api-1.5.8.jar of other jar files into the lib directory
Put the mysql-connector-java-5.1.12-bin.jar In the mysql java connector release package into the lib directory
Put the slf4j-api-1.5.11.jar and slf4j-nop-1.5.11.jar in the slf4j release package into the lib directory
The project references hibernate3.jar from the lib directory.

Add the object class ContactEntity for testing under the src directory:

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 + "\" }"; }}

Test the object ing file 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">

Test class:

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. 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">

The project structure is as follows:

Run the test
Use Eclipse to compile the class file, including Main. class, ContactEntity. class, hibernate. cfg. xml, and ContactEntity. hbm. xml.
Copy to the lib directory and run Main. class. The result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.