您的位置:首页 > 编程语言 > Java开发

入门例子:myeclipse开发hibernate(SQLServer)

2009-03-02 17:10 381 查看
package cn.edu.hdu.java;

public class Cat implements java.io.Serializable {

// Fields

private String catId;

private String name;

private String sex;

private String weight;

// Constructors

/** default constructor */

public Cat() {

}

/** minimal constructor */

public Cat(String name) {

this.name = name;

}

/** full constructor */

public Cat(String name, String sex, String weight) {

this.name = name;

this.sex = sex;

this.weight = weight;

}

// Property accessors

public String getCatId() {

return this.catId;

}

public void setCatId(String catId) {

this.catId = catId;

}

public String getName() {

return this.name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return this.sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getWeight() {

return this.weight;

}

public void setWeight(String weight) {

this.weight = weight;

}

}

----------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

Mapping file autogenerated by MyEclipse - Hibernate Tools

-->

<hibernate-mapping>

<class name="cn.edu.hdu.java.Cat" table="cat" >

<id name="catId" type="java.lang.String">

<column name="cat_id" length="32" />

<generator class="uuid.hex"></generator>

</id>

<property name="name" type="java.lang.String">

<column name="name" length="16" not-null="true" />

</property>

<property name="sex" type="java.lang.String">

<column name="sex" length="8" />

</property>

<property name="weight" type="java.lang.String">

<column name="weight" length="8" />

</property>

</class>

</hibernate-mapping>

-------------------------------------------------------------------------------------

package cn.edu.hdu.java.util;

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.cfg.Configuration;

/**

* Configures and provides access to Hibernate sessions, tied to the

* current thread of execution. Follows the Thread Local Session

* pattern, see {@link http://hibernate.org/42.html }.

*/

public class HibernateSessionFactory {

/**

* Location of hibernate.cfg.xml file.

* Location should be on the classpath as Hibernate uses

* #resourceAsStream style lookup for its configuration file.

* The default classpath location of the hibernate config file is

* in the default package. Use #setConfigFile() to update

* the location of the configuration file for the current session.

*/

//private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

private static Configuration configuration = new Configuration();

private static org.hibernate.SessionFactory sessionFactory;

private static String configFile = "/hibernate.cfg.xml";

private HibernateSessionFactory() {

}

/**

* Returns the ThreadLocal Session instance. Lazy initialize

* the <code>SessionFactory</code> if needed.

*

* @return Session

* @throws HibernateException

*/

public static Session getSession() throws HibernateException {

Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {

if (sessionFactory == null) {

rebuildSessionFactory();

}

session = (sessionFactory != null) ? sessionFactory.openSession()

: null;

threadLocal.set(session);

}

return session;

}

/**

* Rebuild hibernate session factory

*

*/

public static void rebuildSessionFactory() {

try {

configuration.configure(configFile);

sessionFactory = configuration.buildSessionFactory();

} catch (Exception e) {

System.err

.println("%%%% Error Creating SessionFactory %%%%");

e.printStackTrace();

}

}

/**

* Close the single hibernate session instance.

*

* @throws HibernateException

*/

public static void closeSession() throws HibernateException {

Session session = (Session) threadLocal.get();

threadLocal.set(null);

if (session != null) {

session.close();

}

}

/**

* return session factory

*

*/

public static org.hibernate.SessionFactory getSessionFactory() {

return sessionFactory;

}

/**

* return session factory

*

* session factory will be rebuilded in the next call

*/

public static void setConfigFile(String configFile) {

HibernateSessionFactory.configFile = configFile;

sessionFactory = null;

}

/**

* return hibernate configuration

*

*/

public static Configuration getConfiguration() {

return configuration;

}

}

------------------------------------------------------------------------------------

package cn.edu.hdu.java;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class TestHibernate {
Session session=null;
Transaction tx=null;
public static void main(String[] args) {
TestHibernate th=new TestHibernate();
List cl=th.getAllCats();
if(cl!=null){
Iterator it=cl.iterator();
while(it.hasNext()){
Cat cat=(Cat)it.next();
System.out.println("catID:"+cat.getCatId()+"/tname:"+cat.getName()+"/tsex:"+cat.getSex());
}
}

}
public List getAllCats(){
session=HibernateSessionFactory.getSession();
List catlist=null;
try{
tx=session.beginTransaction();
catlist=session.createQuery("from Cat").list();
return catlist;
}catch(Exception ex){
System.err.println(ex.getMessage());
return null;
}finally{
HibernateSessionFactory.closeSession();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: