JPA ManyToOne实体映射

时间:2015-04-23 17:21:38

标签: java hibernate jpa persistence

使用" OneToMany"部署带有实体的Web项目时遇到问题映射。这是第一个实体:

    @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;
    }

这是第二个依赖于复合键的实体:

@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;
}

这是关键:

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

    //Also includes overriden hash/equals
}

部署后,我收到一个模糊的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

OneToMany / ManyToOne注释引入了此异常。以前的成功是通过简单地单独填充/保持两个实体来实现的,这当然不是理想的。

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

您是否尝试更换:

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

使用:

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

或:

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

希望这可以帮助您解决问题!