org.hibernate.MappingException: 无法确定自定义对象类型的类型

huangapple go评论64阅读模式
英文:

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

问题

Spring Boot 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: 在类路径资源 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class] 中定义的名为 'mvcConversionService' 的 Bean 创建失败:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.format.support.FormattingConversionService]:工厂方法 'mvcConversionService' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 com.myproject.eshop_orders.repo.CartRepository 中定义的名为 'cartRepository' 的 Bean 创建失败,该 Bean 在声明的 @EnableJpaRepositories 上定义:在设置 Bean 属性 'mappingContext' 时无法解析对 Bean 'jpaMappingContext' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:名为 'jpaMappingContext' 的 Bean 创建失败:初始化方法调用失败;嵌套异常是 javax.persistence.PersistenceException:[PersistenceUnit:default] 无法构建 Hibernate SessionFactory;嵌套异常是 org.hibernate.MappingException:无法确定类型:com.myproject.eshop_orders.api.model.Product,在表格:cart_item,在列上:[org.hibernate.mapping.Column(product)]
英文:

Spring boot 2.3

Entity Cart has many entites CartItem.

So here my models:

@Entity
public class Cart {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @OneToMany(mappedBy = &quot;cart&quot;, fetch = FetchType.EAGER,
            cascade = CascadeType.ALL)
    private Set&lt;CartItem&gt; 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 = &quot;cart_id&quot;, nullable = false)
    private Cart cart;


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

But when I run application I get error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;mvcConversionService&#39; 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 &#39;mvcConversionService&#39; threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;cartRepository&#39; defined in com.myproject.eshop_orders.repo.CartRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean &#39;jpaMappingContext&#39; while setting bean property &#39;mappingContext&#39;; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;jpaMappingContext&#39;: 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)]

答案1

得分: 1

你在CartItem中使用了Product,但没有像@OneToOne这样的方式定义任何关联关系。因此,JPA将product视为CartItem表中的列,并且无法确定用于数据库列的product的兼容类型。这就是为什么错误显示为:

无法确定类型:com.myproject.eshop_orders.api.model.Product,
在表格:cart_item中,对于列:[org.hibernate.mapping.Column(product)]

可能在CartItem表中,你应该有与Product@OneToOne关联关系:

@OneToOne
private Product product;
英文:

You are using Product in CartItem without defining any relation like @OneToOne. So JPA treats product as column in CartItem table and can not determine compatible type for product for database column. That's why error states that

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

May be you have @OneToOne relation with Product in CartItem table.

@OneToOne
private Product product;

huangapple
  • 本文由 发表于 2020年4月10日 07:14:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61131699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定