1

I am getting a null pointer exception when i try to call a getter for a relationship. it is my understanding that the container will fill this field in with an appropriate list object whilst the entity is still managed.

the schema was pre-existing so this is a bottom up mapping.

This is my 'one' side entity of the onetomany relationship:

@Entity
@Table(name=CollectorHeader.TABLE_NAME)
public class CollectorHeader implements Serializable {
    ...
    @Id
    @Column(name = "COLLECTORHEADERID")
    private long id;

    @OneToMany(mappedBy="collectorHeader", fetch=FetchType.LAZY)
    private List<CollectorDetail> collectorDetails;
    ...
}

And here is my 'many' side entity :

@Entity
@Table(name = CollectorDetail.TABLE_NAME)
public class CollectorDetail implements Serializable {
    ...
    @Id
    @Column(name = "COLLECTORDETAILID", unique = true)
    long id;
    ...
    @NotNull 
    @ManyToOne
    @JoinColumn(name = "COLLECTORHEADERID")
    private CollectorHeader collectorHeader;
    ...
    public CollectorDetail(long id, @NotNull CollectorHeader collectorHeader, 
                           long provenanceLinkPk, @NotNull String provenanceLinkClass) {
        setId(id);
        setCollectorHeader(collectorHeader);
        setProvenanceLinkPk(provenanceLinkPk);
        setProvenanceLinkClass(provenanceLinkClass);
    }
}

And this is where i am calling the relationship:

public CollectorDetail createCollectorDetail(long collectorHeaderId, long     provenanceLinkPk, @NotNull String provenanceLinkClass) throws SystemException {
    CollectorHeader collectorHeader = em.find(CollectorHeader.class, collectorHeaderId);
    if(collectorHeader == null) {
        String error = "There is no Collector Header with the id: '" + collectorHeaderId + "'";
        log.error(error);
        throw new SystemException(error);
    }
    CollectorDetail collectorDetail =
        new CollectorDetail(NextNumberFactory.getInstance().getNextNumberLong("cocollectordetail")
                , collectorHeader
                , provenanceLinkPk
                , provenanceLinkClass);
    collectorHeader.getCollectorDetails().add(collectorDetail); //NULLPOINTEREXCEPTION
    em.merge(collectorDetail);
    em.merge(collectorHeader);
    return collectorDetail;
}

Sql Schema:

CREATE TABLE COCOLLECTORHEADER (
    COLLECTORHEADERID   DECIMAL(20,0) NOT NULL PRIMARY KEY,
    COLLECTEDTIMESTAMP TIMESTAMP, -- the date when the information was collected
    PROCESSEDTIMESTAMP TIMESTAMP, -- the date when the information was processed
    FILEFORMAT VARCHAR(16) NOT NULL,
    SEQUENCENUMBER INTEGER
);

CREATE TABLE COCOLLECTORDETAIL (
    COLLECTORDETAILID   DECIMAL(20,0) NOT NULL PRIMARY KEY,
    COLLECTORHEADERID   DECIMAL(20,0) NOT NULL
        REFERENCES COCOLLECTORHEADER(COLLECTORHEADERID),
    PROVENANCELINKPK        DECIMAL(20,0) NOT NULL,
    PROVENANCELINKCLASS VARCHAR(128) NOT NULL
);

Any assistance would be much appreciated.

4

1 回答 1

3

使用有效的集合对象初始化collectorDetails为:

private List<CollectorDetail> collectorDetails = new ArrayList<CollectorDetail>();

还要检查初始化 JPA 实体 getter 中的字段是一种好习惯吗?

于 2013-09-02T07:23:29.820 回答