2015-02-17 84 views
0

我正在爲Neo4j在Hibernate OGM實現上做POC。在這一過程中,我創建了以下對象:Neo4j中的對象繼承hibernate OGM

@Entity 
public class AutoDealership { 


private String name; 

public AutoDealership(String nam){ 
    this.name = nam; 
} 
@Id 
public String getName(){ 
    return name; 
} 

private void setName(String n){ 
    name = n; 
} 

private List<Vehicle> vehicle = new ArrayList<Vehicle>(); 

@OneToMany(mappedBy="autoDealership") 
public List<Vehicle> getVehicle(){ 
    return vehicle; 
} 

private void setVehicles(List<Vehicle> v){ 
    this.vehicle = v; 
} 

} 

和車輛類:

public abstract class Vehicle { 

protected AutoDealership autoDealership; 
abstract int getValue(); 

@ManyToOne 
public abstract AutoDealership getAutoDealership(); 

private void setValue(int v){ 

} 
} 

這是擴展到有簡單的實現類。我運行此代碼如下:

 EntityManager em = emf.createEntityManager(); 
     EntityTransaction tx = em.getTransaction(); 
     tx.begin(); 
     OgmSession session =em.unwrap(OgmSession.class); 
     Camry camry1 = new Camry("Joe"); 
     Camry camry2 = new Camry("Elvis"); 
     Silverado s = new Silverado("TX"); 
     Maserati mas = new Maserati(); 
     mas.setStatus("Massive"); 
     session.saveOrUpdate(camry1); 
     session.saveOrUpdate(camry2); 
     session.saveOrUpdate(s); 
     session.saveOrUpdate(mas); 
     AutoDealership a = new AutoDealership("Slick Willie's"); 
     session.saveOrUpdate(a); 
     camry1.setAutoDealership(a); 
     camry2.setAutoDealership(a); 
     mas.setAutoDealership(a); 
     s.setAutoDealership(a); 
     tx.commit(); 
     em.close(); 
     emf.close(); 

當我運行它,它失敗:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: ogm-neo4j] Unable to build Hibernate SessionFactory 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1239) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:855) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845) 
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) 
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75) 
at org.hibernate.ogm.jpa.HibernateOgmPersistence.createEntityManagerFactory(HibernateOgmPersistence.java:62) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) 
at dogs.TestClass.main(TestClass.java:22) 
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: cars.AutoDealership.vehicle[cars.Vehicle] 
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1134) 
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:793) 
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728) 
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70) 
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697) 
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) 

如果我註釋掉與從AutoDealership獲取車輛什麼,代碼編譯和作品,成功地將節點插入到數據庫中。我試圖改變mappedBy目標無濟於事。

回答

1

事實證明,問題是我不得不宣佈Vehicle作爲實體。