Я работаю в проекте Java Vaadin с весной и спящим. У меня есть следующий Entity в моем проекте:Hibernate сделать правильный запрос базы данных, но вернуть нулевой объект
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>();
}
....
Тогда у меня есть следующий запрос:
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=?
Если я выполняю оператор mysql в консоли, я получаю правильный ответ, но Hibernate возвращает нулевой объект.
Я не могу получить ошибку. Может ли кто-нибудь помочь мне с этим?
Я думаю, что две возможности в порядке. У меня есть много других именованных запросов, выполненных таким же образом –