我在DB中有表:A,B,D
A和B之间的关系是OneToMany。 B和D之间的关系是OneToMany。 我想将A的项目从a复制到'
表A:主键是id。
id description
a aaa
b bbb
c ccc
表B:主键是id和id_A。
id id_A description
1 a xyz
2 a x'y'z'
表D:主键是id,id_A,id_B。
id id_A id_B description
1 a 1 xyz
2 a 2 x'y'z'
当在A表中复制项目“a”时,B中的两个项目的id为1,必须复制2。 我希望复制后的数据库如下:
表A:
id description
a aaa
b bbb
c ccc
a' aaa
表B:
id id_A description
1 a xyz
2 a x'y'z'
1 a' xyz
2 a' x'y'z'
表D:
id id_A id_B description
1 a 1 xyz
2 a 2 x'y'z'
1 a' 1 xyz
2 a' 2 x'y'z'
这是我的Dao代码:
public void copy(A a', String id_A) throws Exception {
EntityManager em = JPA.em();
EntityManagerFactory factory = Persistence.createEntityManagerFactory(Consts.DEFAULT_PERSISTENCE_UNIT);
em = factory.createEntityManager();
em.getTransaction().begin();
// insert new A with a'
insert(a');
// insert B for A
BDao Bdao = new BDao();
DDao Ddao = new DDao();
List<B> listB = dao.findByAId(id_A);
List<D> listD = new ArrayList<>();
for (int i = 0; i < listB.size(); i++) {
BPK id = new BPK();
id.setId_A(id_A);
B newB = listB.get(i);
newB.setId(id);
Bdao.insert(newB);
if (((i + 1) % listB.size()) == 0) {
em.flush();
em.clear();
}
// insert D
listD = Ddao.getById(id_A, newB.getId().getId_B());
for (int j = 0; j < listD.size(); j++) {
D newD = listD.get(i);
newD.setId().setId_A(id_A);
newD.setId().setId_B(newB.getId().getId_B());
Ddao.insert(newD);
if (((j + 1) % listD.size()) == 0) {
em.flush();
em.clear();
}
}
}
em.getTransaction().commit();
}
但是当我跑步时,错误发生了:
javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an B was altered from BPK@7fa6cf87 to BPK@62310b05
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458)
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67)
at jp.co.cacco.OPLUX.api.daos.JudgingRuleDao.findByAuthoriModelId(JudgingRuleDao.java:281)
at jp.co.cacco.OPLUX.api.daos.AuthoriModelDao.copyAuthoriModel(AuthoriModelDao.java:241)
at jp.co.cacco.OPLUX.api.services.AuthoriModelDetailService.copyAuthoriModel(AuthoriModelDetailService.java:252)
at jp.co.cacco.OPLUX.manager.controllers.AuthoriModelDetailController.submit(AuthoriModelDetailController.java:126)
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$115$$anonfun$apply$115.apply(routes_routing.scala:2044)
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$115$$anonfun$apply$115.apply(routes_routing.scala:2044)
at play.core.Router$HandlerInvokerFactory$$anon$4.resultCall(Router.scala:264)
at play.core.Router$HandlerInvokerFactory$JavaActionInvokerFactory$$anon$15$$anon$1.invocation(Router.scala:255)
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:55)
at play.GlobalSettings$1.call(GlobalSettings.java:67)
at play.mvc.Security$AuthenticatedAction.call(Security.java:44)
at play.db.jpa.TransactionalAction$1.apply(TransactionalAction.java:23)
at play.db.jpa.TransactionalAction$1.apply(TransactionalAction.java:21)
at play.db.jpa.JPA.withTransactionAsync(JPA.java:180)
at play.db.jpa.TransactionalAction.call(TransactionalAction.java:18)
at play.core.j.JavaAction$$anonfun$11.apply(JavaAction.scala:82)
at play.core.j.JavaAction$$anonfun$11.apply(JavaAction.scala:82)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40)
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:46)
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32)
at scala.concurrent.impl.Future$.apply(Future.scala:31)
at scala.concurrent.Future$.apply(Future.scala:492)
at play.core.j.JavaAction$class.apply(JavaAction.scala:82)
at play.core.Router$HandlerInvokerFactory$JavaActionInvokerFactory$$anon$15$$anon$1.apply(Router.scala:252)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130)
at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:129)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:128)
at scala.Option.map(Option.scala:145)
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:128)
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:121)
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:483)
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:483)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:519)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:519)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:496)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:496)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: org.hibernate.HibernateException: identifier of an instance of B was altered from BPK@7fa6cf87 to BPK@62310b05
at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:80)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:192)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:152)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:61)
at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1191)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1257)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
... 49 more
请提前帮助我。