Java 无法通过反射 getter 获取字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18210365/
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-11 23:20:52  来源:igfitidea点击:

Could not get a field value by reflection getter

javahibernatehibernate-criteria

提问by dtrunk

I'm trying to filter a result set by a foreign key:

我正在尝试通过外键过滤结果集:

createCriteria(Person.class).add(Restrictions.ne("position", 1L)).list()

But getting this exception: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.example.model.Position.id

但是得到这个例外: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.example.model.Position.id

Here are the necessary JPA entities (trimmed down to the necessary fields):

以下是必要的 JPA 实体(缩减为必要的字段):

@Entity
@Table
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    @JoinColumn(nullable = false)
    @ForeignKey(name = "person_position_fkey")
    private Position position;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Position getPosition() {
        return position;
    }

    public void setPosition(Position position) {
        this.position = position;
    }
}

@Entity
@Table
public class Position {
    @Id
    @GeneratedValue
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

采纳答案by Yurii Shylov

Try Restrictions.ne("position.id", 1L)

尝试 Restrictions.ne("position.id", 1L)