AnnotationException: 无法映射集合

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

AnnotationException: Unable to map collection

问题

Class Cart:

  1. @Data
  2. @Entity
  3. @IdClass(Cart.CartId.class)
  4. @Table(name = "cart_products")
  5. public class Cart {
  6. @NoArgsConstructor
  7. @AllArgsConstructor
  8. static class CartId implements Serializable {
  9. private Long orderIds;
  10. private Long drinkIds;
  11. }
  12. // Fields
  13. //
  14. @Id
  15. private Long orderIds;
  16. @Id
  17. private Long drinkIds;
  18. @ManyToOne
  19. @JoinColumn(name = "order_id", referencedColumnName = "id")
  20. private Order order;
  21. @ManyToOne
  22. @JoinColumn(name = "drink_id", referencedColumnName = "id")
  23. private Drink drink;
  24. private int count;
  25. }

Class Order:

  1. @Entity
  2. @Table(name = "pg_order")
  3. public class Order {
  4. // Fields
  5. //
  6. @Id
  7. @GeneratedValue
  8. private Long id;
  9. private String address;
  10. @Column(name = "phone_number")
  11. private String phoneNumber;
  12. @Temporal(TemporalType.TIMESTAMP)
  13. @Column(name = "date_order")
  14. private Date dateOrder;
  15. @Enumerated(EnumType.STRING)
  16. @Column(name = "order_status")
  17. private OrderStatus orderStatus;
  18. @Column(name = "total_cost")
  19. private BigDecimal totalCost;
  20. // Relationships
  21. //
  22. @ManyToOne
  23. @JoinColumn(name = "user_id")
  24. private User user;
  25. @OneToMany
  26. @JoinColumns({
  27. @JoinColumn(name = "order_id", referencedColumnName = "order_id"),
  28. @JoinColumn(name = "drink_id", referencedColumnName = "drink_id")
  29. })
  30. private Set<Cart> cart;
  31. }

ERRORS:

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countriesRepository' defined in ru.coffeetearea.repository.catalog.CountriesRepository 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 org.hibernate.AnnotationException: Unable to map collection ru.coffeetearea.model.Order.cart
英文:

I am writing an online store using Spring Boot and hiberbate. I have an Order class where I need a Сart link with relationhip @OneToMany. I created all tables and created all the fields I needed. Added links to Java-code but still throws an error. I can't understand what's the matter.
I can't figure out where I made a mistake in the mapping. Could this error be related to something else?

Class Cart:

  1. @Data
  2. @Entity
  3. @IdClass(Cart.CartId.class)
  4. @Table(name = &quot;cart_products&quot;)
  5. public class Cart {
  6. @NoArgsConstructor
  7. @AllArgsConstructor
  8. static class CartId implements Serializable {
  9. private Long orderIds;
  10. private Long drinkIds;
  11. }
  12. // Fields
  13. //
  14. @Id
  15. private Long orderIds;
  16. @Id
  17. private Long drinkIds;
  18. @ManyToOne
  19. @JoinColumn(name = &quot;order_id&quot;, referencedColumnName = &quot;id&quot;)
  20. private Order order;
  21. @ManyToOne
  22. @JoinColumn(name = &quot;drink_id&quot;, referencedColumnName = &quot;id&quot;)
  23. private Drink drink;
  24. private int count;
  25. }

Class Order:

  1. @Entity
  2. @Table(name = &quot;pg_order&quot;)
  3. public class Order {
  4. // Fields
  5. //
  6. private @Id
  7. @GeneratedValue
  8. Long id;
  9. private String address;
  10. @Column(name = &quot;phone_number&quot;)
  11. private String phoneNumber;
  12. @Temporal(TemporalType.TIMESTAMP)
  13. @Column(name = &quot;date_order&quot;)
  14. private Date dateOrder;
  15. @Enumerated(EnumType.STRING)
  16. @Column(name = &quot;order_status&quot;)
  17. private OrderStatus orderStatus;
  18. @Column(name = &quot;total_cost&quot;)
  19. private BigDecimal totalCost;
  20. // Relationships
  21. //
  22. @ManyToOne
  23. @JoinColumn(name = &quot;user_id&quot;)
  24. private User user;
  25. @OneToMany
  26. @JoinColumns({
  27. @JoinColumn(name = &quot;order_id&quot;, referencedColumnName = &quot;order_id&quot;),
  28. @JoinColumn(name = &quot;drink_id&quot;, referencedColumnName = &quot;drink_id&quot;)
  29. })
  30. private Set&lt;Cart&gt; cart;
  31. }

AnnotationException: 无法映射集合

AnnotationException: 无法映射集合

ERRORS:

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;countriesRepository&#39; defined in ru.coffeetearea.repository.catalog.CountriesRepository 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 org.hibernate.AnnotationException: Unable to map collection ru.coffeetearea.model.Order.cart
  2. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  3. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  4. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  5. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1444) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  8. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  9. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  10. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  12. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:621) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  13. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:609) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  14. at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:51) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  15. at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:36) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  16. at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  17. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  18. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  19. at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  20. at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  21. at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  22. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  23. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  24. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  25. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  26. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  27. at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  28. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  29. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  30. at ru.coffeetearea.CoffeeTearea.main(CoffeeTearea.java:12) ~[main/:na]
  31. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;jpaMappingContext&#39;: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to map collection ru.coffeetearea.model.Order.cart
  32. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  33. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  34. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  35. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  36. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  37. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  38. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  39. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  40. ... 28 common frames omitted
  41. Caused by: org.hibernate.AnnotationException: Unable to map collection ru.coffeetearea.model.Order.cart
  42. at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1690) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  43. at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:929) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  44. at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:845) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  45. at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:784) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  46. at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:53) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  47. at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1705) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  48. at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1654) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  49. at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:281) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  50. at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1217) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  51. at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1248) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  52. at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  53. at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  54. at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  55. at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
  56. at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
  57. at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
  58. at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
  59. Caused by: org.hibernate.cfg.RecoverableException: Unable to find column with logical name: order_id in org.hibernate.mapping.Table(pg_order) and its related supertables and secondary tables
  60. at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:832) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  61. at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:256) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  62. at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1680) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  63. ... 16 common frames omitted
  64. Caused by: org.hibernate.MappingException: Unable to find column with logical name: order_id in org.hibernate.mapping.Table(pg_order) and its related supertables and secondary tables
  65. at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:827) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
  66. ... 18 common frames omitted

答案1

得分: 2

你这里的映射有误:

  1. @OneToMany
  2. @JoinColumns({
  3. @JoinColumn(name = "order_id", referencedColumnName = "order_id"),
  4. @JoinColumn(name = "drink_id", referencedColumnName = "drink_id")
  5. })
  6. private Set<Cart> cart;

你说在OrderCart表格上都有order_iddrink_id列,但事实并非如此。

实际上,由于你在子实体(@ManyToOne所在的地方)定义了映射,你可以在父实体上简单地引用该映射:

  1. @OneToMany(mappedBy = "order")
  2. private Set<Cart> cart;
英文:

Your mapping is wrong here:

  1. @OneToMany
  2. @JoinColumns({
  3. @JoinColumn(name = &quot;order_id&quot;, referencedColumnName = &quot;order_id&quot;),
  4. @JoinColumn(name = &quot;drink_id&quot;, referencedColumnName = &quot;drink_id&quot;)
  5. })
  6. private Set&lt;Cart&gt; cart;

You're saying that there is both on Order and Cart table a order_id and drink_id column, which is not the case.

Actually since you have the mapping defined on the child side (where @ManyToOne resides), you can simply refer to that mapping on the parent side:

  1. @OneToMany(mappedBy = &quot;order&quot;)
  2. private Set&lt;Cart&gt; cart;

huangapple
  • 本文由 发表于 2020年7月28日 15:27:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63129013.html
匿名

发表评论

匿名网友

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

确定