我在dos中运行datanucleus增强程序,如下所示:

java -cp C:\Users\Chania\workspace\myproject\war\WEB-INF\classes\;C:\repo\datanucleus-full-3.1.0-release\lib\datanucleus-enhancer-3.1.0-release.jar;C:\repo\datanucleus-full-3.1.0-release\lib\datanucleus-core-3.1.0-release.jar;C:\repo\datanucleus-full-3.1.0-release\deps\jdo-api-3.1-SNAPSHOT-20110926.jar;C:\repo\datanucleus-full-3.1.0-release\lib\datanucleus-api-jdo-3.1.0-release.jar;C:\repo\datanucleus-full-3.1.0-release\deps\log4j-1.2.14.jar;C:\repo\datanucleus-full-3.1.0-release\deps\asm-4.0.jar -Dlog4j.configuration=file:C:\repo\datanucleus-full-3.1.0-release\log4j.properties org.datanucleus.enhancer.DataNucleusEnhancer -v C:\Users\Chania\workspace\myproject\war\WEB-INF\classes\com\myproject\domain\*.class > logfile.txt

我使用的Java版本是:
java version "1.6.0_27"

我得到的错误如下:
 ...
 308  [main] DEBUG DataNucleus.Persistence  - Added converter for java.net.URI<->java.lang.String using org.datanucleus.store.types.converters.URIStringConverter
 308  [main] DEBUG DataNucleus.Persistence  - Added converter for java.net.URL<->java.lang.String using org.datanucleus.store.types.converters.URLStringConverter
 309  [main] DEBUG DataNucleus.Persistence  - Added converter for java.util.UUID<->java.lang.String using org.datanucleus.store.types.converters.UUIDStringConverter
 309  [main] DEBUG DataNucleus.Persistence  - TypeConverter for javax.time.calendar.LocalDate<->java.lang.String ignored since java type not present in CLASSPATH
 310  [main] DEBUG DataNucleus.Persistence  - TypeConverter for javax.time.calendar.LocalTime<->java.lang.String ignored since java type not present in CLASSPATH
 311  [main] DEBUG DataNucleus.Persistence  - TypeConverter for javax.time.calendar.LocalTime<->java.lang.Long ignored since java type not present in CLASSPATH
 312  [main] DEBUG DataNucleus.Persistence  - TypeConverter for javax.time.calendar.LocalDateTime<->java.lang.String ignored since java type not present in CLASSPATH
 312  [main] DEBUG DataNucleus.Persistence  - Type converter support loaded
 312  [main] DEBUG DataNucleus.MetaData  - MetaDataManager : Input=(XML,Annotations), XML-Validation=false, XML-Suffices=(persistence=*.jdo, orm=orm, query=*.jdoquery), JDO-listener=false
 338  [main] DEBUG DataNucleus.MetaData  - MetaData Management : Loading Metadata for classes "[com.myproject.domain.Record, com.myproject.domain.RecordSummary, com.myproject.domain.Comment]" ...
 411  [main] DEBUG DataNucleus.MetaData  - Class "com.myproject.domain.Record" has been specified with JDO annotations so using those.
 596  [main] DEBUG DataNucleus.MetaData  - Class "com.myproject.domain.RecordSummary" has been specified with JDO annotations so using those.
 600  [main] DEBUG DataNucleus.MetaData  - Class "com.myproject.domain.Comment" has been specified with JDO annotations so using those.
 Errors were encountered when loading the specified MetaData files and classes. See the nested exceptions for details
604  [main] ERROR DataNucleus.Enhancer  - DataNucleus Enhancer completed with an error. Please review the enhancer log for full details. Some classes may have been enhanced but some caused errors
 Errors were encountered when loading the specified MetaData files and classes. See the nested exceptions for details
org.datanucleus.exceptions.NucleusUserException: Errors were encountered when loading the specified MetaData files and classes. See the nested exceptions for details
    at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:536)
    at org.datanucleus.enhancer.DataNucleusEnhancer.getFileMetadataForInput(DataNucleusEnhancer.java:734)
    at org.datanucleus.enhancer.DataNucleusEnhancer.enhance(DataNucleusEnhancer.java:525)
    at org.datanucleus.enhancer.DataNucleusEnhancer.main(DataNucleusEnhancer.java:1258)
Caused by: java.lang.NullPointerException
    at org.datanucleus.api.jdo.metadata.JDOAnnotationReader.processMemberAnnotations(JDOAnnotationReader.java:1682)
    at org.datanucleus.metadata.annotations.AbstractAnnotationReader.getMetaDataForClass(AbstractAnnotationReader.java:206)
    at org.datanucleus.metadata.annotations.AnnotationManagerImpl.getMetaDataForClass(AnnotationManagerImpl.java:171)
    at org.datanucleus.metadata.MetaDataManager.loadAnnotationsForClass(MetaDataManager.java:2650)
    at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:496)
    ... 3 more
Nested Throwables StackTrace:
java.lang.NullPointerException
    at org.datanucleus.api.jdo.metadata.JDOAnnotationReader.processMemberAnnotations(JDOAnnotationReader.java:1682)
    at org.datanucleus.metadata.annotations.AbstractAnnotationReader.getMetaDataForClass(AbstractAnnotationReader.java:206)
    at org.datanucleus.metadata.annotations.AnnotationManagerImpl.getMetaDataForClass(AnnotationManagerImpl.java:171)
    at org.datanucleus.metadata.MetaDataManager.loadAnnotationsForClass(MetaDataManager.java:2650)
    at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:496)
    at org.datanucleus.enhancer.DataNucleusEnhancer.getFileMetadataForInput(DataNucleusEnhancer.java:734)
    at org.datanucleus.enhancer.DataNucleusEnhancer.enhance(DataNucleusEnhancer.java:525)
    at org.datanucleus.enhancer.DataNucleusEnhancer.main(DataNucleusEnhancer.java:1258)
DataNucleus Enhancer completed with an error. Please review the enhancer log for full details. Some classes may have been enhanced but some caused errors

增强类的来源如下:
@PersistenceCapable(detachable = "true")
public class Record implements Serializable, Cacheable<String> {

    private static final long serialVersionUID = 9022509306966814904L;

    @PrimaryKey
    @Persistent(defaultFetchGroup = "true", valueStrategy = IdGeneratorStrategy.IDENTITY)
    private String id;

    @Name
    @Persistent(defaultFetchGroup = "true")
    private String name;

    @Name
    @Persistent(defaultFetchGroup = "true")
    private String surname;

    @Persistent(defaultFetchGroup = "true")
    private Date dateOfBirth;

    @Persistent(defaultFetchGroup = "true")
    private Date dateRecorded;

    @Persistent(defaultFetchGroup = "true")
    private String locationId;

    @Persistent(embedded = "true", defaultFetchGroup = "true")
    private CustomLocation customLocation;

    @Persistent(defaultFetchGroup = "true")
    private Category category;

    @Persistent(embedded = "true", defaultFetchGroup = "true")
    private CustomCategory customCategory;

    @Persistent(defaultFetchGroup = "true")
    private String description;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private List<StoredImageInfo> images;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private List<StoredAudioInfo> audio;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private List<StoredFileInfo> files;

    @Persistent(defaultFetchGroup = "true")
    private ModerationMode moderationMode;

    @Persistent(defaultFetchGroup = "true")
    private VisibilityMode visibilityMode;

    @Persistent(embedded = "true", defaultFetchGroup = "true")
    private UserProfileSummary owner;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private Map<String, UserProfileSummary> editors;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private Map<String, UserProfileSummary> connections;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private Map<String, RecordSummary> recordConnections;

    @Persistent(defaultFetchGroup = "true")
    private RecordType type;

    @Persistent(defaultFetchGroup = "true")
    private Date expiry;

    public Record() {
    }

    public Record(Record copy) {
        deepCopy(copy);
    }

    ...
}


@PersistenceCapable(embeddedOnly = "true")
public class RecordSummary implements Serializable {

    private static final long serialVersionUID = 9024309306966814904L;

    @Persistent(defaultFetchGroup = "true")
    private String id;

    @Persistent(defaultFetchGroup = "true")
    private RecordRole role;

    @Persistent(defaultFetchGroup = "true")
    private String name;

    @Persistent(defaultFetchGroup = "true")
    private String surname;

    @Persistent(defaultFetchGroup = "true")
    private Date dateOfBirth;

    @Persistent(defaultFetchGroup = "true")
    private Date dateRecorded;

    @SuppressWarnings("unused")
    private RecordSummary() {
        // for serialization
    }

    ...
}


@PersistenceCapable(detachable = "true")
public class Comment implements Attachable<String>, Serializable {

    private static final long serialVersionUID = -7624377549524333808L;

    @PrimaryKey
    @Persistent(defaultFetchGroup = "true", valueStrategy = IdGeneratorStrategy.IDENTITY)
    private String id;

    @Persistent(defaultFetchGroup = "true")
    private String recordId;

    @Persistent(defaultFetchGroup = "true")
    private String username;

    @Persistent(defaultFetchGroup = "true")
    private Date date;

    @Persistent(defaultFetchGroup = "true")
    private String content;

    @SuppressWarnings("unused")
    private Comment() {
        // for serialization
    }

    ...
}

datanucleus enhancer 3.0版本没有出现任何此类异常(尽管3.0有其他问题:problem 1problem 2)。这是DataNucleus3.1的错误还是我做错了什么?
更新:datanucleus增强器为所有标记为NullPointerException的列表字段抛出@Persistent(embeddedElement = "true")。这很可能是datanucleus 3.1增强器的一个bug。
更新2:根据请求,我将包含再现问题的最小代码:
@PersistenceCapable(detachable = "true")
public class Record {

    @PrimaryKey
    @Persistent(defaultFetchGroup = "true", valueStrategy = IdGeneratorStrategy.IDENTITY)
    private String id;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private List<MyEmbeddedElement> aList;

    public Record() {
    }

}


@PersistenceCapable(embeddedOnly = "true")
public class MyEmbeddedElement {

    @Persistent
    private String myField;

}

错误:
 730  [main] DEBUG DataNucleus.MetaData  - Class "com.myproject.domain.Record" has been specified with JDO annotations so using those.
 760  [main] DEBUG DataNucleus.MetaData  - Class "com.myproject.domain.MyEmbeddedElement" has been specified with JDO annotations so using those.
 Errors were encountered when loading the specified MetaData files and classes. See the nested exceptions for details
763  [main] ERROR DataNucleus.Enhancer  - DataNucleus Enhancer completed with an error. Please review the enhancer log for full details. Some classes may have been enhanced but some caused errors
 Errors were encountered when loading the specified MetaData files and classes. See the nested exceptions for details
org.datanucleus.exceptions.NucleusUserException: Errors were encountered when loading the specified MetaData files and classes. See the nested exceptions for details
    at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:536)
    at org.datanucleus.enhancer.DataNucleusEnhancer.getFileMetadataForInput(DataNucleusEnhancer.java:734)
    at org.datanucleus.enhancer.DataNucleusEnhancer.enhance(DataNucleusEnhancer.java:525)
    at org.datanucleus.enhancer.DataNucleusEnhancer.main(DataNucleusEnhancer.java:1258)
Caused by: java.lang.NullPointerException
    at org.datanucleus.api.jdo.metadata.JDOAnnotationReader.processMemberAnnotations(JDOAnnotationReader.java:1682)
    at org.datanucleus.metadata.annotations.AbstractAnnotationReader.getMetaDataForClass(AbstractAnnotationReader.java:206)
    at org.datanucleus.metadata.annotations.AnnotationManagerImpl.getMetaDataForClass(AnnotationManagerImpl.java:171)
    at org.datanucleus.metadata.MetaDataManager.loadAnnotationsForClass(MetaDataManager.java:2650)
    at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:496)
    ... 3 more
Nested Throwables StackTrace:
java.lang.NullPointerException
    at org.datanucleus.api.jdo.metadata.JDOAnnotationReader.processMemberAnnotations(JDOAnnotationReader.java:1682)
    at org.datanucleus.metadata.annotations.AbstractAnnotationReader.getMetaDataForClass(AbstractAnnotationReader.java:206)
    at org.datanucleus.metadata.annotations.AnnotationManagerImpl.getMetaDataForClass(AnnotationManagerImpl.java:171)
    at org.datanucleus.metadata.MetaDataManager.loadAnnotationsForClass(MetaDataManager.java:2650)
    at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:496)
    at org.datanucleus.enhancer.DataNucleusEnhancer.getFileMetadataForInput(DataNucleusEnhancer.java:734)
    at org.datanucleus.enhancer.DataNucleusEnhancer.enhance(DataNucleusEnhancer.java:525)
    at org.datanucleus.enhancer.DataNucleusEnhancer.main(DataNucleusEnhancer.java:1258)
DataNucleus Enhancer completed with an error. Please review the enhancer log for full details. Some classes may have been enhanced but some caused errors

更新3:此问题存在于所有数据核版本中,从3.0.2开始。

最佳答案

当我在我的应用程序中嵌入一个集合时

@Element(embedded="true")
Collection<OtherType> myCollField;

这对我来说很有效,而且看起来更符合逻辑地指定嵌入元素。
你的地图字段也错了…地图没有“元素”,有“键”和“值”

关于java - 运行DataNucleus Enhancer 3.1.0版本的NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11823840/

10-10 12:56