HQL Select 查询
HQL Select Query
如何 select 基于唯一列的行。
在我的 table 中,ID 是自动生成的。我正在使用 phone 数字到 select 在我的 table 模式中唯一的行。
例如:
Id Phone Name
1 2209897 abc
2 5436567 def
这里,Id为主键。
Phone 是独一无二的。
我正在使用 phone 登录。
我不希望生成列表,而是生成单行作为输出。
@Repository
public class CustomerDetailsDAOImpl implements CustomerDetailsDAO {
@Autowired
SessionFactory sessionFactory;
@Override
@Transactional
public List<CustomerDetails> getCustomer(String customerPhone) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from CustomerDetails where customerPhone =:p");
q.setParameter("p", customerPhone);
List<CustomerDetails> customer = q.getResultList();
session.close();
return customer;
}
不要使用 q.getResultList()
,而是使用 q.getSingleResult()
,因为 phone 数字在您的数据库中是唯一的,查询将 return CustomerDetails
的单个对象class.
public CustomerDetails getCustomer(String customerPhone) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from CustomerDetails where customerPhone =:p");
CustomerDetails customer=null;
q.setParameter("p", customerPhone);
try{
customer = q.getResultList();
}catch(NoResultException ex){
System.out.println(ex.getMessage());
}finally{
session.close();
}
return customer;
}
注意 :如果您的查询没有从数据库中检索任何数据,那么 getSingleResult()
将抛出 NoResultException
异常。
所以你必须从你这边处理这个异常。
您可以参考上面的例子。我已经为 NoResultException
异常处理添加了代码。
使用 getSingleResult() 而不是使用 getListResult()。这会执行一个 SELECT 查询,returns 一个未类型化的结果。因此,无论何时您希望以唯一类型检索单个记录,都应使用 getSingleResult()。
@Repository
public class CustomerDetailsDAOImpl implements CustomerDetailsDAO {
@Autowired
SessionFactory sessionFactory;
@Override
@Transactional
public List<CustomerDetails> getCustomer(String customerPhone) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from CustomerDetails where customerPhone =:p");
q.setParameter("p", customerPhone);
CustomerDetails customer = q.getSingleResult();
session.close();
return customer;
}
希望这对您有所帮助:)
如何 select 基于唯一列的行。 在我的 table 中,ID 是自动生成的。我正在使用 phone 数字到 select 在我的 table 模式中唯一的行。
例如:
Id Phone Name
1 2209897 abc
2 5436567 def
这里,Id为主键。 Phone 是独一无二的。
我正在使用 phone 登录。 我不希望生成列表,而是生成单行作为输出。
@Repository
public class CustomerDetailsDAOImpl implements CustomerDetailsDAO {
@Autowired
SessionFactory sessionFactory;
@Override
@Transactional
public List<CustomerDetails> getCustomer(String customerPhone) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from CustomerDetails where customerPhone =:p");
q.setParameter("p", customerPhone);
List<CustomerDetails> customer = q.getResultList();
session.close();
return customer;
}
不要使用 q.getResultList()
,而是使用 q.getSingleResult()
,因为 phone 数字在您的数据库中是唯一的,查询将 return CustomerDetails
的单个对象class.
public CustomerDetails getCustomer(String customerPhone) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from CustomerDetails where customerPhone =:p");
CustomerDetails customer=null;
q.setParameter("p", customerPhone);
try{
customer = q.getResultList();
}catch(NoResultException ex){
System.out.println(ex.getMessage());
}finally{
session.close();
}
return customer;
}
注意 :如果您的查询没有从数据库中检索任何数据,那么 getSingleResult()
将抛出 NoResultException
异常。
所以你必须从你这边处理这个异常。
您可以参考上面的例子。我已经为 NoResultException
异常处理添加了代码。
使用 getSingleResult() 而不是使用 getListResult()。这会执行一个 SELECT 查询,returns 一个未类型化的结果。因此,无论何时您希望以唯一类型检索单个记录,都应使用 getSingleResult()。
@Repository
public class CustomerDetailsDAOImpl implements CustomerDetailsDAO {
@Autowired
SessionFactory sessionFactory;
@Override
@Transactional
public List<CustomerDetails> getCustomer(String customerPhone) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from CustomerDetails where customerPhone =:p");
q.setParameter("p", customerPhone);
CustomerDetails customer = q.getSingleResult();
session.close();
return customer;
}
希望这对您有所帮助:)