简体   繁体   English

无法在hibernate中删除OneToMany关系的实例

[英]Can not delete an instance of OneToMany relationship in hibernate

I have a OneToMany relationship, I can insert records but can not delete them, when I try to delete it runs into " a foreign key constraint fails" error. 我有一个OneToMany关系,我可以插入记录但不能删除它们,当我尝试删除它时遇到“外键约束失败”错误。 I have used cascade delete orphan as following but does not work yet. 我使用了cascade delete orphan如下,但还没有工作。

Parent class has following getter for its member 父类的成员具有以下getter

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

@Entity
@DynamicUpdate
public class User extends Employee{
    private string userli;
    privae List<Message> messagelist();

    .....

    @OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    public List<Message> getMessagelist() {
        return messagelist;
    }

Member class has following getter for its parent 成员类的父级具有以下getter

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
 ......  

   @ManyToOne
   public User getReciever() {
        return reciever;
   }

I used following annotation as well but did not work 我也使用了以下注释,但没有用

     @Cascade(org.hibernate.annotations.CascadeType.DELETE)

My hibernate dependency is as following 我的hibernate依赖如下

 <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.1.Final</version>
    <type>jar</type>
</dependency>

My code to remove the message 我的代码删除邮件

            Message message = (Message) session.get(Message.class, id);
            session.delete(message);
            tx.commit();
Try to change the cascade = cascadeType.ALL 

and check

    @OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    public List<Message> getMessagelist() {
            return messagelist;

It might work but not sure 

There are a few ways to work with an OneToMany relationship. 有几种方法可以使用OneToMany关系。 One of the most common way would be like: 最常见的方式之一是:

@OneToMany(mappedBy="receiver", CascadeType.REMOVE)
public List<Message> getMessagelist() {
        return messagelist;
}

....

@ManyToOne
public User getReciever() {
        return reciever;
}

Note that fetch = FetchType.LAZY is the default you do not really need to specify it. 请注意,fetch = FetchType.LAZY是您不需要指定它的默认值。

Additionally, you may need to recreate you tables because the db constraint has been created already. 此外,您可能需要重新创建表,因为已经创建了db约束。 Do not trust 100% on hbm2ddl.auto=update in this case. 在这种情况下,不要相信100%的hbm2ddl.auto = update。 I would suggest dropping the relevant tables (Message, Reciver, and Receiver_Message or Message_Receiver). 我建议删除相关的表(Message,Reciver,Receiver_Message或Message_Receiver)。 Next, you can use hbm2ddl.auto=update. 接下来,您可以使用hbm2ddl.auto = update。

I hope it helps. 我希望它有所帮助。

Cheers 干杯

Try adding following annotations. 尝试添加以下注释。 It worked for me. 它对我有用。

@OneToMany(mappedBy="receiver", cascade=CascadeType.ALL, fetch=FetchType.LAZY)  
@Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
 public Set<Message> getMessagelist() {
    return messagelist;
}

I am using entity manager remove method and it works for me. 我正在使用实体管理器删除方法,它适用于我。 I am using Set instead of List, which is a efficient way, in my opinion. 在我看来,我使用的是Set而不是List,这是一种有效的方式。

Delete orphan annotation is just for telling hibernate that if "I remove entity from MessageList and try to merge User then you can safely delete Message" 删除孤立注释只是为了告诉hibernate,如果“我从MessageList中删除实体并尝试合并用户,那么你可以安全地删除消息”

@Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM