简体   繁体   中英

JPA - One to many merge issue

I am facing the next issue, when I trying to merge an entity.

I have a class Menu (One to Many) --> Category (One to Many) --> Goods association.

If I persist first the menu with an empty List of Category and Goods the menu is persisted correctly.

Now, if I merge the current save menu with one category and empty List of Goods, the Category saves fine, even if I add a new good into the empty List of goods this is persisted fine.

Now, If I add a second good into the list of goods I have this error:

Menu [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 [category_id=2, categoryName=b, goods=[ ]]], business=2]

-------------- Hibernate:

 /* load models.menu.Menu */ select menu0_.MENU_ID as MENU_ID1_7_1_, menu0_.BUSINESS_ID as BUSINESS2_7_1_, menu0_.MENU_DESCRIPTION as MENU_DES3_7_1_, menu0_.MENU_NAME as MENU_NAM4_7_1_, categories1_.Menu_MENU_ID as Menu_MEN1_8_3_, category2_.CATEGORY_ID as categori2_8_3_, category2_.CATEGORY_ID as CATEGORY1_2_0_, category2_.CATEGORY_NAME as CATEGORY2_2_0_ from MENUES menu0_ left outer join MENUES_CATEGORIES categories1_ on menu0_.MENU_ID=categories1_.Menu_MENU_ID left outer join Categories category2_ on categories1_.categories_CATEGORY_ID=category2_.CATEGORY_ID where menu0_.MENU_ID=? Hibernate: /* load models.menu.Product */ select product0_.GOODS_ID as GOODS_ID2_6_0_, product0_.GOODS_BRAND as GOODS_BR3_6_0_, product0_.GOODS_CREATED as GOODS_CR4_6_0_, product0_.GOODS_DESCRIPTION as GOODS_DE5_6_0_, product0_.GOODS_IMAGE as GOODS_IM6_6_0_, product0_.GOODS_PRICE as GOODS_PR7_6_0_ from GOODS product0_ where product0_.GOODS_ID=? and product0_.DTYPE='PRODUCT' 

Hibernate:

 /* insert models.menu.Product */ insert into GOODS (GOODS_BRAND, GOODS_CREATED, GOODS_DESCRIPTION, GOODS_IMAGE, GOODS_PRICE, DTYPE) values (?, ?, ?, ?, ?, 'PRODUCT') Hibernate: select goods0_.GOODS_ID as GOODS_ID2_6_0_, goods0_.GOODS_ID as GOODS_ID2_6_1_, goods0_.GOODS_BRAND as GOODS_BR3_6_1_, goods0_.GOODS_CREATED as GOODS_CR4_6_1_, goods0_.GOODS_DESCRIPTION as GOODS_DE5_6_1_, goods0_.GOODS_IMAGE as GOODS_IM6_6_1_, goods0_.GOODS_PRICE as GOODS_PR7_6_1_, goods0_.GOODS_PRICE_TYPE as GOODS_PR8_6_1_, goods0_.DTYPE as DTYPE1_6_1_ from GOODS goods0_ where goods0_.GOODS_ID=? Hibernate: select goods0_.GOODS_ID as GOODS_ID2_6_0_, goods0_.GOODS_ID as GOODS_ID2_6_1_, goods0_.GOODS_BRAND as GOODS_BR3_6_1_, goods0_.GOODS_CREATED as GOODS_CR4_6_1_, goods0_.GOODS_DESCRIPTION as GOODS_DE5_6_1_, goods0_.GOODS_IMAGE as GOODS_IM6_6_1_, goods0_.GOODS_PRICE as GOODS_PR7_6_1_, goods0_.GOODS_PRICE_TYPE as GOODS_PR8_6_1_, goods0_.DTYPE as DTYPE1_6_1_ from GOODS goods0_ where goods0_.GOODS_ID=? Hibernate: /* update models.menu.Product */ update GOODS set GOODS_BRAND=?, GOODS_CREATED=?, GOODS_DESCRIPTION=?, GOODS_IMAGE=?, GOODS_PRICE=? where GOODS_ID=? Hibernate: /* create one-to-many row models.menu.Category.goods */ update GOODS set GOODS_ID=? where GOODS_ID=? [warn] ohejsSqlExceptionHelper - SQL Error: 1062, SQLState: 23000 [error] ohejsSqlExceptionHelper - Duplicate 

entry '1' for key 'PRIMARY' [error] application -

! @76lfn933p - Internal server error, for (PUT) [/menu] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement]] 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) at play.filters.cors.AbstractCORSPolicy$$anonfun$1.applyOrElse(AbstractCORSPolicy.scala:153) at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:412) at scala.concurrent.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) at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:68) at scala.concurrent.impl.Promise$DefaultPromise.$anonfun$tryComplete$1(Promise.scala:284) Caus ed by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1443) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1423) at repository.JPAMenuesRepository.lambda$updateMenu$2(JPAMenuesRepository.java:46) at play.db.jpa.DefaultJPAApi.lambda$withTransaction$3(DefaultJPAApi.java:197) at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:138) at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:196) at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:169) Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement at o rg.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178) at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45) at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1567) at org.hibernate.persister.collection.OneToManyPersister.insertRows(OneToManyPersister.java:193) at org.hibernate.action.internal.CollectionUpdateAction.execute(CollectionUpdateAction.java:85) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589) Caused by: com.mysql.jdbc .exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)

I could fix it.

The problem was, that the relation One to Many has a @JoinColumn instead of @JoinTable. Thats the issue when you code for several hours, you just dont see the obvious.

Thanks to all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM