1

我有以下类作为超类

@Entity
@MappedSuperclass
public class Location implements LocationCapable {

    @Basic
    private Double latitude;

    @Basic
    private Double longitude;

    @Basic
    private List<String> geocells;

    @PrePersist
    @Transient
    private void generateGeoCells() {
        geocells = GeocellManager.generateGeoCell(getLocation());
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    @Override
    @Transient
    @JsonIgnore
    public Point getLocation() {
        return new Point(latitude, longitude);
    }

    @Override
    @Transient
    @JsonIgnore
    public String getKeyString() {
        return latitude + ":" + longitude;
    }

    @Override
    public List<String> getGeocells() {
        return geocells;
    }

    public void setGeocells(List<String> geocells) {
        this.geocells = geocells;
    }

}

另一个继承自这个的

但是当我尝试运行 JUnit 测试时,我得到了这个

Caused by: org.datanucleus.metadata.InvalidMetaDataException: Class Location has application-identity and no objectid-class specified yet has 0 primary key fields. Unable to use SingleFieldIdentity.
at org.datanucleus.metadata.AbstractClassMetaData.determineObjectIdClass(AbstractClassMetaData.java:1032)
at org.datanucleus.metadata.ClassMetaData.populate(ClassMetaData.java:205)
at org.datanucleus.metadata.AbstractClassMetaData.validateSuperClass(AbstractClassMetaData.java:720)
at org.datanucleus.metadata.AbstractClassMetaData.determineSuperClassName(AbstractClassMetaData.java:642)
at org.datanucleus.metadata.ClassMetaData.populate(ClassMetaData.java:193)
at org.datanucleus.metadata.MetaDataManager$1.run(MetaDataManager.java:2317)
at java.security.AccessController.doPrivileged(Native Method)
at org.datanucleus.metadata.MetaDataManager.populateAbstractClassMetaData(MetaDataManager.java:2311)
at org.datanucleus.metadata.MetaDataManager.populateFileMetaData(MetaDataManager.java:2148)
at org.datanucleus.metadata.MetaDataManager.initialiseFileMetaDataForUse(MetaDataManager.java:864)
at org.datanucleus.metadata.MetaDataManager.loadPersistenceUnit(MetaDataManager.java:794)
at org.datanucleus.jpa.EntityManagerFactoryImpl.initialisePMF(EntityManagerFactoryImpl.java:488)
at org.datanucleus.jpa.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:355)
at org.datanucleus.store.appengine.jpa.DatastoreEntityManagerFactory.<init>(DatastoreEntityManagerFactory.java:63)
at org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider.createEntityManagerFactory(DatastorePersistenceProvider.java:35)
at javax.persistence.Persistence.createFactory(Persistence.java:172)
... 67 more

我也尝试在超类中添加用@Id注释的关键字段,但它没有给我任何结果

4

2 回答 2

2

如消息所述,您必须有一个 @Id 字段。

于 2011-01-18T10:15:17.390 回答
0

看起来这是增强或eclipse插件的一些问题,重新启动IDE后问题消失了。但只有这个ID的问题。实际上,我遇到了另一个与嵌入式实体相关的非常奇怪的问题。我有以下域模型:

@Entity
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key id;
    @Basic
    private String name;
    // Entity won't persist if location is not null, requires to persist with
    // further update
    @Embedded
    // @OneToOne(cascade = CascadeType.ALL) it works the same when I add or remove this line
    private Location location;

    /* getters and setters */
} 

@Embeddable
public class Location implements LocationCapable {
    @Basic
    @NotNull
    private Double latitude;
    @Basic
    @NotNull
    private Double longitude;
    @Basic
    private List<String> geocells;

    /* getters and setters */
} 

为了测试它,我有以下 JUnit 测试用例:

    @Test
    public void testSave() throws Exception {
        City city = new City("testCity1", new Location(1d, 1d));
        cityDao.persist(city);
        assertNotNull(city.getId());
    }

cityDao.persist(city)做的很简单jpaTemplate.persist(object);

当我尝试坚持这个实体时,我得到了以下异常:

Caused by: java.lang.IllegalArgumentException: out of field index :-1
    at com.myproject.model.Location.jdoProvideField(Location.java)
    at org.datanucleus.state.JDOStateManagerImpl.provideField(JDOStateManagerImpl.java:2585)
    at org.datanucleus.state.JDOStateManagerImpl.provideField(JDOStateManagerImpl.java:2555)
    at org.datanucleus.store.mapped.mapping.CollectionMapping.postInsert(CollectionMapping.java:91)
    at org.datanucleus.store.mapped.mapping.EmbeddedPCMapping.postInsert(EmbeddedPCMapping.java:104)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager.runPostInsertMappingCallbacks(DatastoreRelationFieldManager.java:217)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager.access$200(DatastoreRelationFieldManager.java:48)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager$1.apply(DatastoreRelationFieldManager.java:116)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager.storeRelations(DatastoreRelationFieldManager.java:81)
    at org.datanucleus.store.appengine.DatastoreFieldManager.storeRelations(DatastoreFieldManager.java:955)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.storeRelations(DatastorePersistenceHandler.java:546)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertPostProcess(DatastorePersistenceHandler.java:304)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObjects(DatastorePersistenceHandler.java:256)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:240)
    at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent(JDOStateManagerImpl.java:3185)
    at org.datanucleus.state.JDOStateManagerImpl.flush(JDOStateManagerImpl.java:4513)
    at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:2814)
    at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:2754)
    at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:2893)
    at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:369)
    at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:256)
    at org.datanucleus.jpa.EntityTransactionImpl.commit(EntityTransactionImpl.java:104)
    at org.datanucleus.store.appengine.jpa.DatastoreEntityTransactionImpl.commit(DatastoreEntityTransactionImpl.java:55)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)
    ... 41 more
于 2011-01-21T11:25:13.743 回答