首先是我的代码。
专业化
@Entity
public class Specialization {
@Id
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="att_id", referencedColumnName="ID")
private List<Attribute> attributes;
//Getters and Setters
}
属性
@Entity
public class Attribute {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private double scaling;
//Getters and Setters
}
我的问题是我得到以下异常:
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1460)
at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:864)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:779)
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)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
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.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
... 98 more
仅在将属性
attributes
添加到Class Specialization
后才发生此异常。因此,没有此属性,一切正常,但如果添加此属性,则会出现异常。我不明白我在做什么。也许有人可以发现我的错误?
附加信息:
Hibernate Core版本:4.3.7。最后。
还有另一个与Specificationization具有oneToMany关系的Entity
GameClass
=>每个GameClass
具有多个Specialization
,每个Specialization
具有多个Attribute
。不知道这是否重要,但这是一个Spring-Boot应用程序
最佳答案
我有我的错。睡觉有点帮助。
我把关系弄混了。
如果一个Specialization
具有许多Attribute
,则注释应如下所示:
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="specc_id", referencedColumnName = "name")
private List<Attribute> attributes;
和 NOT 像这样:
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="att_id", referencedColumnName="ID")
private List<Attribute> attributes;