gpt4 book ai didi

java - 添加实体列表时的Spring Data jpa( hibernate )空指针异常

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:10 24 4
gpt4 key购买 nike

首先是我的代码。

特化

@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 添加到类 Specialization 后发生。所以没有这个属性一切正常,但是如果我添加这个属性,就会出现异常。我不明白我做错了什么。也许有人能发现我的错误?

附加信息:

Hibernate 核心版本:4.3.7。最终。

还有另一个实体 GameClass 与 Specialization 具有一对多关系 => 每个 GameClass 都有多个 Specialization 并且每个 Specialization 有多个 Attribute

不知道这是否重要,但这是一个 Spring-Boot 应用程序

最佳答案

我错了。睡一点有帮助。我混淆了这些关系。

如果一个Specialization有很多Attribute,注解应该是这样的:

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

关于java - 添加实体列表时的Spring Data jpa( hibernate )空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com