原因:java.lang.IllegalArgumentException:无法在此ManagedType []上找到具有给定名称[]的Attribute。

时间:2019-06-03 19:28:06

标签: spring hibernate spring-boot

所以我有这个kotlin项目:

package com.example.demo

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.repository.CrudRepository
import java.util.*
import javax.persistence.*

@MappedSuperclass
abstract class ContainerEntity<TData : DataEntity> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    @OneToOne(fetch = FetchType.EAGER, cascade = [CascadeType.ALL], optional = false)
    lateinit var data: TData
}


@MappedSuperclass
abstract class DataEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    val created = Date()
}

@Entity
class Car : ContainerEntity<CarData>()

@Entity
class CarData : DataEntity() {
    var color = "red"
}

@Entity
class Apple : ContainerEntity<AppleData>()

@Entity
class AppleData : DataEntity() {
    var age = 32
}

/**
 * WORKS
 */

interface CarRepository : CrudRepository<Car, Int> {
    fun findByDataColor(color: String): Car?
}


/**
 * FAILS
 */

interface AppleRepository : CrudRepository<Apple, Int> {
    fun findByDataAge(age: Int): Apple?
}

获取此异常:

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [age] on this ManagedType [com.example.demo.DataEntity]
        at org.hibernate.metamodel.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:128) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at org.hibernate.metamodel.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:113) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at org.hibernate.metamodel.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:111) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at org.hibernate.query.criteria.internal.path.SingularAttributePath.locateAttributeInternal(SingularAttributePath.java:71) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at org.hibernate.query.criteria.internal.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:204) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:177) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:673) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:606) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:578) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:381) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:304) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:209) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:122) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:57) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:119) ~[spring-data-commons-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:95) ~[spring-data-commons-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:81) ~[spring-data-commons-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:147) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:270) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:79) ~[spring-data-jpa-2.1.8.RELEASE.jar:2.1.8.RELEASE]
        ... 98 common frames omitted

有一个小型演示项目:https://github.com/TomGrill/spring-hibernate-bug

根据要求:

//@MappedSuperclass
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
abstract class ContainerEntity<TData : DataEntity> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    @OneToOne(fetch = FetchType.EAGER, cascade = [CascadeType.ALL], optional = false)
    lateinit var data: TData
}


//@MappedSuperclass
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
abstract class DataEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    val created = Date()
}

1 个答案:

答案 0 :(得分:0)

您将@MappedSuperclass与DataEntity一起使用,但应使用@Inheritance。

@MappedSuperclass是告诉JPA的注释,所有子类必须具有继承的属性,但与JPA继承无关。

要在JPA中使用继承,必须使用@Inheritance。

有三种策略定义了如何将initritit树存储到数据库中:SINGLE_TABLE,JOINED和TABLE_PER_CLASS。

  • SINGLE_TABLE(默认):同一表上的所有类
  • 已加入:即使每个类都是抽象的,每个类都有一个表
  • TABLE_PER_CLASS:仅非抽象类的表