java - org.hibernate.MappingException : Could not determine type for custom object type

标签 java hibernate spring-boot spring-data-jpa spring-boot-jpa

Spring 启动2.3

实体Cart有许多实体CartItem

这是我的模型:

@Entity
public class Cart {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @OneToMany(mappedBy = "cart", fetch = FetchType.EAGER,
            cascade = CascadeType.ALL)
    private Set<CartItem> cartItems;


@Entity
public class CartItem {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private Product product;
    private int quantity;
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "cart_id", nullable = false)
    private Cart cart;


@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @NotNull
    private String name;
    @ElementCollection
    private Set<String> images;

但是当我运行应用程序时出现错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cartRepository' defined in com.myproject.eshop_orders.repo.CartRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.myproject.eshop_orders.api.model.Product, at table: cart_item, for columns: [org.hibernate.mapping.Column(product)]

最佳答案

您在 CartItem 中使用 Product,但未定义 @OneToOne 等任何关系。因此,JPA 将 product 视为 CartItem 表中的列,并且无法确定数据库列的 product 的兼容类型。这就是为什么错误指出

Could not determine type for: com.myproject.eshop_orders.api.model.Product, 
at table: cart_item, for columns: [org.hibernate.mapping.Column(product)

可能您与 CartItem 表中的 Product 存在 @OneToOne 关系。

@OneToOne
private Product product;

关于java - org.hibernate.MappingException : Could not determine type for custom object type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61131699/

相关文章:

java - MySQL 连接消失 Java

java调用Web服务时出现错误403

Hibernate 在 Tomcat/lib 中看不到 JDBC 驱动程序,但仅在 WEB-INF/lib 中

java - 是否可以在 hibernate 状态下将表视为只读?

java - 如何在没有 get/set 方法的情况下获取对象 JSON

java - 如何在 Apache Lucene 中将页面转换为 SearchResult

java - 计算并删除字符串列表中的重复项,但将重复项添加到键中(任何惯用的 java8 式方式?)

java - 创建类路径资源 [jpaContext.xml] 中定义的名称为 'entityManagerFactory' 的 bean 时出错

java - Spring Security 配置 - HttpSecurity 与 WebSecurity

java - Spring Boot 和 Camel Mail SSL 配置