gpt4 book ai didi

java - Spring JPA hibernate H2

转载 作者:行者123 更新时间:2023-11-30 05:26:23 25 4
gpt4 key购买 nike

尝试使用 Spring Data Repository 保存数据时,收到可嵌入实体的 javax.persistence.EntityNotFoundException。似乎 select 语句没有找到我的嵌入实体 CommunicationPreference 的数据。我该如何修复这个错误?

堆栈跟踪:

org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.talentreef.notification.recipient.persistent.models.CommunicationPreference with id 61390ce8-719c-44e4-9cab-57070d728456; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.talentreef.notification.recipient.persistent.models.CommunicationPreference with id 61390ce8-719c-44e4-9cab-57070d728456

at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:378)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:144)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$ExposeRepositoryInvocationInterceptor.invoke(CrudMethodMetadataPostProcessor.java:364)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy106.save(Unknown Source)
at com.talentreef.notification.recipient.persistent.services.RecipientPersistService.upsert(RecipientPersistService.java:33)
at com.talentreef.notification.recipient.persistent.services.RecipientPersistentServiceTest.shouldSaveRecipient(RecipientPersistentServiceTest.java:127)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: javax.persistence.EntityNotFoundException: Unable to find com.talentreef.notification.recipient.persistent.models.CommunicationPreference with id 61390ce8-719c-44e4-9cab-57070d728456
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:162)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:234)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:350)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:127)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:93)
at org.hibernate.internal.SessionImpl.fireLoadNoChecks(SessionImpl.java:1288)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1160)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:682)
at org.hibernate.type.EntityType.resolve(EntityType.java:464)
at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239)
at org.hibernate.type.EntityType.resolve(EntityType.java:457)
at org.hibernate.type.EntityType.replace(EntityType.java:358)
at org.hibernate.type.AbstractType.replace(AbstractType.java:164)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:203)
at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:502)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:257)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:334)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:188)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:927)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:897)
at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:261)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:490)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:415)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:216)
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:523)
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:455)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:418)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:216)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:149)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:459)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:263)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:334)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:188)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:72)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:905)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:891)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:309)
at com.sun.proxy.$Proxy104.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:538)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 43 more

hibernate 查询:

Hibernate: 
select
recipient0_.recipient_id as recipien1_1_1_,
recipient0_.alias as alias2_1_1_,
recipient0_.email as email3_1_1_,
recipient0_.firstname as firstnam4_1_1_,
recipient0_.lastname as lastname5_1_1_,
recipient0_.mobile as mobile6_1_1_,
audiences1_.recipient_recipient_id as recipien1_3_3_,
audience2_.audience_id as audience2_3_3_,
audience2_.audience_id as audience1_2_0_,
audience2_.alias as alias2_2_0_,
audience2_.client_id as client_i3_2_0_,
audience2_.recipient_id as recipien4_2_0_,
audience2_.type as type5_2_0_
from
notification.recipient recipient0_
left outer join
notification.recipient_audiences audiences1_
on recipient0_.recipient_id=audiences1_.recipient_recipient_id
left outer join
notification.recipient_audience audience2_
on audiences1_.audiences_audience_id=audience2_.audience_id
where
recipient0_.recipient_id=?
Hibernate:
select
audience0_.audience_id as audience1_2_1_,
audience0_.alias as alias2_2_1_,
audience0_.client_id as client_i3_2_1_,
audience0_.recipient_id as recipien4_2_1_,
audience0_.type as type5_2_1_,
followers1_.audience_id as audience2_4_3_,
followers1_.follower_id as follower1_4_3_,
followers1_.follower_id as follower1_4_0_,
followers1_.audience_id as audience2_4_0_,
followers1_.client_id as client_i3_4_0_,
followers1_.follower_recipient as follower4_4_0_,
followers1_.recipient_id as recipien5_4_0_
from
notification.recipient_audience audience0_
left outer join
notification.recipient_follower followers1_
on audience0_.audience_id=followers1_.audience_id
where
audience0_.audience_id=?
Hibernate:
select
follower0_.follower_id as follower1_4_0_,
follower0_.audience_id as audience2_4_0_,
follower0_.client_id as client_i3_4_0_,
follower0_.follower_recipient as follower4_4_0_,
follower0_.recipient_id as recipien5_4_0_
from
notification.recipient_follower follower0_
where
follower0_.follower_id=?
Hibernate:
select
recipientp0_.preference_id as preferen1_7_0_,
recipientp0_.client_id as client_i2_7_0_,
recipientp0_.list_id as list_id6_7_0_,
recipientp0_.last_modified as last_mod3_7_0_,
recipientp0_.recipient_id as recipien4_7_0_,
recipientp0_.source_type as source_t5_7_0_
from
notification.recipient_preference recipientp0_
where
recipientp0_.preference_id=?
Hibernate:
select
communicat0_.list_id as list_id1_0_0_,
communicat0_.email as email2_0_0_,
communicat0_.sms as sms3_0_0_,
communicat0_.stream as stream4_0_0_
from
notification.preference_list communicat0_
where
communicat0_.list_id=?

数据模型:

@Builder
@Getter
@Entity
@Embeddable
@Table(name = RecipientPreferenceTable.NAME, schema = Notification.SCHEMA_NAME)
public class RecipientPreference {

@Id
@Column(name = RecipientPreferenceTable.COLUMN_PREFERENCE_ID, unique = true, length = 16)
private UUID id;

@Column(name = RecipientPreferenceTable.COLUMN_RECIPIENT_ID, nullable = false, length = 16)
private UUID recipientId;

@Column(name = RecipientPreferenceTable.COLUMN_CLIENT_ID, nullable = false)
private String clientId;

@OneToOne
@AttributeOverride(name = "id", column = @Column(name = CommunicationPreferenceTable.COLUMN_LIST_ID, nullable = false))
@JoinColumn(name = CommunicationPreferenceTable.COLUMN_LIST_ID)
private CommunicationPreference communicationPreference;

@Enumerated(EnumType.STRING)
@Column(name = RecipientPreferenceTable.COLUMN_SOURCE_TYPE)
private SourceType sourceType;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = RecipientPreferenceTable.COLUMN_LAST_MODIFIED)
private Date lastModified;

protected RecipientPreference() {}

public RecipientPreference(UUID id,
UUID recipientId,
String clientId,
CommunicationPreference communicationPreference,
SourceType sourceType,
Date lastModified) {

this.id = id;
this.recipientId = recipientId;
this.clientId = clientId;
this.communicationPreference = communicationPreference;
this.sourceType = sourceType;
this.lastModified = lastModified;
}
}

@Builder
@Getter
@Entity
@Embeddable
@Table(name = CommunicationPreferenceTable.NAME, schema = Notification.SCHEMA_NAME)
public class CommunicationPreference {

@Id
@Column(name = CommunicationPreferenceTable.COLUMN_LIST_ID, length = 16)
private UUID id;
private boolean sms;
private boolean email;
private boolean stream;

protected CommunicationPreference(){}

public CommunicationPreference(UUID id, boolean sms, boolean email, boolean stream) {
this.id = id;
this.sms = sms;
this.email = email;
this.stream = stream;
}
}

保存时填充的数据: enter image description here

最佳答案

数据库中不存在 ID 为 61390ce8-719c-44e4-9cab-57070d728456 的 CommunicationPreference

它应该在测试之前创建,或者应修改 RecipientPreferenceCommunicationPreference 的 OneToOne 关系以允许自动创建:

public class RecipientPreference {

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@AttributeOverride(name = "id", column = @Column(name = CommunicationPreferenceTable.COLUMN_LIST_ID, nullable = false))
@JoinColumn(name = CommunicationPreferenceTable.COLUMN_LIST_ID)
private CommunicationPreference communicationPreference;

}

A beginner’s guide to JPA and Hibernate Cascade Types

关于java - Spring JPA hibernate H2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495385/

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