java Spring Data JPA / Hibernate“无法找到具有给定名称的属性”

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

Spring Data JPA / Hibernate "Unable to locate Attribute with the given name"

javaspringhibernatespring-mvckotlin

提问by Heady

We have a problem with a Spring Web Application and Hibernate. It is written in Kotlin. We have an abstract Entity

我们有一个 Spring Web 应用程序和 Hibernate 的问题。它是用 Kotlin 编写的。我们有一个抽象实体

@Inheritance(strategy = InheritanceType.JOINED)
abstract @Entity class ContactLogEntry protected constructor() {

    @GeneratedValue @Id val id: Long = 0


    @ManyToOne
    @JoinColumn
    protected lateinit var _contact: AbstractContact
    open val contact: AbstractContact? get() = _contact

  @ManyToOne
    protected var _user: User? = null
    open val user: User? get() = _user

And some of those:

其中一些:

@Entity class MailLogEntry() : ContactLogEntry() {


    override var contact: Lead
        get() = super.contact as Lead
        set(value) {
            super._contact = value
        }

 override var user: Telephonist
        get() = super.user as Telephonist
        private set(value) {
            super._user = value
        }

Note that "Lead" inherits directly from "AbstractContact". The Problem is with the property contact. The User Property, where Telephonist inherits directly from User, works fine.

请注意,“Lead”直接继承自“AbstractContact”。问题出在财产上contact。Telephonist 直接从 User 继承的 User 属性工作正常。

We get Unable to locate Attribute with the the given name [contact] on this ManagedType (PATH to ContactLogEntry)

我们得到 Unable to locate Attribute with the the given name [contact] on this ManagedType (PATH to ContactLogEntry)

We did it the same way before, where it works. Really no clue whats wrong.

我们以前用同样的方法做的,在那里它起作用。真的不知道出了什么问题。

回答by Manos Batsis

In my case, using pure java, the cause was that an abstract @MappedSuperClass defined abstract getter/setter methods according to an interface, but did not define the actual member field.

在我的情况下,使用纯 Java,原因是抽象的 @MappedSuperClass 根据接口定义了抽象的 getter/setter 方法,但没有定义实际的成员字段。

The error went away after removing the getter/setter methods from the abstract class, there was no need for them after all. HTH

从抽象类中删除 getter/setter 方法后错误消失了,毕竟不需要它们。HTH

回答by Julio Villane

A wider possibility is that you refactorized an attribute name but with an inconsistency in a:

更广泛的可能性是您重构了一个属性名称,但在以下内容中存在不一致:

  • getter and/or setter
  • @Query
  • @NamedEntityGraphs
  • @NamedQuery
  • getter 和/或 setter
  • @询问
  • @NamedEntityGraphs
  • @NamedQuery