Hibernate中的ResultTransformer返回null [英] ResultTransformer in Hibernate return null

查看:174
本文介绍了Hibernate中的ResultTransformer返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ResultTransformer仅从实体中选择特定属性,只是我不需要实体中的所有属性。
但是我面临的问题是当一个房产是一对多的时候。
这是一个简单的例子。

$ p $ @Entity
@Table(name =STUDENT)
public class Student
{

private long studentId;
private String studentName;
私人列表<手机> studentPhoneNumbers = new ArrayList< Phone>();

@Id
@GeneratedValue
@Column(name =STUDENT_ID)
public long getStudentId()
{
return this。学生卡;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name =STUDENT_PHONE,joinColumns = {@JoinColumn(name =STUDENT_ID)},inverseJoinColumns = {@JoinColumn (name =PHONE_ID)})
public List< Phone> getStudentPhoneNumbers()
{
返回this.studentPhoneNumbers;
}
@Column(name =STUDENT_NAME,nullable = false,length = 100)
public String getStudentName()
{
return this.studentName;



$ b

这是ResultTransformer用于存储选定属性的类。

  public class StudentDTO 
{
private long m_studentId;
私人列表<手机> m_studentPhoneNumbers = new ArrayList< Phone>();
..
构造函数和getter和setter ..

最后,Criteria代码

  Criteria criteria = session.createCriteria(Student.class)
.setProjection(Projections.projectionList()
.add(Projections.property(studentId),m_studentId)
.add(Projections.property(studentPhoneNumbers),m_studentPhoneNumbers))
.setResultTransformer(Transformers.aliasToBean( StudentDTO.class));


List list = criteria.list();
StudentDTO p =(StudentDTO)list.get(0);

所以,当我得到StudentDTO对象后,只有studenId可用,studentPhoneNumber为null ..
是否表示ResultTransformer不适用于任何关系?或者我的方式是错误的
有什么建议吗?
$ b $ p

谢谢

解决方案

我之前遇到过这个问题。休眠期间的变压器通常会导致沮丧。您可以按照建议进行手动分配,也可以使用hibernate将DTO类映射到同一个表。只需将StudentDTO类的注释添加为Student类,然后像正常一样加载它。所以学生DTO会是:
$ b $ pre $ @Entity
@Table(name =STUDENT)

public class StudentDTO
{

private long studentId;
private String studentName;
私人列表<手机> studentPhoneNumbers = new ArrayList< Phone>();

@Id
@GeneratedValue
@Column(name =STUDENT_ID)
public long getStudentId()
{
return this。学生卡;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name =STUDENT_PHONE,joinColumns = {@JoinColumn(name =STUDENT_ID)},inverseJoinColumns = {@JoinColumn (name =PHONE_ID)})
public List< Phone> getStudentPhoneNumbers()
{
返回this.studentPhoneNumbers;
}

只留下你不想加载的内容。


I'm using ResultTransformer to select only particular properties from entity, just i don't need all properties from entity. But the problem i faced is when a property is "one-to-many". Here is a simple example.

@Entity
@Table(name = "STUDENT")
public class Student
{

private long studentId;
private String studentName;
private List<Phone> studentPhoneNumbers = new ArrayList<Phone>();

@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId()
{
  return this.studentId;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_PHONE", joinColumns = {@JoinColumn(name = "STUDENT_ID")}, inverseJoinColumns = {@JoinColumn(name = "PHONE_ID")})
public List<Phone> getStudentPhoneNumbers()
{
  return this.studentPhoneNumbers;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName()
{
  return this.studentName;
}

Here is the class used by ResultTransformer for storing the selected properties.

public class StudentDTO
{
private long m_studentId;
private List<Phone> m_studentPhoneNumbers = new ArrayList<Phone>();
.. 
constructors and getters and setters..

And finally the Criteria code

 Criteria criteria = session.createCriteria(Student.class)
    .setProjection(Projections.projectionList()
      .add(Projections.property("studentId"), "m_studentId")
      .add(Projections.property("studentPhoneNumbers"), "m_studentPhoneNumbers"))
    .setResultTransformer(Transformers.aliasToBean(StudentDTO.class));


  List list = criteria.list();
  StudentDTO p = (StudentDTO) list.get(0);

So, after i get StudentDTO object , only studenId is available, studentPhoneNumber is null .. Does it mean ResultTransformer does not work with any relationships ? or my way is wrong Any suggestions?

Thanks

解决方案

I have run into this problem before. The transformers in hibernate generally lead to frustration. You can do a manual assignment as is suggested, or you can map the DTO class to the same table using hibernate. Just add the same annotation to your StudentDTO class as the Student class and then load it like normal. So student DTO would be:

@Entity
@Table(name = "STUDENT")

public class StudentDTO
{

private long studentId;
private String studentName;
private List<Phone> studentPhoneNumbers = new ArrayList<Phone>();

@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId()
{
  return this.studentId;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_PHONE", joinColumns = {@JoinColumn(name = "STUDENT_ID")}, inverseJoinColumns = {@JoinColumn(name = "PHONE_ID")})
public List<Phone> getStudentPhoneNumbers()
{
  return this.studentPhoneNumbers;
}

Just leave out what you do not want to load.

这篇关于Hibernate中的ResultTransformer返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆