JPA - 一对多合并问题

当我尝试合并一个实体时,我正面临下一个问题 .

我有一个课程菜单(一对多) - >类别(一对多) - >商品关联 .

如果我首先坚持使用空的列表类别和商品的菜单,菜单将被正确保留 .

现在,如果我将当前保存菜单与一个类别合并并清空商品列表,则类别保存正常,即使我将新商品添加到空白商品列表中,这仍然存在 .

现在,如果我在商品列表中添加第二个商品,我会遇到此错误:

菜单[menuId = 1,menu_name = dsad,menu_description = null,categories = [Category [category_id = 1,categoryName = a,goods = [GoodsImp [id = 1,brand = asd,createdDate = Mon Jan 22 18:26: 35 ART 2018],price = 3.0,description = asd,type = product]],GoodsImp [id = null,brand = dsad,createdDate = null],price = 33333.0,description = sd,type = product]]]],类别[category_id = 2,categoryName = b,goods = []]],business = 2] -------------- Hibernate:/ * load models.menu.Menu * / select
menu0_.MENU_ID为MENU_ID1_7_1_,
menu0_.BUSINESS_ID为BUSINESS2_7_1_,
menu0_.MENU_DESCRIPTION为MENU_DES3_7_1_,
menu0_.MENU_NAME为MENU_NAM4_7_1_,
categories1_.Menu_MENU_ID为Menu_MEN1_8_3_,
category2_.CATEGORY_ID as categori2_8_3_,
category2_.CATEGORY_ID为CATEGORY1_2_0_,
category2_.CATEGORY_NAME为CATEGORY2_2_0_

菜单菜单0_
左外连接
MENUES_CATEGORIES类别1_
在menu0_.MENU_ID = categories1_.Menu_MENU_ID
左外连接
分类类别2_
在categories1_.categories_CATEGORY_ID = category2_.CATEGORY_ID
哪里
menu0_.MENU_ID =?休眠:
/ * load models.menu.Product * / select
product0_.GOODS_ID为GOODS_ID2_6_0_,
product0_.GOODS_BRAND为GOODS_BR3_6_0_,
product0_.GOODS_CREATED为GOODS_CR4_6_0_,
product0_.GOODS_DESCRIPTION为GOODS_DE5_6_0_,
product0_.GOODS_IMAGE为GOODS_IM6_6_0_,
product0_.GOODS_PRICE为GOODS_PR7_6_0_

商品product0_
哪里
product0_.GOODS_ID =?
和product0_.DTYPE ='PRODUCT'

休眠:

/ * insert models.menu.Product
*/ 插入

产品
(GOODS_BRAND,GOODS_CREATED,GOODS_DESCRIPTION,GOODS_IMAGE,GOODS_PRICE,DTYPE)

(?,?,?,?,?,'PRODUCT')Hibernate:
选择
goods0_.GOODS_ID为GOODS_ID2_6_0_,
goods0_.GOODS_ID为GOODS_ID2_6_1_,
goods0_.GOODS_BRAND为GOODS_BR3_6_1_,
goods0_.GOODS_CREATED为GOODS_CR4_6_1_,
goods0_.GOODS_DESCRIPTION为GOODS_DE5_6_1_,
goods0_.GOODS_IMAGE为GOODS_IM6_6_1_,
goods0_.GOODS_PRICE为GOODS_PR7_6_1_,
goods0_.GOODS_PRICE_TYPE为GOODS_PR8_6_1_,
goods0_.DTYPE为DTYPE1_6_1_

货物商品0_
哪里
goods0_.GOODS_ID =?休眠:
选择
goods0_.GOODS_ID为GOODS_ID2_6_0_,
goods0_.GOODS_ID为GOODS_ID2_6_1_,
goods0_.GOODS_BRAND为GOODS_BR3_6_1_,
goods0_.GOODS_CREATED为GOODS_CR4_6_1_,
goods0_.GOODS_DESCRIPTION为GOODS_DE5_6_1_,
goods0_.GOODS_IMAGE为GOODS_IM6_6_1_,
goods0_.GOODS_PRICE为GOODS_PR7_6_1_,
goods0_.GOODS_PRICE_TYPE为GOODS_PR8_6_1_,
goods0_.DTYPE为DTYPE1_6_1_

货物商品0_
哪里
goods0_.GOODS_ID =?休眠:

/ * update models.menu.Product * / update
产品

GOODS_BRAND =?
GOODS_CREATED =?
GOODS_DESCRIPTION =?
GOODS_IMAGE =?
GOODS_PRICE =?
哪里
GOODS_ID =?休眠:
/ *创建一对多行模型.menu.Category.goods * / update
产品

GOODS_ID =?
哪里
GOODS_ID =? [warn] o.h.e.j.s.SqlExceptionHelper - SQL错误:1062,SQLState:23000 [错误] o.h.e.j.s.SqlExceptionHelper - 重复
键'PRIMARY'[错误]应用程序的条目'1' - ! @ 76lfn933p - 内部服务器错误,用于(PUT)[/ menu] - > play.api.http.HttpErrorHandlerExceptions $$ anon $ 1:执行异常[[PersistenceException:org.hibernate.exception.ConstraintViolationException:无法执行语句]] at play.api.http.HttpErrorHandlerExceptions $ .throwableToUsefulException(HttpErrorHandler.scala:255)at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)at play.filters.cors.AbstractCORSPolicy $$ anonfun $ 1.applyOrElse(AbstractCORSPolicy) .scala:155)在scala.concurrent上的play.filters.cors.AbstractCORSPolicy $$ anonfun $ 1.applyOrElse(AbstractCORSPolicy.scala:153)scala.concurrent.Future . $ anonfun $ recoverWith $ 1(Future.scala:412) . impl.Promise . $ anonfun $ transformWith $ 1(Promise.scala:37)at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)at play.api.libs.streams.Execution $ trampoline $ .execute(Execution .scala:70)scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:68)at scala.concurrent.impl.Promise $ Defa ultPromise . $ anonfun $ tryComplete $ 1(Promise.scala:284)引起:javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:无法在org.hibernate的org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)的org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)中执行语句位于org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1443)的org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1423)存储库中的.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164) . 在play.db.jpa.DefaultJPAApi.withTransaction的play.db.jpa.DefaultJPAApi.lambda $ withTransaction $ 3(DefaultJPAApi.java:197)上的JPAMenuesRepository.lambda $ updateMenu $ 2(JPAMenuesRepository.java:46)(DefaultJPAApi.java:138) at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:196)at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:169)引起:org.hibernate.exception.ConstraintViolationException:无法执行语句at org.hibernate.exception.internal.SQLExceptionTyp eDelegate.convert(SQLExceptionTypeDelegate.java:59)org.hibernate.exception.invert.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)位于org.hibernate.engine.jdbc的org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)位于org.hibernate.persister.collection.OneToManyPersister.insertRows(OneToManyPersister.java)的org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1567)中的.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45) :193)atg.hibernate.action.internal.CollectionUpdateAction.execute(CollectionUpdateAction.java:85)at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589)引起:com.mysql.jdbc . exceptions.jdbc4.MySQLIntegrityConstraintViola .Exception:在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)的sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)处的sun.reflect.NativeConstructorAccessorImpl.newInstance0(本地方法)中的键'PRIMARY'的重复条目'1'位于com.mysql.jdbc.Util.getInstance(Util.java:408)com.mysql.jdbc.Util.handleNewInstance(Util.java:425)的.lang.reflect.Constructor.newInstance(未知来源) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)的com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)中的.jdbc.SQLError.createSQLException(SQLError.java:936) . mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)

回答(1)

2 years ago

我可以解决它 .

问题是,One to Many的关系有@JoinColumn而不是@JoinTable . 你编写几个小时的问题就是这个问题,你只是看不出明显的问题 .

谢谢大家 .