问题描述
首先是我的代码。 专业化
@Entity
公共类专业化{
@Id
私人字符串名称;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name =att_id,referencedColumnName =ID)
private List< Attribute>属性;
// Getters and Setters
}
属性
@Entity
公共类属性{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私人长ID;
私人字符串名称;
私人双倍缩放;
// Getters and Setters
}
我的问题是我得到以下异常:
由org.hibernate.cfg引发java.lang.NullPointerException
。 annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1460)
处org.hibernate.cfg.annotations.CollectionBinder org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:864)
。 bindStarToManySecondPass(CollectionBinder.java:779)
在org.hibernate.cfg.annotations.CollectionBinder $ 1.secondPass(CollectionBinder.java:728)
在org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java :在org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697 70)
)
在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
。在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:852)$ B $在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:845)$ B B $ b。在org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
在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
仅在添加属性属性后发生此异常。到类专业化。所以没有这个属性一切正常,但如果我添加此属性,异常出现。我不明白我做错了什么。
也许有人可以找到我的错误?
其他信息: Hibernate核心版本:4.3.7。最后。 还有另一个实体 GameClass ,与Specialization = OneToMany的关系每个 GameClass 有多个 Specialization s,每一个 Specialization 都有多个属性 s。 不知道这是否重要,但这是一个Spring-Boot应用程序 我弄错了。睡觉有点帮助。 和不是像这样: first my Code. Specialization Attribute My Problem is that i get a following Exception: This Exception only Occurs after adding the attribute attributes to the Class Specialization. So without this attribute everything works fine, but if I add this attribute, the exception comes up. I don't get what I'm doing wrong.Maybe somebody could find my mistake? Additional information: Hibernate Core Version: 4.3.7. Final. There is another Entity GameClass with a oneToMany relationship to Specialization => Every GameClass has multiple Specializations and every Specialization has multiple Attributes. Don't know if this matters but this is a Spring-Boot application I've got my mistake. Sleeping a bit helped.I've mixed up the relations. If one Specialization has many Attributes, the annotations should look like this: and NOT like this: 这篇关于Spring Data jpa(hibernate)nullpointer添加实体列表时的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
如果一个 Specialization 有许多属性 s,注释应该如下所示:
@OneToMany(cascade = CascadeType.ALL )
@JoinColumn(name =specc_id,referencedColumnName =name)
private List< Attribute>属性;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name =att_id,referencedColumnName =ID)
private List<属性>属性;
@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
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="specc_id", referencedColumnName = "name")
private List<Attribute> attributes;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="att_id", referencedColumnName="ID")
private List<Attribute> attributes;