少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

/**
 * UserBean entity. @author MyEclipse Persistence Tools
 */

public class UserBean implements java.io.Serializable {

 // Fields

 private String id1;
 private String name1;
 private java.util.Date createtime1;
 private String address;

 // Constructors

 /** default constructor */
 public UserBean() {
 }

 /** minimal constructor */
 public UserBean(String id1) {
  this.id1 = id1;
 }

 /** full constructor */
 public UserBean(String id1, String name1, java.util.Date createtime1, String address) {
  this.id1 = id1;
  this.name1 = name1;
  this.createtime1 = createtime1;
  this.address = address;
 }

 // Property accessors

 public String getId1() {
  return this.id1;
 }

 public void setId1(String id1) {
  this.id1 = id1;
 }

 public String getName1() {
  return this.name1;
 }

 public void setName1(String name1) {
  this.name1 = name1;
 }

 public java.util.Date getCreatetime1() {
  return this.createtime1;
 }

 public void setCreatetime1(java.util.Date createtime1) {
  this.createtime1 = createtime1;
 }

 public String getAddress() {
  return this.address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

}



<?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 Persistence Tools
-->
<hibernate-mapping>
    <class name="com.lcdbtsf.air.po.UserBean" table="ABIN5" schema="ABIN" dynamic-update="true">
        <id name="id1" type="string">
            <column name="ID1" precision="0" />
            <generator class="assigned" />
        </id>
        <property name="name1" type="string">
            <column name="NAME1" default="defaultname1"/>
        </property>
        <property name="createtime1" type="java.util.Date">
            <column name="CREATETIME1" length="19" />
        </property>
        <property name="address" type="string" update="true"  access="field" insert="true">
            <column name="ADDRESS" default="defaultaddress" />
        </property>
    </class>
   
    <query name="getUserBeanByAddress">
     <![CDATA[select o from UserBean o where o.address=:address order by o.createtime1 desc]]>
    </query>
   
     <!-- 命名查询中使用sql,不推荐使用,影响跨数据库
    <sql-query name="getUserBeanByAddress">
     <![CDATA[select o from UserBean o where o.address=:address order by o.createtime1 desc]]>
    </sql-query>
    -->
   
</hibernate-mapping>




package com.abin.lee.hibernate;

import java.util.List;

import junit.framework.TestCase;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;

import com.lcdbtsf.air.po.UserBean;

public class UserDao extends TestCase {

 public  List<UserBean> getAll(){
  Configuration cfg = new Configuration().configure(); // 获取hibernate的配置信息
  SessionFactory sf = cfg.buildSessionFactory(); // 根据config建立sessionFactory
  Session session = sf.openSession(); // factory用于建立session,开启Session,相当于开启JDBC的Connection
  Transaction ts = session.beginTransaction(); // 创建事务的对象ts
  List<UserBean> list=null;
  try {
   Criteria criteria=session.createCriteria(UserBean.class);
   criteria.addOrder(Order.desc("createtime1"));
   list=criteria.list();
   ts.commit();
  } catch (HibernateException he) {
   he.printStackTrace();
   ts.rollback();
  }
  return list;
 }
 
 public  List<UserBean> getAllByAddress(String address){
  Configuration cfg = new Configuration().configure(); // 获取hibernate的配置信息
  SessionFactory sf = cfg.buildSessionFactory(); // 根据config建立sessionFactory
  Session session = sf.openSession(); // factory用于建立session,开启Session,相当于开启JDBC的Connection
  Transaction ts = session.beginTransaction(); // 创建事务的对象ts
  List<UserBean> list=null;
  try {
   Query query=session.getNamedQuery("getUserBeanByAddress");
   query.setParameter("address", address);
   list=query.list();
   ts.commit();
  } catch (HibernateException he) {
   he.printStackTrace();
   ts.rollback();
  }
  return list;
 }

}





package com.abin.lee.hibernate;

import java.util.List;

import junit.framework.TestCase;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.lcdbtsf.air.po.UserBean;

public class UserDaoTest extends TestCase {

 public void test() {
   List<UserBean> list=new UserDao().getAllByAddress("shanghai");
  try {
   if(list.size()>0){
    UserBean user =list.get(0);
    System.out.println(list.get(0).getId1());
   }
  } catch (HibernateException he) {
   he.printStackTrace();  }
 }

}

 

posted on 2012-09-10 13:28 abin 阅读(1807) 评论(0)  编辑  收藏 所属分类: hibernate

只有注册用户登录后才能发表评论。


网站导航: