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.