我在定义hibernate实体时遇到了一个问题。定义如下:
实体同意:
@Entity
@Table(name = "Consent", schema = "OPTIO")
public class Consent {
@Id
@GeneratedValue(generator = "sequence-generator")
@Column(name = "Id" , columnDefinition="unique, NOT NULL")
private Integer Id;
@Column(nullable=false)
private Date CreatedOnUtc;
@NotNull
@ManyToOne(optional=false, fetch = FetchType.EAGER)
@JoinColumn(name="DisclaimerId", referencedColumnName = "Id", nullable=false, insertable=true, updatable=false)
private Disclaimer Disclaimer;
实体免责声明:
@Entity
@Table(name="DISCLAIMER", schema="OPTIO")
public class Disclaimer {
@Id
@GeneratedValue(generator = "sequence-generator2")
@Column(name = "Id" , columnDefinition="unique NOT NULL")
private Integer Id;
@Column(nullable = false)
private String Name;
应用程序无法加载。EntityManagerFactory定义引发异常
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [C:\EclipseWorkspace2021-JDK15\optioversion3\src\test\resources\optio_beans_test.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: optio_persistence_unit] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalStateException: Could not resolve binding for table `Consent`
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:142)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:95)
at com.netsol.optioversion3.test.OPTIOTestDriver.main(OPTIOTestDriver.java:62)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: optio_persistence_unit] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalStateException: Could not resolve binding for table `Consent`
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
... 12 more
Caused by: java.lang.IllegalStateException: Could not resolve binding for table `Consent`
at org.hibernate.sql.ast.tree.from.AbstractColumnReferenceQualifier.resolveTableReference(AbstractColumnReferenceQualifier.java:43)
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.createTableGroupJoin(ToOneAttributeMapping.java:572)
at org.hibernate.sql.ast.tree.from.TableGroupJoinProducer.createTableGroupJoin(TableGroupJoinProducer.java:31)
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.createTableGroupJoin(ToOneAttributeMapping.java:518)
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.lambda$generateFetch$0(ToOneAttributeMapping.java:425)
at org.hibernate.sql.ast.spi.FromClauseAccess.resolveTableGroup(FromClauseAccess.java:59)
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.generateFetch(ToOneAttributeMapping.java:422)
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.generateFetch(ToOneAttributeMapping.java:61)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.lambda$createFetchableBiConsumer$8(LoaderSelectBuilder.java:645)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.lambda$visitFetches$7(LoaderSelectBuilder.java:526)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.hibernate.persister.entity.AbstractEntityPersister.visitFetchables(AbstractEntityPersister.java:6412)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.visitFetches(LoaderSelectBuilder.java:525)
at org.hibernate.loader.ast.internal.LoaderSqlAstCreationState.visitFetches(LoaderSqlAstCreationState.java:138)
at org.hibernate.sql.results.graph.AbstractFetchParent.afterInitialize(AbstractFetchParent.java:30)
at org.hibernate.sql.results.graph.entity.internal.EntityResultImpl.<init>(EntityResultImpl.java:52)
at org.hibernate.sql.results.graph.entity.internal.EntityResultImpl.<init>(EntityResultImpl.java:34)
at org.hibernate.persister.entity.AbstractEntityPersister.createDomainResult(AbstractEntityPersister.java:1234)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.generateSelect(LoaderSelectBuilder.java:335)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.createSelect(LoaderSelectBuilder.java:132)
at org.hibernate.loader.ast.internal.SingleIdEntityLoaderStandardImpl.createLoadPlan(SingleIdEntityLoaderStandardImpl.java:179)
at org.hibernate.loader.ast.internal.SingleIdEntityLoaderStandardImpl.prepare(SingleIdEntityLoaderStandardImpl.java:53)
at org.hibernate.persister.entity.AbstractEntityPersister.prepareLoader(AbstractEntityPersister.java:4467)
at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:4458)
at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.finishInitialization(MappingMetamodelImpl.java:214)
at org.hibernate.metamodel.internal.RuntimeMetamodelsImpl.finishInitialization(RuntimeMetamodelsImpl.java:47)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:304)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1263)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
... 16 more
任何帮助都将不胜感激:
如果我改变主意 FetchType
至 LAZY
它向上加载,但在插入时失败 Consent
表条目。设置时 FetchType
至 LAZY
以及移除 @NotTNull
从 JoinColumn
但是传递空值 Disclaimer
示例它似乎工作正常,新行被插入/创建在底层oracledb中。
1条答案
按热度按时间jtoj6r0c1#
如果查看生成的语句以创建表:
你会看到的
id unique, NOT NULL not null
有两个not null
. 原因是你两者都在用@Id
以及columnDefinition = "unique NOT NULL"
在主键列上。这将导致生成上面错误生成的表创建语句。你应该移除
columnDefinition = "unique NOT NULL"
分开是因为你不需要这个。你已经在用了@Id
注解。这适用于两个表。你们两个都应该更新
Id
中的字段Consent
以及Disclaimer
班级。更新:
同意.java
免责声明.java
您只需将下面的两行添加到application.properties文件中,即可在控制台上查看生成的sql语句: