These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: deletion of child elements when trying to delete parent
PostPosted: Thu Feb 03, 2011 8:37 am 
Newbie

Joined: Thu Feb 03, 2011 7:44 am
Posts: 1
tHIS IS MY CODE :
wHEN I AM TRYING TO DELETE THE DATA FROM PARENT TABLE IT IS NOT AUTOMATICALLY DELETING FROM THE CHILD TABLE...HERE ARE MY ENTITY CLASSES - STUDENT.JAVA(parent table) and Phone.java(Child table)
Code:

import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;


@Entity

@Table(name="student")

public class Student {


@Id
@GeneratedValue

private int id;


@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL,orphanRemoval=true)
@JoinTable(name = "student_phone", joinColumns = { @JoinColumn(name = "student_id_fk") }, inverseJoinColumns = { @JoinColumn(name = "phone_id_fk") })
private List<Phone> phonenumbers;






public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Column(name="sname", length=100,nullable=false)

private String sname;


public String getSname() {

return sname;
}

Student(String sname,List<Phone> phonenumbers)
{
   this.sname=sname;
   this.setPhonenumbers(phonenumbers);
}

public void setSname(String sname) {

this.sname = sname;

}


@Column(name="sroll",nullable=false)

private int sroll;

/**

* @return the sroll

*/

public int getSroll() {

return sroll;

}

/**

* @param sroll the sroll to set

*/

public void setSroll(int sroll) {

this.sroll = sroll;

}

@Column(name="scourse",length=10,nullable=false)

private String scourse;

/**

* @return the scourse

*/

public String getScourse() {

return scourse;

}

/**

* @param scourse the scourse to set

*/

public void setScourse(String scourse) {

this.scourse = scourse;

}

public void setPhonenumbers(List<Phone> phonenumbers) {
   this.phonenumbers = phonenumbers;
}

public List<Phone> getPhonenumbers() {
   return phonenumbers;
}

}





Code:

package student;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "PHONE")
public class Phone {
   
   
   private long phoneId;
    private String phoneType;
    private String phoneNumber;
   
   
   


    public Phone() {
   }

   public Phone(String phoneType, String phoneNumber) {
       this.phoneType = phoneType;
       this.phoneNumber = phoneNumber;
    }
   
    @Id
    @GeneratedValue
   @Column(name = "PHONE_ID")
   public long getPhoneId() {
       return this.phoneId;
    }

   public void setPhoneId(long phoneId) {
        this.phoneId = phoneId;
    }

   @Column(name = "PHONE_TYPE", nullable = false, length=10)
    public String getPhoneType() {
       return this.phoneType;
    }

    public void setPhoneType(String phoneType) {
        this.phoneType = phoneType;
    }
     
    @Column(name = "PHONE_NUMBER", nullable = false, length=15)
    public String getPhoneNumber() {
        return this.phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

   

   

}





The error i am getting is :


Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate:
    select
        phone0_.phone_id as phone1_1_0_,
        phone0_.phone_number as phone2_1_0_
    from
        phone phone0_
    where
        phone0_.phone_id=?
Record Deleting ...1
Hibernate:
    delete
    from
        phone
    where
        phone_id=?
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
   at JpaDelete.main(JpaDelete.java:18)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
   ... 1 more
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
   ... 1 more
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`jpacrud`.`student_phone`, CONSTRAINT `FKDD1C4A6AD9D22DA6` FOREIGN KEY (`phone_id_fk`) REFERENCES `phone` (`phone_id`))
   at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   ... 9 more




PLEASE HELP...


Top
 Profile  
 
 Post subject: Re: deletion of child elements when trying to delete parent
PostPosted: Fri Feb 04, 2011 5:58 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

According to the stack you're trying to delete a phone record so you're trying to delete a child, not a parent ...
Your one-to-many relationship is unidirectional and uses a join table that's why you're having a FK_VIOLATION when trying to delete a child ...
I suggest using a bidirectional relation or putting the FK directly in the phone table and not in an independant join table.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.