Java hibernate条件投影获取联接表的列
我有以下几点:Java hibernate条件投影获取联接表的列,java,mysql,hibernate,jpa,criteria,Java,Mysql,Hibernate,Jpa,Criteria,我有以下几点: public class User { private Integer idUser; private String user; private String password; private Perfil perfil; Id @GeneratedValue(strategy = IDENTITY) @Column(name = "idUser", unique = true, nullable = false)
public class User
{
private Integer idUser;
private String user;
private String password;
private Perfil perfil;
Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "idUser", unique = true, nullable = false)
public Integer getIdUser()
{
return this.idUser;
}
public void setIdUser(Integer idUser)
{
this.idUser = idUser;
}
@Column(name = "user", nullable = false)
public String getUser()
{
return this.user;
}
public void setUser(String user)
{
this.user = user;
}
@Column(name = "password", nullable = false)
public String getPassword()
{
return this.password;
}
public void setPassword(String password)
{
this.password = password;
}
@OneToOne
@JoinColumn(name = "idPerfil", nullable = false)
public Perfil getIdPerfil()
{
return idPerfil;
}
public void setIdPerfil(Perfil idPerfil)
{
this.idPerfil = idPerfil;
}
}
public class Perfil
{
private Integer idPerfil;
private String perfil;
private String description;
Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "idPerfil", unique = true, nullable = false)
public Integer getIdPerfil()
{
return this.idPerfil;
}
public void setIdPerfil(Integer idPerfil)
{
this.idPerfil = idPerfil;
}
@Column(name = "description", nullable = false)
public String getDescription()
{
return this.description;
}
public void setDescription(String description)
{
this.description = description;
}
}
我只想获得idUser、user和idPerfil;我使用的是:
Session session = HibernateUtil.openSession();
try
{
Criteria criteria = session.createCriteria(User.class);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("idUser"), "idUser");
proList.add(Projections.property("user"), "user");
proList.add(Projections.property("idPerfil"), "idPerfil");
criteria.setProjection(proList);
criteria.setFetchMode("idPerfil.idPerfil", org.hibernate.FetchMode.JOIN);
criteria.setResultTransformer(Transformers.aliasToBean(User.class));
List<User> list = criteria.list();
return list;
}
catch (Exception e)
{
throw e;
}
finally
{
session.close();
}
会话会话=HibernateUtil.openSession();
尝试
{
条件=session.createCriteria(User.class);
ProjectionList proList=Projections.ProjectionList();
proList.add(Projections.property(“idUser”),“idUser”);
proList.add(Projections.property(“用户”),“用户”);
proList.add(Projections.property(“idPerfil”),“idPerfil”);
标准:setProjection(proList);
setFetchMode(“idPerfil.idPerfil”,org.hibernate.FetchMode.JOIN);
条件.setResultTransformer(Transformers.aliasToBean(User.class));
List=criteria.List();
退货清单;
}
捕获(例外e)
{
投掷e;
}
最后
{
session.close();
}
对于实体用户,我可以得到specify属性的值,但是从idPerfil我可以得到所有属性的值,我只需要idPerfil。如何进行查询?尝试为idPerfil添加别名:
criteria.createAlias("idPerfil", "idp");
然后在投影中引用它:
proList.add(Projections.property("idp.idPerfil"), "idPerfil");
然后拆下变压器:
criteria.setResultTransformer(Transformers.aliasToBean(User.class));
并将结果更改为:
List=criteria.List()
列表中的每个元素都将是对象[]数组:
for(Object data : list) {
Object[] projection = (Object[]) data;
Integer idUser = projection[0];
String user = projection[1];
Integer idPerfil= projection[3];
...
}
我试过了,但当我使用criteria.setResultTransformer(Transformers.aliasToBean(User.class));,时出错;。org.hibernate.property.BasicPropertyAccessor$BasicSetter.set HHH000091:预期类型:com.entity.Perfil,实际值:java.lang.Integer错误org.hibernate.PropertyAccessException:调用com.entity.User.idPerfil org.hibernate.PropertyAccessException的setter时发生IllegalArgumentException:调用com.User.idPerfil的setter时发生IllegalArgumentException。如果我不使用criteria.setResultTransformer(Transformers.aliasToBean(User.class));idPerfil看起来像Integer,不像objectok谢谢,但没有办法将其转换为用户?因为我更喜欢像实体一样工作。当只选择实体的一部分时,投影很有用。还可以将选定列映射到DTO。对于获取实体,您不需要投影