我将OneToMany-Relationship存储到Map中时遇到问题。我在Spring中使用JPA / Hibernate。
这是我的代码:
@Table(name = "Room_Member", uniqueConstraints = @UniqueConstraint(
columnNames = { "room", "member" }))
public class RoomMember {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "room")
private Room room;
@ManyToOne(optional = false)
@JoinColumn(name = "member")
private Member member;
// Getters and Setters omitted
}
上课是我想要的地图:
public Room {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@OneToMany(mappedBy = "room")
@MapKeyJoinColumn(name = "member")
private Map<Member, RoomMember> roomMember;
// Getters and Setters omitted
}
我无法运行,因为我得到了NullPointerException
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 java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856)
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:542)
at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at de.swr.eva.EventAnwendungApplication.main(EventAnwendungApplication.java:40)
Caused by: java.lang.NullPointerException: null
at org.hibernate.envers.configuration.internal.metadata.CollectionMetadataGenerator.addValueToMiddleTable(CollectionMetadataGenerator.java:567)
at org.hibernate.envers.configuration.internal.metadata.CollectionMetadataGenerator.addIndex(CollectionMetadataGenerator.java:477)
at org.hibernate.envers.configuration.internal.metadata.CollectionMetadataGenerator.addOneToManyAttached(CollectionMetadataGenerator.java:212)
at org.hibernate.envers.configuration.internal.metadata.CollectionMetadataGenerator.addCollection(CollectionMetadataGenerator.java:157)
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.addValueInSecondPass(AuditMetadataGenerator.java:321)
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.addValue(AuditMetadataGenerator.java:354)
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.addProperties(AuditMetadataGenerator.java:374)
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.generateSecondPass(AuditMetadataGenerator.java:713)
at org.hibernate.envers.configuration.internal.EntitiesConfigurator.configure(EntitiesConfigurator.java:108)
at org.hibernate.envers.boot.internal.EnversServiceImpl.doInitialize(EnversServiceImpl.java:152)
at org.hibernate.envers.boot.internal.EnversServiceImpl.initialize(EnversServiceImpl.java:117)
at org.hibernate.envers.boot.internal.AdditionalJaxbMappingProducerImpl.produceAdditionalMappings(AdditionalJaxbMappingProducerImpl.java:99)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:288)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:875)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.__createEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.__createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 16 common frames omitted
如果使用ID作为密钥,它可以工作:
@OneToMany(mappedBy = "room")
@MapKey(name = "id")
private Map<String, RoomMember> roomMember;
但我需要会员作为地图密钥。 Ofc我可以手动构建这个地图,但我认为它应该自动生成。
这个按预期工作:
@OneToMany(mappedBy = "room")
private Set<RoomMember> roomMember;
你能看到我的失败并给我一个暗示吗?
祝福!
WiZo