org.hibernate.MappingException: 无法确定自定义对象类型的类型
org.hibernate.MappingException: Could not determine type for custom object type
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
table 中的列,并且无法确定数据库列的 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
table 中与 Product
有 @OneToOne
关系。
@OneToOne
private Product product;
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
table 中的列,并且无法确定数据库列的 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
table 中与 Product
有 @OneToOne
关系。
@OneToOne
private Product product;