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.  [ 1 post ] 
Author Message
 Post subject: delete cascade problem by many-to-one relationship
PostPosted: Sun Apr 08, 2007 9:03 pm 
Newbie

Joined: Sun Apr 08, 2007 8:37 pm
Posts: 4
i am getting the same exception, if i try to delete an entity from the side "one" by a many-to-one relationship.
First i'll explain the very basic scenario: i have 2 entities, Genre and Movie. Every movie belongs to a genre. many movies can belong to only one genre. so relationship is many-to-one. later after I've added some entity instances to genre and movie, i want to delete a genre instance. any movie, which belongs to this genre, should be deleted automatically.

i use annotations and my two classes look like these.

Code:
@Entity
public class Genre implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;

setters and getters ...
}

@Entity
public class Movie implements java.io.Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String title;

@ManyToOne(cascade={CascadeType.ALL})
private Genre genre;

setters and getters ...
}


if i try to delete a genre, i am getting this exception:

Code:
02:48:26,968 DEBUG SQL:401 - select genre0_.id as id5_0_, genre0_.name as name5_0_ from Genre genre0_ where genre0_.id=?
Hibernate: select genre0_.id as id5_0_, genre0_.name as name5_0_ from Genre genre0_ where genre0_.id=?
02:48:26,984 DEBUG SQL:401 - delete from Genre where id=?
Hibernate: delete from Genre where id=?
02:48:27,000  WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: null
02:48:27,000 ERROR JDBCExceptionReporter:78 - failed batch
02:48:27,000 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at tvsas1.Main.transaction4(Main.java:74)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at tvsas1.Main.main(Main.java:44)
Caused by: java.sql.BatchUpdateException: failed batch
        at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
        at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
        ... 9 more
        at tvsas1.Main.transaction4(Main.java:74)
        at tvsas1.Main.main(Main.java:44)
Caused by: java.sql.BatchUpdateException: failed batch
        at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
        at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
        ... 9 more


codes between session.beginTransaction() and session.getTransaction.commit() for adding some entities

Code:
Genre genre1 = new Genre("Drama");
Genre genre2 = new Genre("War");
       
Movie movie1 = new Movie();
movie1.setGenre(genre1);
movie1.setTitle("Matrix 1");
             
Movie movie2 = new Movie();
movie2.setGenre(genre2);
movie2.setTitle("Matrix 2");       
       
Movie movie3 = new Movie();
movie3.setGenre(genre2);
movie3.setTitle("Matrix 3");
       
session.save(genre1);
session.save(genre2);
session.save(movie1);
session.save(movie2);
session.save(movie3);


and codes between session.beginTransaction() and session.getTransaction.commit() for deletin a genre

Code:
//get a genre
Genre genre = GenreDAO.findById(new Long(1));
session.delete(g);


what is missing here?

versions i use:
Code:
Hibernate Core 3.2.3.ga
hibernate-annotations-3.3.0.GA


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

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.