无法使用 UUID 上的 datanucleus 坚持到 Cassandra

Cannot persist to Cassandra with datanucleus on UUID

我有一个 table,其 ID 字段是这样定义的:

create table XXX (
id uuid primary key,
name varchar,
....

而我的class XXX是这样标注的:

public class XXX {

@PrimaryKey
@Persistent(customValueStrategy = "uuid")
public UUID id;
...

尝试发出 PersistenceManager.makePersistent() 时,我得到:

Exception in thread "main" javax.jdo.JDOFatalInternalException: Invalid Id generated for field XXX.id, id : 54e5abde-732b-42ff-af92-06af89af46ff at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:673) at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:720) at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:740) at Importer.save(StayImporter.java:84) at Importer.exec(StayImporter.java:102) at Importer.main(Importer.java:110) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) NestedThrowablesStackTrace: Invalid Id generated for field XXX.id, id : 54e5abde-732b-42ff-af92-06af89af46ff org.datanucleus.exceptions.NucleusException: Invalid Id generated for field XXX.id, id : 54e5abde-732b-42ff-af92-06af89af46ff at org.datanucleus.store.AbstractStoreManager.getStrategyValue(AbstractStoreManager.java:1300) at org.datanucleus.state.StateManagerImpl.populateStrategyFields(StateManagerImpl.java:2201) at org.datanucleus.state.StateManagerImpl.initialiseForPersistentNew(StateManagerImpl.java:418) at org.datanucleus.state.StateManagerImpl.initialiseForPersistentNew(StateManagerImpl.java:120) at org.datanucleus.state.ObjectProviderFactoryImpl.newForPersistentNew(ObjectProviderFactoryImpl.java:218) at org.datanucleus.ExecutionContextImpl.persistObjectInternal(ExecutionContextImpl.java:2078) at org.datanucleus.ExecutionContextImpl.persistObjectWork(ExecutionContextImpl.java:1922) at org.datanucleus.ExecutionContextImpl.persistObject(ExecutionContextImpl.java:1777) at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:715) at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:740)

我觉得UUID不错,为什么不行?

Maven 是:

<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-accessplatform-jdo-cassandra</artifactId>
<version>4.2.1</version>
<type>pom</type>
</dependency>

<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.1.9</version>
</dependency>

Cassandra 版本为 2.2.3

查看此论坛post:http://www.datanucleus.org/servlet/forum/viewthread_thread,7922_lastpage,yes#lastpost

链接内容摘录:

"UUIDGenerator" generates Strings. Consequently you cannot currently use it to generate UUID values with released code. Nightly builds add support for converting a String generated value to any of TimeZone, Locale, UUID, Currency, so ought to work with that.

Barring that you can easily get the code and create your own ValueGenerator plugin that extends AbstractGenerator to generate UUID objects.