Spring Hibernate Annotation用于连接具有不同关系的多个表

时间:2017-05-14 12:24:28

标签: java spring hibernate annotations

我有以下类的设置。如您所见,实体之间的连接如下:Passager(1-n)Buchung(n-1)Faehrfahrt(n-1)Faehre(n-1)Strecke。我尝试将上面的逻辑实现到我的hibernate设置中。但是,如果我开始这个(开始意味着:使用gradlew构建命令,因为我的项目是Spring,Gradle,Java))我收到以下错误消息(参见本尾声明),这基本上说我对1-To的理解 - 许多关系错了,例如我尝试将非集合映射为集合。但是,我没有看到我在做这个错误的地方,因为根据我的理解,所有关系都被正确注释。那为什么它不起作用?

感谢您的帮助! :)

@Entity
public class Buchung { 

@Id
@GeneratedValue
private UUID id;    

@JsonIgnore
@ManyToOne()
private Faehrfahrt faehrfahrt;

@JsonIgnore
@ManyToOne
private Passagier passagier;

...

@Entity
public class Passagier {



@Id
@GeneratedValue
private UUID id;


@OneToMany(cascade = CascadeType.ALL,mappedBy = "buchung")
@JoinColumn(name = "passagier_id")
private ArrayList<Buchung> buchungen = new ArrayList<>();

...

@Entity
public class Faehre {    

@Id
@GeneratedValue
private UUID id;


@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="faehre_id")
private List<Faehrfahrt> faehrfahrten = new ArrayList<>();

@JsonIgnore
@ManyToOne
private Strecke strecke;

...

@Entity
public class Faehrfahrt {

private Date zeitpunkt;

@Id
@GeneratedValue
private UUID id;

@JsonIgnore
@ManyToOne
private Faehre faehre;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "buchung")
@JoinColumn(name="faehrfahrt_id")
private ArrayList<Buchung> buchungen = new ArrayList<>();

...

@Entity
public class Strecke {

@Id
@GeneratedValue
private UUID id;


@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="strecke_id")
private List<Faehre> faehren = new ArrayList<>();

...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: aiApp.applicationcore.faehrecomponent.Faehrfahrt.buchungen
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at aiApp.applicationcore.Application.main(Application.java:35) [main/:na]
Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: aiApp.applicationcore.faehrecomponent.Faehrfahrt.buchungen
        at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:322) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1892) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:911) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:738) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        ... 16 common frames omitted

1 个答案:

答案 0 :(得分:1)

好的,根据设置,您的配置应该或多或少类似于以下内容:

<强> Buchung

import Data.Char

chatToMaybeInt :: Char -> Maybe Int
chatToMaybeInt '.' = Nothing
chatToMaybeInt  x  = Just $ digitToInt x

main = putStrLn $ show $ fmap chatToMaybeInt "13.2..2"

<强> Passagier

@Entity
public class Buchung { 

   @JsonIgnore
   @ManyToOne
   @JoinColumn(name = "faehrfahrt_id")
   private Faehrfahrt faehrfahrt;

   @JsonIgnore
   @ManyToOne
   @JoinColumn(name = "passagier_id") 
   private Passagier passagier;

}

<强> Faehre

@Entity
public class Passagier {

   @OneToMany(cascade = CascadeType.ALL,mappedBy = "passagier")
   private List<Buchung> buchungen = new ArrayList<>();

}

<强> Faehrfahrt

@Entity
public class Faehre {    

   @OneToMany(cascade = CascadeType.ALL, mappedBy="faehre")
   private List<Faehrfahrt> faehrfahrten = new ArrayList<>();

   @JsonIgnore
   @ManyToOne
   @JoinColumn(name = "strecke_id")
   private Strecke strecke;

}

<强> Strecke

@Entity
public class Faehrfahrt {

   @JsonIgnore
   @ManyToOne
   @JoinColumn(name = "faehre_id")
   private Faehre faehre;

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "faehrafhrt")
   private List<Buchung> buchungen = new ArrayList<>();

}