简体   繁体   English

此 HQL 查询不适用于元素或成员,但本机查询可以

[英]This HQL Query doesn't work with elements nor member of, but native query is ok

I have a problem running this HQL query:我在运行此 HQL 查询时遇到问题:

@Query("select m from Mesa m where not exists (" +
      " from ValesMaster v " +
      " where v.cobrado = false " +
      " and v.id.fecha = :fecha " +
      " and m.id in elements(v.mesas)) " )
List<Mesa> getAvailableTables(@Param("fecha") Date fecha);

When I use this native query everything is OK:当我使用这个本机查询时,一切正常:

@Query(value = "SELECT m.* from mesas m where not exists (" +
      "SELECT DISTINCT unnest(mesas) AS umesas FROM vales_master vm " +
      "WHERE cobrado = false AND fecha = :fecha and m.id = any(vm.mesas))", nativeQuery = true)
   List<Mesa> getAvailableTables(@Param("fecha") Date fecha);

But I wanto to use HQL.但我想使用 HQL。 These are my entities Mesa:这些是我的实体 Mesa:

@Entity
@Table(name = "mesas")
public class Mesa implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @Column(unique = true, nullable = false)
   private Integer id;

   @Column(nullable = false)
   private Boolean activo;

   @Column(length = 16)
   private String nombre;

   @Column(nullable = false)
   private Boolean room;


   @ManyToOne
   @JoinColumn(name="id_station")
   private Estacion estacion;

   public Mesa() {
   }

   public Integer getId() {
      return this.id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public Boolean getActivo() {
      return this.activo;
   }

   public void setActivo(Boolean activo) {
      this.activo = activo;
   }

   public String getNombre() {
      return this.nombre;
   }

   public void setNombre(String nombre) {
      this.nombre = nombre;
   }

   public Boolean getRoom() {
      return this.room;
   }

   public void setRoom(Boolean room) {
      this.room = room;
   }

   public Estacion getEstacion() {
      return estacion;
   }

   public void setEstacion(Estacion estacion) {
      this.estacion = estacion;
   }
}

and ValesMaster:和 ValesMaster:

@TypeDef(
        name = "string-array",
        typeClass = StringArrayType.class
)
@TypeDef(
        name = "int-array",
        typeClass = IntArrayType.class
)
@Entity
@Table(name = "vales_master")
public class ValesMaster implements Serializable {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   private ValesMasterPK id;

   @Column(length = 128)
   private String cliente;

   @Column(nullable = false)
   private Boolean closed;

   @Column(nullable = false)
   private Boolean cobrado;

   @Column(name = "cod_vip_clients", length = 16)
   private String codVipClients;

   private Integer comensales;

   @Column(nullable = false, precision = 131089)
   @ColumnDefault("0.00")
   private BigDecimal descuento;

   @Column(name = "descuento_porciento", nullable = false, precision = 131089)
   @ColumnDefault("0.00")
   private BigDecimal descuentoPorciento;

   private Integer disco;

   private Integer foreigners;

   @Column(name = "id_cajero")
   private Integer idCajero;

   @Column(name = "id_dependiente")
   private Integer idDependiente;

   @Column(name = "id_payment")
   private Integer idPayment;

   @Column(nullable = false, precision = 131089)
   @ColumnDefault("0.00")
   private BigDecimal impuestos;

   @Column(name = "impuestos_porciento", nullable = false, precision = 131089)
   @ColumnDefault("0.00")
   private BigDecimal impuestosPorciento;

   @Column(name = "is_discount_percent", nullable = false)
   @ColumnDefault("true")
   private Boolean isDiscountPercent;

   @Column(name = "is_printed", nullable = false)
   @ColumnDefault("false")
   private Boolean isPrinted;

   @Type(type = "int-array")
   private int[] mesas;

   (...)

Any idea why this HQL Query give a "java.lang.NullPointerException" at build stage?知道为什么这个 HQL 查询会在构建阶段给出“java.lang.NullPointerException”吗? The problem is with this section:问题出在这一节:

" and m.id in elements(v.mesas) ) "

And I think maybe is because int-array type definition, but I really want to work with my HQL Query.我认为可能是因为 int-array 类型定义,但我真的很想使用我的 HQL 查询。 Any idea?任何的想法? member of doesn't work neither.成员也不起作用。 BTW excuse my poor english.顺便说一句,请原谅我糟糕的英语。 Is not my native language.不是我的母语。

Thanks in advance,提前致谢,

CarlosM卡洛斯

You are missing to end your @Query and your subquery.您缺少结束 @Query 和子查询。 Try this and lets see what will happen next.试试这个,让我们看看接下来会发生什么。

@Query("select m from Mesa m where not exists (" +
      " from ValesMaster v " +
      " where v.cobrado = false " +
      " and v.id.fecha = :fecha " +
      " and m.id in elements(v.mesas) )")
List<Mesa> getAvailableTables(@Param("fecha") Date fecha);

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM