0

I'm new to Spring and JPQL (and stack overflow too!), and I have a problem with a JPQL query, trying to use WHERE TYPE.

Mi classes are (sorry, the names are in Spanish):

@Entity
@Table(name = "ALQUILER")
public class Alquiler implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -4036725817142449041L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Include()
private Long id;

@ToString.Exclude
@ManyToOne
@JoinColumn(name = "ID_ALOJAMIENTO")
private Alojamiento alojamiento;
}

@Entity
@Table(name = "ALOJAMIENTO")
@Inheritance( strategy = InheritanceType.JOINED )

public abstract class Alojamiento implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = -4656346394072676126L;

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    @Getter@Setter
    @ToString.Exclude
    private Long id;
    
    @Column
    (name = "PLAZAS", nullable = false)
    private Integer plazas;
    
    @Column
    (name = "PRECIO", nullable = false)
    private Double precio;
}

@Entity
@Table(name = "APARTAMENTO")
@Builder
@EqualsAndHashCode(callSuper=false)
@ToString(callSuper = true)

public class Apartamento extends Alojamiento{
    

    /**
     * 
     */
    private static final long serialVersionUID = -3561089045391062665L;

    @Column(name = "DIRECCION", nullable = false, unique=true)
    private String direccion;
    
    @Column(name="ASCENSOR", nullable = false)
    private boolean ascensor;
}

@Entity
@Table(name = "CASA")
@Builder
@EqualsAndHashCode(callSuper=false)
@ToString(callSuper = true)

public class Casa extends Alojamiento{
    
    /**
     * 
     */
    private static final long serialVersionUID = 4745518992755935327L;

    @Column(name = "DIRECCION", nullable = false, unique=true)
    private String direccion;
    
    @Column(name = "METROS_FINCA", nullable = false)
    private Double metrosFinca;
}

I did not traduced the names of the classes to avoid mistakes, but to make this more readable for everybody, we can traduce them like:

  • Alquiler-> Rent
  • Alojamiento-> Accommodation
  • Casa -> House
  • Apartamento -> Flat

Basically, I have two types of accomodation, houses and flats, and people rent them. I want to count how many flats where rented in total.

My JPQL query:

SELECT COUNT(*) FROM Alquiler al WHERE TYPE (al.alojamiento)= Apartamento

The error:

Caused by: org.hibernate.QueryException: could not resolve property: Alojamiento of: es.seresco.alquileresCadati.model.Alquiler

I don't know if the error is because TYPE does not allow to to that, or it's because the query is incorrect.

Thank you. Regards.

  • It seems that `TYPE` expression only works on the entity you actually query. So in your example `TYPE(al)` would be valid. You could probably turn it around ` from Alojamiento al where TYPE(al) = Apartamento`, should return a list of all `Apartamento`s – XtremeBaumer May 18 '22 at 12:55
  • Thanks for your comment. Unfortunately, the solution didn't work. It returns "could not resolve property: class of: es.seresco.alquileresCadati.model.Alquiler", because Alquiler doesn't have any child class. – David Anton Alvarez May 18 '22 at 13:49
  • [Check this](https://stackoverflow.com/a/7808003/7109162). Either you make a check on the actual class, you try to use `TYPE(al.alojamiento) IN :param` – XtremeBaumer May 18 '22 at 13:57
  • Thanks for the reply. I'll try that as soon as possible. – David Anton Alvarez May 19 '22 at 08:19

0 Answers0