0

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. I have used cascade delete orphan as following but does not work yet.

Parent class has following getter for its member

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

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

 <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();
Tim Norman
  • 421
  • 1
  • 12
  • 31

3 Answers3

1
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 
muthukumar
  • 2,233
  • 3
  • 23
  • 30
1

There are a few ways to work with an OneToMany relationship. 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.

Additionally, you may need to recreate you tables because the db constraint has been created already. Do not trust 100% on hbm2ddl.auto=update in this case. I would suggest dropping the relevant tables (Message, Reciver, and Receiver_Message or Message_Receiver). Next, you can use hbm2ddl.auto=update.

I hope it helps.

Cheers

Rafa
  • 1,997
  • 3
  • 21
  • 33
0

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.

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"

@Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN) 
Ankit
  • 113
  • 1
  • 7