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

标签 java hibernate hibernate-criteria

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

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

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

以下是必要的 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;
    }
}

最佳答案

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

关于java - 无法通过反射 getter 获取字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18210365/

相关文章:

javascript - 如果用户在 jsp 中填写一个字段(同时使用 spring mvc 和 hibernate),我如何自动填写其他表单字段

java - Hibernate 多对多标准投影

grails 创建查询 : wrong pagination with totalCount

java - 使用 hibernate 条件从一个字段中获取两个不同的计数,以获得不同的日期

java - 编写 IntelliJ Scala Plugin 插件?

java - Hibernate - 强制父类和子类之间的内部连接

java - 如何使用 JAXB 将 xml 字符串解码为对象

java - JBoss 缓存配置

java - Java 中带有 iText 的多行标题

java - 将图像序列化为android