2013-07-28 7 views
22

두 개의 열이 firstnamelastname이고 하나의 테이블에 String 데이터 유형이 있다고 가정 해 보겠습니다. 일반적으로 내 hql 쿼리를 작성Hibernate HQL 쿼리에서 두 개의 속성을 연결할 수 있습니까?

"select firstname,lastname from contact" 

두 속성을 모두 연결하는 hql 쿼리를 작성할 수 있습니까?

select concat(c.firstname, ' ', c.lastname) as fullname from Contact c 

the documentation를 참조하십시오

어쩌면 뭔가 당신이 분리를 원하는 경우, "select firstname+lastname as fullname from Contact"

답변

33
select concat(c.firstname, c.lastname) as fullname from Contact c 

같은 나.

+0

감사합니다. 그 사이에'',''또는'space '를 추가 할 수 있습니까? – abiieez

+2

내 수정 된 답변보기 –

3

나는

public List<Contract> findContracts(String fullName) { 
    Query q = sessionFactory.getCurrentSession().createQuery("from Contract c where :fullName = concat(c.firstname, ' ', c.lastname)"); 
    q.setString("fullName", fullName); 
    return q.list();} 
15

당신은 당신의 엔티티에 계산 된 열을 만들 수 있습니다 HQL과 있도록했다 :

@Formula(value = " concat(first_name, ' ', last_name) ") 
private String fullName; 

그리고 당신은 당신이 어떤에 할 것 같은이 필드를 참조하여 HQL에 다른.

귀하의 경우에는

, 당신은 할 수 있습니다 :

"select fullName from Contact" 
2

당신은 또한 사용할 수 있습니다 || 연결 연산자 :

"select c.firstName || ' ' || c.lastName as fullName from Contact" 

비록 혼란 스러울 지 모르지만 읽을 수는 있습니다.

관련 문제