2016-11-19 2 views
1

봄과 최대 절전 모드를 사용하는 Vaadin Java 프로젝트에서 작업하고 있습니다. 내가 쿼리를 실행하면최대 절전 모드로 데이터베이스 쿼리를 만들지 만 null 엔티티를 반환합니다.

public AreaFuncional obtenerAreaPorId(Long id) { 
    AreaFuncional area = (AreaFuncional) 
    getSessionFactory().getNamedQuery("obtenerAreaPorId").setLong(0, id) 
    .uniqueResult(); 
    if (area == null) 
     throw new RuntimeException("No existe el area con id " + id); 
    return area; 

} 

, 나는 데이터베이스에 대한 올바른 MySQL의 쿼리를 얻을 :

01:49:11,785 DEBUG http-bio-8080-exec-42 hibernate.SQL:109 - select areafuncio0_.id as id1_0_, areafuncio0_.area_pertenencia_id as area_per5_0_, areafuncio0_.email as email2_0_, areafuncio0_.nombre as nombre3_0_, areafuncio0_.telefonos as telefono4_0_ from AreaFuncional areafuncio0_ where areafuncio0_.id=? 

import java.io.Serializable; 
import java.util.HashSet; 
import java.util.Set; 
import javax.persistence.CascadeType; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 

import org.hibernate.annotations.NamedQueries; 
import org.hibernate.annotations.NamedQuery; 

@Entity 
@NamedQueries({ 
    @NamedQuery(name = "listarAreas", query = "SELECT a FROM AreaFuncional a"), 
    @NamedQuery(name = "obtenerAreaPorId", query = "SELECT a FROM AreaFuncional a where a.id=?") }) 
public class AreaFuncional implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    private String nombre; 

    @ManyToOne(cascade = { CascadeType.ALL }) 
    @JoinColumn(name = "area_pertenencia_id") 
    private AreaFuncional areaPertenencia; 

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    private Set<AreaFuncional> subAreas; 

    @OneToMany(fetch = FetchType.LAZY) 
    private Set<Empleado> empleados; 
    private String telefonos; 
    private String email; 

    public AreaFuncional() { 
     this.subAreas = new HashSet<AreaFuncional>(); 
     this.empleados = new HashSet<Empleado>(); 
    } 
.... 

가 그럼 난 다음 쿼리를 가지고 : 내 프로젝트에 다음 엔티티를

콘솔에서 mysql 문을 실행하면 정답을 얻지 만 Hibernate는 null 객체를 반환합니다.

오류가 표시되지 않습니다. 아무도 도와 줄 수 있습니까?

답변

1

당신이 다음 명명 된 쿼리를 사용하려면 당신이로 사용한다 :

@NamedQuery(name = "obtenerAreaPorId", query = "SELECT a FROM AreaFuncional a where a.id= :id") }) 

getSessionFactory().getNamedQuery("obtenerAreaPorId").setLong("id", id) 
+0

나는 두 가지 가능성이 있다고 생각합니다. 같은 방법으로 많은 다른 명명 된 쿼리를 수행했습니다. –

0

난 당신이 id 매개 변수를 전달할 수 없다고 생각; 테스트하기 :

@NamedQuery(name = "obtenerAreaPorId", query = "SELECT a FROM AreaFuncional a where a.id=:id") }) 

getSessionFactory().getNamedQuery("obtenerAreaPorId").setParameter("id",myId) 
관련 문제