Java org.hibernate.PropertyNotFoundException: 找不到 0 的设置器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18744512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:39:39  来源:igfitidea点击:

org.hibernate.PropertyNotFoundException: Could not find setter for 0

javahibernatehql

提问by masT

I'm fetching only selected attributes from a table using HQL, maintaining a list of non-entity class objects. For Eg. My entity class:

我只使用 HQL 从表中获取选定的属性,维护非实体类对象的列表。对于例如。我的实体类:

@Entity
@Table(name="STUDENT")
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @Column(name="NAME", columnDefinition="TEXT", length="60", nullable = false)
    private String name;

    @ManyToOne
    @JoinColumn(name = "Dept_id", nullable = false)
    private Department department;

    // Other fields...
    // Getter-Setters
}

and non-persistent DTO class is having only fewer class members (say, name only):

和非持久性 DTO 类只有较少的类成员(例如,仅名称):

public class StudentDTO {
    private String name;
    // Getter-Setter for name
}

Now using

现在使用

public List<StudentDTO> getStudents(Long deptId) {
    List<StudentDTO> students;

    Query query = session.createQuery("select student.name " +
            "from Student as student " +
            "where Dept_id =?").setResultTransformer(new AliasToBeanResultTransformer(StudentDTO.class));
    query.setString(0, Long.toString(deptId));

    students = CommonUtil.castList(StudentDTO.class, query.list()); 
    return students;
}

where castList converts any collection into ArrayList.

其中 castList 将任何集合转换为 ArrayList。

public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
            List<T> resultList = new ArrayList<T>(c.size());
            for(Object o: c)
              resultList.add(clazz.cast(o));
            return resultList;
        }

throws org.hibernate.PropertyNotFoundException: Could not find setter for 0 on class ../StudentDTO

抛出org.hibernate.PropertyNotFoundException:在类 ../StudentDTO 上找不到 0 的设置器

Referring Hibernate exception PropertyNotFoundException when using Transformer, I changed my query to "select student.id as id,...", having Long idinside StudentDTObut that throw same exception, saying setter not found for 1.

在使用 Transformer 时引用Hibernate 异常 PropertyNotFoundException,我将查询更改为"select student.id as id,...",有Long id内部StudentDTO但抛出相同的异常,说找不到 1 的 setter。

Getter/Setters are given for each property. Please suggest changes!

每个属性都给出了 Getter/Setter。请提出修改意见!

采纳答案by Debojit Saikia

As you are using AliasToBeanResultTransformer, you have to declare proper aliasnames in your query. The class name itself says that it will transform the results into your resultClassusing the aliasnames.

在使用时AliasToBeanResultTransformer,您必须aliasquery. 类名本身说它会将结果转换为您resultClass使用的alias名称。

The transformTuplemethod of AliasToBeanResultTransformerclass uses the aliasnames to find the setter methods of your resultClass (StudentDto):

class的transformTuple方法AliasToBeanResultTransformer使用alias名称来查找您的 setter 方法resultClass (StudentDto)

for (int i = 0; i < aliases.length; i++) {
                    String alias = aliases[i];
                    if(alias != null) {
                        setters[i] = propertyAccessor.getSetter(resultClass, alias);
                    }
                }

In order to make AliasToBeanResultTransformerwork properly you need to use proper alias names in your query. If the property name in your StudentDtoclass is name, you should use select student.name as name. Using select student.name as nwill again throw exception. The aliasnames that you are using in the queryshould be same as the property names in your DTO class.

为了AliasToBeanResultTransformer正常工作,您需要在query. 如果类中的属性名称StudentDtoname,则应使用select student.name as name. 使用select student.name as n将再次抛出异常。alias您在 中使用的名称query应与 DTO 类中的属性名称相同。

回答by M2E67

set alias for each field in select clause, these field is same as filed name in your persistence class which you set as transformer class in your query, for example:

为 select 子句中的每个字段设置别名,这些字段与您在查询中设置为转换器类的持久性类中的字段名称相同,例如:

 public mypersistenceclass()
    {
    // define constructor
    public mypersistenceclass(){}

       private String username;
    // define setter and getter

 }

  public userclass()
  {
  ...
     Query query = session.createQuery("select Users.username as username ...")
      .setResultTransformer(new AliasToBeanResultTransformer(mypersistenceclass.class));

  ...
  }

回答by jaskirat Singh

just write each column name as below : Select column_name as property_name , ...., from Class_name

只需写下每个列名: Select column_name as property_name , ...., from Class_name

eg: select student.name as namefrom Student

例如:从 Student 中选择 student.name作为名称