java/jsp在线急求,能不能通过不是主键的字段进行取值?

来源:百度知道 编辑:UC知道 时间:2024/06/25 01:30:55
有一个客户表,其中有个叫number(非主键,但设了唯一的字段),我想在JSP里通过这个字段对客户表进行取值

本来我用的是id(主键进行取的),现在要用到其他字段,怎么试都不行,大家帮我想想办法啊。
下面是我以前用ID取值的方法
public Customer getbycustomerId(String customerId) {
Session session = null;
Transaction tran = null;

try {

session = HibernateUtil.getSession();
tran = session.beginTransaction();
Customer byId = (Customer) session.get(Customer.class, customerId);
Hibernate.initialize(byId.getCustomerId());
tran.commit();
return byId;
} catch (RuntimeException re) {
tran.rollback();
throw re;
} finally {
session.close();
}

}

晕。
使用session.get().肯定是要通过主键的。
你不会使用查询语句吗?
String hql=“from Customer c where c.customerId = ?”;
Customer byId = (Customer) session.createQuery(hql)
.setParameter(0, customerId )
.uniqueResult();

这只是一种查询方法。
你可以再去搜索其他hibernate查询方法。

可以的.只要是唯一的..就可以取..但前提没有外联!