我遇到了一个问题,当我在 ManyToOne 关系的多方面删除一条记录时,它可以工作,但是在下一个数据库事务中,我得到一个异常,说找不到我刚刚删除的实体(例如,当尝试添加另一个链接时)说找不到我刚刚删除的那个)。我在数据库中验证了记录正在被删除。
我在 Hibernate 中使用 Spring MVC 和 Spring Data JPA。我有一个用户类和一个 NewsLink 类。一个用户可以有许多新闻链接,我想删除其中一个新闻链接。我只是通过构建一个简单的应用程序来学习如何自己使用 Spring 框架。所以这对我来说很新。我很感激任何帮助!
用户实体类
@Entity
@Table(name = "users")
public class User implements Serializable{
@Id
@Column(name = "idusers")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
....
@OneToMany(mappedBy="user", fetch= FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval= true)
private Set<NewsLink> newsLink;
NewsLink 实体类
@Entity
@Table(name = "news_links")
public class NewsLink implements Serializable{
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(insertable=false, updatable=false)
private Long idusers;
....
@ManyToOne
@JoinColumn(name="idusers")
private User user;
这是执行删除的服务类中的方法。我认为这是我做得不对的部分:
@Override
@Transactional
public User deleteNewsLink(int id, User user){
Iterator<NewsLink> links = user.getNewsLink().iterator();
while(links.hasNext()){
NewsLink link = links.next();
if (link.getId() == id){
links.remove();
}
}
userRepository.save(user);
return user;
}
这是我看到的例外:
Request processing failed; nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.test.dailyemail.repository.NewsLink with id 8; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.test.dailyemail.repository.NewsLink with id 8
javax.persistence.EntityNotFoundException: Unable to find com.test.dailyemail.repository.NewsLink with id 8
at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:155)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:210)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1078)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1005)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:651)
at org.hibernate.type.EntityType.resolve(EntityType.java:471)
at org.hibernate.type.EntityType.replace(EntityType.java:328)
at org.hibernate.type.CollectionType.preserveSnapshot(CollectionType.java:581)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:534)
at org.hibernate.type.CollectionType.replace(CollectionType.java:664)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:177)
at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:372)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:309)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:903)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:887)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:891)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879)