我遇到以下情况:
BasicEntity.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public abstract class BasicEntity {
private Long id;
private String name; // represents full name of the entity
public BasicEntity() {
// TODO Auto-generated constructor stub
}
public BasicEntity(String name) {
this.name = name;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof BasicEntity))
return false;
BasicEntity other = (BasicEntity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Institution.java
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class Institution extends BasicEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6803767084313616361L;
private Set<Activity> activities = new HashSet<>();
public Institution() {
}
public Institution(String name) {
super(name);
}
@JsonIgnore
@OneToMany(mappedBy = "institution", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Activity> getActivities() {
return this.activities;
}
public void setActivities(Set<Activity> activities) {
this.activities.clear();
this.activities.addAll(activities);
}
@Override
public int hashCode() {
String name = getName();
Long id = getId();
int result = 17;
final int prime = 31;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
String name = getName();
Long id = getId();
if (this == obj)
return true;
if ((obj == null) || getClass() != obj.getClass())
return false;
Institution that = (Institution) obj;
if (id == null) {
if (that.getId() != null)
return false;
} else if (!id.equals(that.getId()))
return false;
if (!name.equals(that.getName())) {
return false;
}
return true;
}
public void addActivity(Activity activity) {
if (!this.activities.contains(activity)) {
this.activities.add(activity);
activity.setInstitution(this);
}
}
public void removeActivity(Activity activity) {
if (this.activities.contains(activity)) {
this.activities.remove(activity);
activity.setInstitution(null);
}
}
}
Activity.java
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Activity extends BasicEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 989891156132782598L;
private BigDecimal price;
private Institution institution;
public Activity() {
// TODO Auto-generated constructor stub
}
public Activity(String activityName, BigDecimal price) {
super(activityName);
this.price = price;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "institution_id")
public Institution getInstitution() {
return institution;
}
public void setInstitution(Institution institution) {
if (sameInstitution(institution)) return;
Institution formerInstituion = this.institution;
this.institution = institution;
if (formerInstituion != null) formerInstituion.removeActivity(this);
if (this.institution != null) this.institution.addActivity(this);
}
@Override
public int hashCode() {
StringBuffer name = new StringBuffer(getName());
final int prime = 31;
return prime * ((getName() == null) ? 0 : name.hashCode() % 89);
}
@Override
public boolean equals(Object obj) {
Long id = getId();
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Activity other = (Activity) obj;
if (this.getName().equals(other.getName()))
return false;
if (id == null) {
if (other.getId() != null)
return false;
} else if (!id.equals(other.getId()))
return false;
return true;
}
private boolean sameInstitution(Institution newInstitution) {
return (institution == null) ? (newInstitution == null) : institution.equals(newInstitution);
}
}
我正在测试:
InstitutionRepositoryTest.java
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
import com.kikia.itacon.domain.Activity;
import com.kikia.itacon.domain.Institution;
@RunWith(SpringRunner.class)
@DataJpaTest
public class InstitutionRepositoryTest {
@Autowired
private InstitutionRepository institutionRepository;
@Autowired
private TestEntityManager testEntityManager;
@Before
public void setUp() {
}
@Test
public void getInstitution_shouldReturnInstitutionInfo() {
Optional<Institution> savedInstitution = Optional
.ofNullable(testEntityManager.persistFlushFind(new Institution("Pesca")));
Optional<Institution> institution = institutionRepository.findInstitutionByName("Pesca");
assertThat(institution.get().getName()).isEqualTo(savedInstitution.get().getName());
}
@Test
public void getInstitution_shouldReturnInstitutionActivities() {
Institution institution = new Institution("I");
Activity regCivil = new Activity("Activity A", new BigDecimal("123"));
Activity identifCivil = new Activity("Activity B", new BigDecimal("500"));
institution.addActivity(identifCivil);
institution.addActivity(regCivil);
Optional<Institution> savedInstitution = Optional.ofNullable(testEntityManager.persistFlushFind(institution));
Optional<Institution> retrievedInstitution = institutionRepository.findInstitutionByName("I");
assertThat(retrievedInstitution.get().getActivities().size()).isEqualTo(2);
assertThat(retrievedInstitution.get().getActivities().size())
.isEqualTo(savedInstitution.get().getActivities().size());
}
}
当我运行JUnit测试时,它失败并显示异常消息:
org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.kikia.itacon.domain.Institution.activities; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.kikia.itacon.domain.Institution.activities
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:351)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
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:135)
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.$Proxy110.findInstitutionByName(Unknown Source)
at com.kikia.itacon.repository.InstitutionRepositoryTest.getInstitution_shouldReturnInstitutionInfo(InstitutionRepositoryTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.kikia.itacon.domain.Institution.activities
at org.hibernate.engine.internal.Collections.processDereferencedCollection(Collections.java:100)
at org.hibernate.engine.internal.Collections.processUnreachableCollection(Collections.java:51)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:262)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:95)
at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)
at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1415)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1501)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1553)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:214)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
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:294)
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)
... 42 more
我不太了解休眠。看来我的人际关系注解是正确的。我尝试了许多解决方案来克服该异常,但失败了。
请,有人可以帮我理解吗?
注意:我的问题具有JUnit测试机制的特殊性,因此我认为不是重复的。