当前位置: 首页 > 知识库问答 >
问题:

导致:java.lang.IllegalArgumentExcema:无法找到此ManagedType[]上具有给定名称[]的属性

金宣
2023-03-14

所以我有这个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个答案

司寇阳朔
2023-03-14

您正在将@MappedSuperclassDataEntity一起使用,但您应该使用@heritation

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

要在JPA中使用继承,您必须使用@heritation

有三种策略定义如何将信息树存储到数据库:SINGLE_TABLE、JOINED和TABLE_PER_CLASS。

  • 单表(默认):同一表上的所有类
 类似资料: