简体   繁体   中英

JPA ManyToOne entity mapping

Having issues deploying a web project with entities using "OneToMany" mapping. Here is the first entity:

    @Entity 
    @Table(name = "OWNER")
    public class Owner implements Serializable
    {
        @Id
        @Column(name = "ID", nullable = false)
        private double ID;

        @Column(name = "NAME", nullable = false, length = 16)
        private String name;

        @OneToMany(cascade={CascadeType.ALL}, mappedBy="USER_ID", targetEntity=Asset.class)
        private List<Asset> assets;
    }

Here is the second Entity which is reliant upon a composite key:

@Entity
@IdClass(AssetKey.class)
@Table(name = "ASSETS")
public class Asset implements Serializable
{
    @Id
    @ManyToOne
    @JoinColumn(name = "USER_ID")
    private double USER_ID;

    @Id
    @Column(name = "ASSET_ID")
    private String asset;
}

Here is the key:

public class ConstraintAnchorAssetImplKey implements Serializable
{
    private double CA_ID;
    private String asset;

    //Also includes overriden hash/equals
}

Upon deployment I receive an obscure NullPointerException:

12:59:59,841 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 50) MSC000001: Failed to start service jboss.persistenceunit."MyProj.war#MyProjJPA": org.jboss.msc.service.StartException in service jboss.persistenceunit."MyProj.war#MyProjJPA": java.lang.NullPointerException 
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:103) [jboss-as-jpa-7.4.3.Final-redhat-2.jar:7.4.3.Final-redhat-2]  
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_05]    
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_05]    
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_05]  
at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final-redhat-1.jar:2.1.1.Final-redhat-1] 
Caused by: java.lang.NullPointerException   
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1461)  
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1391)   
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1786)     at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)  
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)    
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900)    
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76)     
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:200) [jboss-as-jpa-7.4.3.Final-redhat-2.jar:7.4.3.Final-redhat-2]    
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.access$600(PersistenceUnitServiceImpl.java:57) [jboss-as-jpa-7.4.3.Final-redhat-2.jar:7.4.3.Final-redhat-2]  
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:99) [jboss-as-jpa-7.4.3.Final-redhat-2.jar:7.4.3.Final-redhat-2]   ... 4 more

This exception is introduced with the OneToMany/ManyToOne annotations. Previous success was achieved by simply populating/persisting the two entities separately which is of course not ideal.

Thanks for any advice!

Did you try to replace:

@OneToMany(cascade={CascadeType.ALL}, mappedBy="USER_ID", targetEntity=Asset.class)
private List<Asset> assets;

with:

@OneToMany(cascade={CascadeType.ALL}, mappedBy="USER_ID", targetEntity=Asset.class)
private Set<Asset> assets = new HashSet<Asset>(0);

or:

@OneToMany(cascade={CascadeType.ALL}, mappedBy="USER_ID", targetEntity=Asset.class)
private List<Asset> assets = new ArrayList<Asset>(0);

Hope this could help you resolve problems!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM