我们的对象结构如上所示。让一个支持Java Persistence的对象命名为' User.java'和User.java包含' Us
的列表
public class FXUserDAOImpl implements FXUserDAO {
private TOVOConvertor<FXUserTO, FXUserVO> tovoConvertor = null;
private PersistenceManagerFactory pmf;
public FXUserDAOImpl() {
this.tovoConvertor = new FXUserVOTO();
this.pmf = JDOHelper.getPersistenceManagerFactory("TALOS");
}
@Override
public Object persist(FXUserTO to) {
FXUserVO vo = tovoConvertor.convertTO(to);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
FxUserManagementMonitor.monitor.debug("Persisting : " + vo);
pm.makePersistent(vo);
tx.commit();
id = pm.getObjectId(vo);
FxUserManagementMonitor.monitor.debug("Persisted Object Id : " + id);
} catch (Exception e) {
FxUserManagementMonitor.monitor.error("Exception persisting data : " , e);
throw new DataManagementRuntimeException(PlatformExceptionEnum.QUERY_SQL_EXCPETION,"Failed to execute persist ",e);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
FxUserManagementMonitor.monitor.debug("Persisted Successfully "+id);
return id;
}
@Override
public List<FXUserTO> retrieveAll() {
PersistenceManager persistentManager;
Transaction transaction;
List<FXUserTO> toList = new ArrayList<>();
persistentManager = pmf.getPersistenceManager();
transaction = persistentManager.currentTransaction();
try {
transaction.begin();
FxUserManagementMonitor.monitor.debug("Retrieving Extent...");
Extent<FXUserVO> voExtend = persistentManager.getExtent(FXUserVO.class, true);
Iterator<FXUserVO> iterator = voExtend.iterator();
iterator.forEachRemaining(eachVo -> {toList.add(tovoConvertor.convertVO(eachVo));});
transaction.commit();
} catch (Exception e) {
FxUserManagementMonitor.monitor.error("Failed to retrieve all ...",e);
throw new DataManagementRuntimeException(PlatformExceptionEnum.QUERY_SQL_EXCPETION,"Failed to execute retrieveAll ",e);
} finally {
if (transaction.isActive()) {
transaction.rollback();
}
persistentManager.close();
}
FxUserManagementMonitor.monitor.debug("Successfully retrieved all ...");
return toList;
}
}
&#13;
&#39;群组&#39 ;.我们还有两个DAO类来持久化和检索User和UserGroups。
我们期望的是,所有用户信息都将保存到用户表,并且只链接到Group_1,Group_2将保留在该表中。但是我们正在获得INVALID SQL Query Exception。如下。实际上它试图将新记录输入UserGroup表
@PersistenceCapable(identityType=IdentityType.APPLICATION,table="FXUserVO")
public class FXUserVO implements TalosVO {
@PrimaryKey
@Column(name ="userId",jdbcType = "VARCHAR")
@Persistent
private String userId;
@Element(column="userId")
@ForeignKey(name="FK_GROUPS_FXUSERVO")
@Persistent
private List<UserGroupVO> groups;
------
}
@PersistenceCapable(identityType=IdentityType.APPLICATION,table="UserGroupVO")
public class UserGroupVO implements TalosVO {
@PrimaryKey
@Column(name ="groupId",jdbcType = "VARCHAR")
@Persistent
private String groupId;
@Column(name ="groupName",jdbcType = "VARCHAR")
@Persistent
private String groupName;
}
&#13;
线程中的异常&#34; main&#34; com.evermore.talos.platform.datamanagement.api.exception.DataManagementRuntimeException:SQL QUERY无效&gt;无法执行持久化
at com.evermore.talos.services.fxservices.fxusermanagementapi.generated.dao.FXUserDAOImpl.persist(FXUserDAOImpl.java:52)
at com.evermore.talos.services.fxservices.fxusermanagementapi.generated.dao.FXUserDAOImpl.persist(FXUserDAOImpl.java:1)
at com.evermore.talos.services.fxservices.fxusermanagementapi.generation.FXUserDAOImplTest.testFXuser(FXUserDAOImplTest.java:56)
at com.evermore.talos.services.fxservices.fxusermanagementapi.generation.FXUserDAOImplTest.main(FXUserDAOImplTest.java:35)
引起:javax.jdo.JDODataStoreException:插入对象&#34; com.evermore.talos.services.fxservices.fxusermanagementapi.generated.vo.UserGroupVO@23c30a20"使用声明&#34; INSERT INTO USERGROUPVO
(GROUPNAME
,GROUPID
,USERID
,GROUPS_INTEGER_IDX
)VALUES(?,?,?,?)&# 34;失败:重复录入&#39;群组1&#39;关键&#39; PRIMARY&#39;
NestedThrowables:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目&#39; Group1&#39;关键&#39; PRIMARY&#39;
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:543)
在org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:717)
at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:738)
at com.evermore.talos.services.fxservices.fxusermanagementapi.generated.dao.FXUserDAOImpl.persist(FXUserDAOImpl.java:46)
......还有3个
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目&#39; Group1&#39;关键&#39; PRIMARY&#39;
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
在com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
在com.mysql.jdbc.Util.getInstance(Util.java:408)
在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
在com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)
at org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)
at org.datanucleus.store.rdbms.ParamLoggingPreparedStatement.executeUpdate(ParamLoggingPreparedStatement.java:393)
at org.datanucleus.store.rdbms.SQLController.executeStatementUpdate(SQLController.java:432)
在org.datanucleus.store.rdbms.request.InsertRequest.execute(InsertRequest.java:398)
在org.datanucleus.store.rdbms.RDBMSPersistenceHandler.insertObjectInTable(RDBMSPersistenceHandler.java:162)
在org.datanucleus.store.rdbms.RDBMSPersistenceHandler.insertObject(RDBMSPersistenceHandler.java:138)
at org.datanucleus.state.StateManagerImpl.internalMakePersistent(StateManagerImpl.java:3369)
at org.datanucleus.state.StateManagerImpl.makePersistent(StateManagerImpl.java:3345)
at org.datanucleus.ExecutionContextImpl.persistObjectInternal(ExecutionContextImpl.java:1987)
at org.datanucleus.ExecutionContextImpl.persistObjectInternal(ExecutionContextImpl.java:2084)
at org.datanucleus.store.types.SCOUtils.validateObjectForWriting(SCOUtils.java:1445)
at org.datanucleus.store.rdbms.scostore.ElementContainerStore.validateElementForWriting(ElementContainerStore.java:289)
at org.datanucleus.store.rdbms.scostore.FKListStore.validateElementForWriting(FKListStore.java:928)
在org.datanucleus.store.rdbms.scostore.FKListStore.internalAdd(FKListStore.java:534)
at org.datanucleus.store.rdbms.scostore.AbstractListStore.addAll(AbstractListStore.java:137)
at org.datanucleus.store.rdbms.mapping.java.CollectionMapping.postInsert(CollectionMapping.java:157)
在org.datanucleus.store.rdbms.request.InsertRequest.execute(InsertRequest.java:507)
在org.datanucleus.store.rdbms.RDBMSPersistenceHandler.insertObjectInTable(RDBMSPersistenceHandler.java:162)
在org.datanucleus.store.rdbms.RDBMSPersistenceHandler.insertObject(RDBMSPersistenceHandler.java:138)
at org.datanucleus.state.StateManagerImpl.internalMakePersistent(StateManagerImpl.java:3369)
at org.datanucleus.state.StateManagerImpl.makePersistent(StateManagerImpl.java:3345)
at org.datanucleus.ExecutionContextImpl.persistObjectInternal(ExecutionContextImpl.java:1987)
at org.datanucleus.ExecutionContextImpl.persistObjectWork(ExecutionContextImpl.java:1830)
at org.datanucleus.ExecutionContextImpl.persistObject(ExecutionContextImpl.java:1685)
at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:712)
......还有5个