如何在不与单个表

时间:2016-09-20 09:30:57

标签: sql-server hibernate nativequery hibernate-native-query

我有一个旧的SQL Server数据库,他们给了我一个查询,它在几个表之间进行了一些连接。

  

SELECT TipoDia,ServBus,Instante,Evento,[VistaHorariosActivos] .Linea,Coche,[VistaHorariosActivos] .Sublinea,[VistaHorariosActivos] .Seccion,Punto,Tipo,Viaje,Operador,Fecha,[VistaHorariosActivos] .Macro,Ruta, OrdenSeccion来自[SAEBase]。[dbo]。[VistaHorariosActivos] INNER JOIN [SAEBase]。[dbo]。[LineaSublineaSeccionOrden] ON [VistaHorariosActivos] .Macro = LineaSublineaSeccionOrden.Macro AND [VistaHorariosActivos] .Linea = LineaSublineaSeccionOrden.Linea AND [VistaHorariosActivos] .Sublinea = LineaSublineaSeccionOrden.Sublinea AND [VistaHorariosActivos] .Seccion = LineaSublineaSeccionOrden.Seccion WHERE Fecha>:fecha AND VistaHorariosActivos.Macro =:macro AND VistaHorariosActivos.Linea =:linea AND(Evento = 1 OR Evento = 5)

我想将这些结果映射到java类(TripDTO):我已经使用正确的数据类型映射了每个列。

问题在于我不知道TripDTO课程是否应该有@Entity和@table注释。实际上,该查询与单个表没有关系。

有人可以帮我定义那个班级吗?

您在这里是代码:

package model;

import java.io.Serializable;
import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

@NamedNativeQueries({
    @NamedNativeQuery(
        name = "getTripDtos",
        query = "<<the query>>",
        resultClass = TripDTO.class
    )   
})
@Entity
@Table(name = "???")
public class TripDTO implements Serializable {

    /*
     * Atributos 
     */
    private static final long serialVersionUID = 5719572417390931185L;

    @Id
    @Column(name = "Macro", insertable = false, updatable = false)
    @Type(type = "usertype.IdMacroUserType")
    private Short macro;
    @Id
    @Column(name = "Linea", insertable = false, updatable = false)
    private Short linea;
    @Id
    @Column(name = "Sublinea", insertable = false, updatable = false)
    private Byte sublinea;
    @Id
    @Column(name = "Seccion", insertable = false, updatable = false)
    @Type(type = "usertype.IdSeccionUserType")
    private Integer seccion;
    @Id
    @Column(name = "Punto", insertable = false, updatable = false)
    private Integer punto;
    @Id
    @Column(name = "Instante", insertable = false, updatable = false)
    private Integer instante;

    @Column(name = "TipoDia", insertable = false, updatable = false)
    @Type(type = "usertype.TipoDiaUserType")
    private String tipoDia;
    @Type(type = "usertype.ServicioBusUserType")
    @Column(name = "ServBus", insertable = false, updatable = false)
    private Integer servBus;
    @Column(name = "Evento", insertable = false, updatable = false)
    @Type(type = "usertype.TipoEventoUserType")
    private Short evento;
    @Column(name = "Coche", insertable = false, updatable = false)
    private Byte coche;
    @Column(name = "Tipo", insertable = false, updatable = false)
    @Type(type = "usertype.TipoNodoUserType")
    private Short tipo;
    @Column(name = "Viaje", insertable = false, updatable = false)
    private Integer viaje;
    @Column(name = "Operador", insertable = false, updatable = false)
    private String operador;
    @Column(name = "Fecha", insertable = false, updatable = false)
    private LocalDateTime fecha;
    @Column(name = "Ruta", insertable = false, updatable = false)
    private Integer ruta;
    @Column(name = "OrdenSeccion", insertable = false, updatable = false)
    private Integer ordenSeccion;
    ...

1 个答案:

答案 0 :(得分:1)

如果您有一个命名查询并且想要将其映射到结果类(始终是实体),则必须使用@Entity注释对其进行注释。 你没有义务用@Table来注释它,但是hibernate(一般来说是jpa)需要知道这个类是一个实体。您也不必使用@Column注释您的字段,因为您只是使用dto而不是持久表行(您的实体不引用数据库中的特定表)。

完成注释后,您可以使用resultClass属性来定义dto @Entity带注释的类。 此类必须包含与查询的输出列对应的所有字段。因此,例如,如果一列是Orden_Seccion,那么这应该是dto类的字段名称。

你也可以使用resultMappings但我认为,正如我从你的查询中看到的那样,resultClass对你的情况来说是好的和干净的。