spring 错误原因:java.lang.非法参数异常:在此ManagedType []上找不到具有给定名称[]的属性

hgtggwj0  于 2022-11-21  发布在  Spring
关注(0)|答案(1)|浏览(168)

所以我有这个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()
}
5n0oy7gb

5n0oy7gb1#

您正在将@MappedSuperclassDataEntity一起使用,但应该使用@Inheritance
@MappedSuperclas s是一个注解,告诉JPA所有子类都必须具有继承的属性,但它与JPA继承无关。
要在JPA中使用继承,必须使用@Inheritance
定义如何将继承树存储到数据库的策略有三种:单个表、联接表和每个类的表。

  • 单一表格(预设值):同一表上的所有类
  • JOINED:每个类的表,即使它们是抽象的
  • 每个类别的表格:仅限非抽象类的表

相关问题