Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JPA和Hibernate为2个表建立单向@oneToMany关系_Java_Spring_Hibernate_Jpa_Annotations - Fatal编程技术网

Java 如何使用JPA和Hibernate为2个表建立单向@oneToMany关系

Java 如何使用JPA和Hibernate为2个表建立单向@oneToMany关系,java,spring,hibernate,jpa,annotations,Java,Spring,Hibernate,Jpa,Annotations,我的项目基于spring boot、Thymeleaf、mysql、html和Jquery。我试图在maintable中创建一个带有单向的@oneToMany关系,但它只会产生如下错误 2017-10-23 16:17:49.908 ERROR 18724 --- [ main] o.s.boot.SpringApplication : Application startup failed org.springframework.beans.

我的项目基于spring boot、Thymeleaf、mysql、html和Jquery。我试图在maintable中创建一个带有单向的@oneToMany关系,但它只会产生如下错误

2017-10-23 16:17:49.908 ERROR 18724 --- [           main] o.s.boot.SpringApplication               : Application startup failed
 
 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: **Unable to map collection
 com.vfraternity.process.entity.EntPropertyMaster.blockListPropSub**   at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1702)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
 ~[spring-context-5.0.0.RC4.jar:5.0.0.RC4]     at
 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858)
 ~[spring-context-5.0.0.RC4.jar:5.0.0.RC4]     at
 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
 ~[spring-context-5.0.0.RC4.jar:5.0.0.RC4]     at
 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122)
 ~[spring-boot-2.0.0.M4.jar:2.0.0.M4]  at
 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
 [spring-boot-2.0.0.M4.jar:2.0.0.M4]   at
 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
 [spring-boot-2.0.0.M4.jar:2.0.0.M4]   at
 org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
 [spring-boot-2.0.0.M4.jar:2.0.0.M4]   at
 org.springframework.boot.SpringApplication.run(SpringApplication.java:1245)
 [spring-boot-2.0.0.M4.jar:2.0.0.M4]   at
 org.springframework.boot.SpringApplication.run(SpringApplication.java:1233)
 [spring-boot-2.0.0.M4.jar:2.0.0.M4]   at
 com.vfraternity.VfSpringBootMain.main(VfSpringBootMain.java:12)
 [classes/:na] Caused by: org.hibernate.AnnotationException: Unable to
 map collection
 com.vfraternity.process.entity.EntPropertyMaster.blockListPropSub     at
 org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1604)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:871)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:786)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)
 ~[spring-orm-5.0.0.RC4.jar:5.0.0.RC4]     at
 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:358)
 ~[spring-orm-5.0.0.RC4.jar:5.0.0.RC4]     at
 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:384)
 ~[spring-orm-5.0.0.RC4.jar:5.0.0.RC4]     at
 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:373)
 ~[spring-orm-5.0.0.RC4.jar:5.0.0.RC4]     at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods AbstractAutowireCapableBeanFactory.java:1761)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698)
 ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]   ... 16 common frames omitted
 Caused by: org.hibernate.cfg.RecoverableException: Unable to find
 column with logical name: propertysubpk in
 org.hibernate.mapping.Table(property_master) and its related
 supertables and secondary tables  at
 org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:831)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:243)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   at
 org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1594)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   ... 31 common frames
 omitted Caused by: org.hibernate.MappingException: Unable to find
 column with logical name: propertysubpk in
 org.hibernate.mapping.Table(property_master) and its related
 supertables and secondary tables  at
 org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:826)
 ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]   ... 33 common frames
 omitted
我试图实现单向使用

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="propertysubfkid", referencedColumnName="propertysubpk")
private List<EntPropertySub> blockListPropSub = newArrayList<EntPropertySub>();

请帮助我解决此问题。.提前感谢

此错误清楚地显示您的架构关系中缺少映射

您需要在
EntPropertySub
中映射
EntPropertyMaster

@OneToMany(cascade=CascadeType.ALL, mappedBy="entPropertyMaster")
private List<EntPropertySub> blockListPropSub = newArrayList<EntPropertySub>();
JPA中的关系始终是单向的,除非你联系 父母和孩子在两个方向上。级联删除 从父级到子级的操作需要从 父对象是子对象(而不是相反)


在@OneToMany关系上使用@JoinColumn时,
name
属性将指向相关表列,即
EntPropertySub
实体,
ReferenceColumnName
应指向存在@OneToMany注释的实体的列,即
EntPropertyMaster

所以基本上你有另一种说法,我认为:

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="propertysubpk", referencedColumnName="propertysubfkid")
private List<EntPropertySub> blockListPropSub = newArrayList<EntPropertySub>();
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name=“propertysubpk”,referencedColumnName=“propertysubfkid”)
私有列表blockListPropSub=newArrayList();

试试这个:确保EntPopertySub表中的外键是如何调用的

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="propertyid")
private List<EntPropertySub> blockListPropSub = new ArrayList<EntPropertySub>();
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name=“propertyid”)
私有列表blockListPropSub=新的ArrayList();
无法在主表上创建物理列,因为您具有OneToMany关系。OneToMany意味着Master有许多子表引用

让我们解释一下问题:

如果主表中有一列,则需要在一个单元格中添加引用列表

比如说

------------------
|子fk|

|1,2,3,4 |错误的原因是:

在org.hibernate.mapping.Table(property\u master)中找不到逻辑名称为propertysubpk的列。

原因是
referencedColumnName
属性错误。它应该是:

JPA 2.11.1.25:引用列的名称 通过此外键列。什么时候 与实体关系映射一起使用 除了下面描述的情况外, 引用的列位于目标的表中 实体。当与单向开关一起使用时 OneToMany外键映射,引用 列位于 源实体。在连接中使用时- 表注释,引用的键列 位于所属实体的实体表中 实体,或反向实体(如果联接是的一部分) 反向联接定义。当用于 集合表映射,引用的 列位于包含 收藏

解释:除非指定了
referencedColumnName
,否则
@JoinColumn
在关系的多个方面(此处为
EntPropertySub
)创建一个列,该列具有引用父实体主键的给定名称。在后一种情况下,“many”表中的FK引用了
referencedColumnName
中给出的列。在本例中,您希望引用
EntPropertyMaster
的主键,因此
referencedColumnName
是多余的。只需使用:

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="propertysubfkid")
private List<EntPropertySub> blockListPropSub = newArrayList<EntPropertySub>();
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name=“propertysubfkid”)
私有列表blockListPropSub=newArrayList();

最后我得到了答案

我对主实体“EntPropertyMaster”进行了更改

“@OneToMany(cascade=CascadeType.ALL)”
“@JoinColumn(name=“propertyidfk”)”
私有列表blockListPropSub=新的ArrayList();
它将在子表中创建列,但我在父实体中编写了此代码

请注意,我们需要为列创建提供唯一的名称,然后只有它才能在子表中创建列。。。。不是primarykey列名 子/父表的名称


隐马尔可夫模型。。使用此解决方案,他将提供双向映射感谢回复好友,仍然得到错误…标记为mappedBy的关联不能定义像“@”JoinTable或“@”JointColumn这样的数据库映射:com.vfraternity.process.entity.EntPropertyMaster.blockListPropSub…我编写了一个类似OneToMany的代码(cascade=CascadeType.ALL,mappedBy=“EntPropertyMaster”)@mimu1011不,两个实体不相互依赖。只有单向交通。引用的第二个实体只是映射其他实体entity@VigneshR您需要在EntPropertySub模型中添加映射实体,这使其成为双向的,对吗?…我不明白..您能用代码解释一下我需要做哪些更改吗?它可以工作,但是它没有在主表中创建一个sperate列为什么?但是我需要的是一个物理列,也需要在主表中创建一个子表…请用代码解释…谢谢你更新了帖子。我希望你能理解。是的,你说得对,但是我们可以创建一个主表引用到子表是可能的。所以最后我在这段代码中做了“@OneToMany(cascade=CascadeType.ALL)”“@JoinColumn(name=“propertyidfk”)“private List blockListPropSub=new ArrayList();它将在子表中创建列,但我在父实体中编写了此代码…请注意,我们需要指定唯一的名称,而不是子/父表的primarykey列名..然后只在子表中创建一个单独的列,因为我们使用的是OneToMany关系…它可以工作,但是它没有在主表中创建一个列为什么?但是我需要的是一个物理列,也需要在主表中创建一个子表…请用代码解释..谢谢你朋友为什么要在主表中创建列?1-many关系的工作方式(1是主表,many是子表)是在子表引用中放置FK列
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="propertysubpk", referencedColumnName="propertysubfkid")
private List<EntPropertySub> blockListPropSub = newArrayList<EntPropertySub>();
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="propertyid")
private List<EntPropertySub> blockListPropSub = new ArrayList<EntPropertySub>();
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="propertysubfkid")
private List<EntPropertySub> blockListPropSub = newArrayList<EntPropertySub>();
"@OneToMany(cascade=CascadeType.ALL)"
"@JoinColumn(name="propertyidfk")"  
private List<EntPropertySub> blockListPropSub = new ArrayList<EntPropertySub>();