我真的很难理解为什么我会收到这个错误。我正在引用我的Artist Entity中的GLOBAL_ID字段。我想我一定错过了JPA内部运作方式。让我们考虑这两个实体。
@Entity
public class Music {
@Id
private String MYID;
@Column(unique=true)
private String GLOBAL_ID;
@ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="ARTIST_ID")
private Artist artist;
}
@Entity
public class Artist {
@Id
private String MYID;
@Column(unique=true)
private String GLOBAL_ID;
}
在我尝试坚持音乐之前。我通过Artist GLOBAL_ID查找数据库(如果已存在)。如果存在,我将获得我从DB获得的实例并将艺术家设置为音乐。之后,我做了EntityManager.merge(音乐)。因此,即使我从数据库中获得这位艺术家,并将新传入的艺术家与现有艺术家交换,因为音乐是新的,实体经理将试图再次坚持下去。所以,我做的不同...... 我做了艺术家查找,当数据库中有一个具有相同GLOBAL_ID的艺术家时,我将音乐中的艺术家字段设置为null并合并音乐。之后我获得了托管音乐实体,并设置了我之前查找过的现有艺术家。没有成功...... 我正在这样合并....音乐= EntityManager.merge(音乐)
我整天都看过这段代码,但却找不到我做错了什么。是不是JPA不应该试图坚持并且实体已经管理了?我不知道。如果有人能帮助我,我将不胜感激。
除了我已尝试过的万亿不同的东西,我试图将从DB找到的艺术家分开,但我以后无法重新合并。
提前感谢您的帮助,
此致
下面是处理持久性和堆栈跟踪的代码。
好的,我会尝试发布和解释...... 这是一个导入bean,它在循环中从目录中读取mp3文件,从中获取ID3标记并从中构造Music类。
这是处理JPA的代码。我已经修改了几次以消除错误。现在,当音乐是现有艺术家的新作品时,我会从音乐中删除艺术家参考,并在合并音乐后将其链接到现有艺术家。在此之前,我只是将艺术家与现有艺术家联系起来并坚持下去。 我可以看到所有带有satysfying ID的日志消息以及所有这些消息。我的意思是我可以注意到艺术家查询找到了现有的艺术家。下面是堆栈跟踪。再次感谢您查看它...
if (mediaMusic.getMYID() == null || mediaMusic.getMYID().equals("")) {
Music oldMusic = null;
Query mq = null;
if (mediaMusic.getGLOBAL_ID() != null && !mediaMusic.getGLOBAL_ID().equals("")) {
mq = em.createNamedQuery(Music.QUERY_FIND_BY_GLOBAL_ID);
mq.setParameter(Music.QUERY_PARAMETER_GLOBAL_ID, mediaMusic.getGLOBAL_ID());
Iterator musicsSameGLID = mq.getResultList().iterator();
if (musicsSameGLID.hasNext()) {
oldMusic = (Music) musicsSameGLID.next();
}
} else if (mediaMusic.getTitle() != null && !mediaMusic.getTitle().equals("")) {
mq = em.createNamedQuery(Music.QUERY_FIND_BY_TITLE);
mq.setParameter(Music.QUERY_PARAMETER_TITLE, mediaMusic.getTitle().toUpperCase());
Iterator existingMusics = mq.getResultList().iterator();
Music existingMusic = null;
while (existingMusics.hasNext()) {
existingMusic = (Music) existingMusics.next();
if (mediaMusic.getArtist() != null && existingMusic.getArtist() != null
&& mediaMusic.getArtist().getGLOBAL_ID() != null
&& mediaMusic.getArtist().getGLOBAL_ID().equals(existingMusic.getArtist().getGLOBAL_ID())) {
oldMusic = existingMusic;
break;
} else if (existingMusic.getArtist() != null && mediaMusic.getArtist() != null
&& existingMusic.getArtist().getName().equals(mediaMusic.getArtist().getName())) {
oldMusic = existingMusic;
break;
}
}
}
if (oldMusic != null) {
mediaMusic = oldMusic;
}
}
// IF IT IS A NEW ARTIST, CHECKING FOR EXISTING ONE
if (mediaMusic.getArtist() != null
&& (mediaMusic.getArtist().getMYID() == null || mediaMusic.getArtist().getMYID().equals(""))) {
if (mediaMusic.getArtist().getGLOBAL_ID() != null
&& !mediaMusic.getArtist().getGLOBAL_ID().equals("")) {
Query aq = em.createNamedQuery(Artist.QUERY_FIND_BY_GLOBAL_ID);
aq.setParameter(Artist.GLOBAL_ID_QUERY_PARAMETER, mediaMusic.getArtist().getGLOBAL_ID());
Iterator existingArtists = aq.getResultList().iterator();
if (existingArtists.hasNext()) {
// Persiste primeiro sem artista
logger.log(Level.INFO, "Artista {0}, ArtistaGLID: {1} para a musica MYID: {2} Titulo: {3} found in DB", new Object[]{mediaMusic.getArtist().getName(), mediaMusic.getArtist().getGLOBAL_ID(), mediaMusic.getMYID(), mediaMusic.getTitle()});
if (mediaMusic.getMYID() == null || mediaMusic.getMYID().equals("")) {
artistaExistente = (Artist) existingArtists.next();
mediaMusic.setArtist(null);
logger.log(Level.INFO, "Inserting and removing Artist: New artist is {0}", mediaMusic.getArtist());
} else {
mediaMusic.setArtist((Artist)existingArtists.next());
}
}
}
}
mediaMusic = em.merge(mediaMusic);
// 20110424 TRYING TO LINK EXISTING ARTIST AFTER MUSIC IN DB
if (artistaExistente != null)) {
logger.log(Level.INFO, "Music already in BD with o MYID: {0}", mediaMusic.getMYID());
logger.log(Level.INFO, "Adding artist {0} MYID: {1}", new Object[]{artistaExistente.getName(), artistaExistente.getMYID()});
mediaMusic.setArtist(artistaExistente);
logger.info("After artist had been added to Music");
}
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '01809552-4f87-45b0-a45b0-afff-2c6f0730a3be' for key 'GLOBAL_ID'
at sun.reflect.GeneratedConstructorAccessor178.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.Util.getInstance(Util.java:382)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2333)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2318)
at com.sun.gjc.spi.base.PreparedStatementWrapper.executeUpdate(PreparedStatementWrapper.java:120)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:789)
... 74 more
|#]
[#|2011-04-25T10:32:01.004-0300|SEVERE|oracle-glassfish3.1|co.bmq.media.beans.MediaMaintenanceBean|_ThreadID=53;_Thr$
javax.ejb.EJBException: Transaction aborted
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5121)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4894)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.jav$
at $Proxy201.updateMedia(Unknown Source)
at co.bmq.media.beans.__EJB31_Generated__MediaStorageServiceBean__Intf____Bean__.updateMedia(Unknown Source)
at co.bmq.media.beans.MediaMaintenanceBean.checkImportDir(MediaMaintenanceBean.java:98)
at sun.reflect.GeneratedMethodAccessor374.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManag
er.java:1124)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5367)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:801)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundTimeout(SystemInterceptorProxy.java:149)
at sun.reflect.GeneratedMethodAccessor373.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:862)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:801)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:371)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5339)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5327)
at com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:4033)
at com.sun.ejb.containers.EJBTimerService.deliverTimeout(EJBTimerService.java:1835)
at com.sun.ejb.containers.EJBTimerService.access$100(EJBTimerService.java:108)
at com.sun.ejb.containers.EJBTimerService$TaskExpiredWork.run(EJBTimerService.java:2708)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.transaction.RollbackException: Transaction marked for rollback.
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:475)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:$
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5115)
... 37 more
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exce$
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '01809552-4f552-4f87-45b0-afff-2c6f0730a3be' for key 'GLOBAL_ID'
Error Code: 1062
Call: INSERT INTO ARTIST (MYID, ANNOTATION, COMMENT, COUNTRY, GENDER, LASTDATANORMALIZATIONDATE, LASTUPDATEDON, LIKES, GLOBAL_ID, NAME, SINCEDATE, TAGS, TODATE, TYPE, ENTITY_UID, VERSION) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [14 parameters bound]
Query: InsertObjectQuery(co.bmq.media.entity.Artist@3741c26)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:798)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:864)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:583)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:526)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1729)
at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:234)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.ja$
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.ja$
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.j$
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:162)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:177)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:$
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.$
at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:808)
at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:711)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModif$
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2842)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1521)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1503)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:$
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsForClassWithChangeSet(CommitManager.java:$
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:136)
at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:3766)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1404)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.ja$
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1511)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3115)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitO$
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListe$
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:6$
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:437)
... 39 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '01809552-4f87-45b0-a$
at sun.reflect.GeneratedConstructorAccessor178.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.Util.getInstance(Util.java:382)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2333)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2318)
at com.sun.gjc.spi.base.PreparedStatementWrapper.executeUpdate(PreparedStatementWrapper.java:120)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:789)
... 74 more
答案 0 :(得分:1)
我认为新的Music对象在解析现有Artist时感到困惑可能会出现任何问题。如果音乐是新的,而不是merge(),请尝试调用persist()。
答案 1 :(得分:0)
我想知道为什么你需要让GLOBAL_ID驻留在两个表中。您可以像SELECT m FROM Music m WHERE m.artist.name = :artistName
一样查询。您可以删除GLOBAL_ID并优化逻辑以使其更简单。
答案 2 :(得分:0)
首先,感谢所有参与的人。实际上,没有足够的细节让你们找出来,这对我的项目非常具体。
问题是:我在这个对象中有更多的双向关系,比如......音乐 - > AlbumTrack - >专辑 - >艺术家。我的问题在于这里的艺术家,我没有使用数据库中存在的那个进行修改,而且日志很难检测到哪个对象导致了它。
再次感谢你们