我正在使用mysqma.querydsl版本4.1.4编写查询,我需要投影直接属性(attrs)和嵌套属性以及这些嵌套属性的一些attrs,而不是所有attrs。
我有两个实体:
@Entity
@Table(name = "p_libro_visado", schema = "lab", catalog = "catastro")
public class PLibroVisadoEntity {
private long id;
private int cantLamina;
private Integer idLibro;
private TipovisadoEntity tipovisado;
@Id
@Column(name = "id")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "cant_lamina")
public int getCantLamina() {
return cantLamina;
}
public void setCantLamina(int cantLamina) {
this.cantLamina = cantLamina;
}
@Basic
@Column(name = "id_libro")
public Integer getIdLibro() {
return idLibro;
}
public void setIdLibro(Integer idLibro) {
this.idLibro = idLibro;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_tipo_visado", referencedColumnName = "id", nullable = false)
public TipovisadoEntity getTipovisado() {
return tipovisado;
}
public void setTipovisado(TipovisadoEntity tipovisadoByIdTipoVisado) {
this.tipovisado = tipovisadoByIdTipoVisado;
}
}
和
@Entity
@Table(name = "tipovisado", schema = "lab", catalog = "catastro")
public class TipovisadoEntity {
private long id;
private String abreviacion;
private String nombre;
private Integer orden;
private Collection<PLibroVisadoEntity> libroVisados;
@Id
@Column(name = "id")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "abreviacion")
public String getAbreviacion() {
return abreviacion;
}
public void setAbreviacion(String abreviacion) {
this.abreviacion = abreviacion;
}
@Basic
@Column(name = "nombre")
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
@Basic
@Column(name = "orden")
public Integer getOrden() {
return orden;
}
public void setOrden(Integer orden) {
this.orden = orden;
}
@OneToMany(mappedBy = "tipovisado")
public Collection<PLibroVisadoEntity> getpLibroVisados() {
return libroVisados;
}
public void setpLibroVisados(Collection<PLibroVisadoEntity> pLibroVisadosById) {
this.libroVisados = pLibroVisadosById;
}
}
我的疑问是:
QPLibroVisadoEntity e = QPLibroVisadoEntity.pLibroVisadoEntity;
QTipovisadoEntity t = QTipovisadoEntity.tipovisadoEntity;
JPAQuery<?> query = factory.query();
return query.select( Projections.bean(PLibroVisadoEntity.class, e.tipovisado, e.idLibro,
Projections.bean(TipovisadoEntity.class, e.tipovisado.nombre)
)).from(e).fetch().stream();
但是,这引发了一个例外:
这是一个例外:
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.319 sec <<< FAILURE!
testFindAll(ar.gov.sf.commons.repository.JpaAsyncRepositoryTest) Time elapsed: 2.673 sec <<< FAILURE!
java.lang.IllegalArgumentException: Unsupported expression new TipovisadoEntity(pLibroVisadoEntity.tipovisado.nombre)
at com.querydsl.core.types.QBean.createBindings(QBean.java:75)
at com.querydsl.core.types.QBean.<init>(QBean.java:129)
at com.querydsl.core.types.QBean.<init>(QBean.java:118)
at com.querydsl.core.types.Projections.bean(Projections.java:68)
at ar.gov.sf.commons.repository.JpaAsyncRepository.findAll(JpaAsyncRepository.java:112)
at ar.gov.sf.commons.repository.JpaAsyncRepositoryTest.testFindAll(JpaAsyncRepositoryTest.java:48)
关于如何修复此代码的任何想法?
感谢!!!