首页 > 解决方案 > java.lang.IllegalArgumentException:不是托管类型:classTestEntity

问题描述

我正在尝试使用 Spring JPA 从数据库中的两个表中获取数据:

在此处输入图像描述

并且有一个例外:

nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.entity.Product

产品实体是:

@Entity
@Table(name = "PRODUCT")
@Getter
@Setter
public class Product {
    @Id
    @Column(name = "id", unique = true, nullable = false)
    private int id;

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "productService", fetch = FetchType.EAGER)
    private List<ProductService> productService;

    @ManyToOne(targetEntity = ProductStatus.class, optional = false)
    @JoinColumn(name = "PRODUCT_STATUS_ID")
    private ProductStatus status;
}

存储库是

public interface CaKeyRepository extends JpaRepository<Product, Integer> {

}

尝试使用 findAll() 加载;

public HashMap<String, Key> loadProduct() throws Exception {
    List<Product> products = caKeyRepository.findAll();

我也使用 WildFly 12 应用程序服务器。

使用依赖项实现项目:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <exclusions>
            <exclusion>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>
    <dependency>
        <groupId>com.atomikos</groupId>
        <artifactId>transactions-hibernate4</artifactId>
    </dependency>

都提供

标签: javahibernatespring-data-jpawildfly

解决方案


看起来你的实体没有被休眠扫描。

如果您通过 persistence.xml 配置了 JPA/Hibernate: <class>com.entity.Product</class>

如果通过 Spring-ORM,那么在您的LocalContainerEntityManagerFactoryBean中设置packagesToScan方法中的包。


推荐阅读