英文:
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 = "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;
But when I run application I get error:
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)]
答案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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论