java 方法命名find query_Hibernate 命名查询NamedQuery

/**

* 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;

}

}

/p>

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

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 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 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 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 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 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();  }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值