本文介绍了Spring Data错误原因:org.hibernate.QueryException:无法解析属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在启动时引发错误

My application throws the error while starting

原因:org.hibernate.QueryException:无法解析属性:org.mycompany.system.model.Code的版本

Caused by: org.hibernate.QueryException: could not resolve property: version of: org.mycompany.system.model.Code

我的存储库方法

@Query("SELECT NEW org.mycompany.util.CodeVO(c.id,  c.description,c.version) FROM Code c where  c.groupName = :groupName")
    List<CodeVO> getByGroupName(@Param("groupName") String groupName);

基类

public abstract class ModelObject<ID extends Serializable> implements Serializable {

    @Column(nullable = false, length = 100)
    private String createdBy;

    @Column(nullable = false)
    private LocalDateTime createdTime = LocalDateTime.now();

    @Version
    private int version;

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

}

子类

 @Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "groupName", "description" }) })
public class Code extends ModelObject<Long> {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    @NotBlank
    private String description;

    public Long getId() {
        return id;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

不可能引用超类属性吗?

is it not possible to refer superclass properties?

推荐答案

必须使用@MappedSuperclass注释ModelObject,JPA中没有自动继承

ModelObject must be annotated with @MappedSuperclass there is no automatic inheritance in JPA

根据规格:

MappedSuperclass注释指定一个类,该类的映射信息适用于从其继承的实体.映射的超类没有为其定义单独的表.

The MappedSuperclass annotation designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.

这篇关于Spring Data错误原因:org.hibernate.QueryException:无法解析属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:28