【问题标题】:java.lang.IllegalArgumentException: Not an managed type: classTestEntityjava.lang.IllegalArgumentException:不是托管类型:classTestEntity
【发布时间】:2019-04-28 05:39:07
【问题描述】:

我正在尝试使用 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>

全部提供

  • 春季版 - 4.3.10.RELEASE
  • Spring 数据版本 - 1.11.6.RELEASE
  • Hibernate-entitymanager 版本 - 4.3.11.Final
  • Hibernate-validator 版本 - 5.1.3.Final
  • Atomikos 版本 - 4.0.4

【问题讨论】:

  • 为什么Productid 类型是DECIMAL 在你的d 中?

标签: java hibernate spring-data-jpa wildfly


【解决方案1】:

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

如果您通过 persistence.xml 配置了 JPA/Hibernate:&lt;class&gt;com.entity.Product&lt;/class&gt;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 2019-12-30
    • 2019-11-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多