[jboss-user] [EJB 3.0] - Entity relationship and remove operations

kstrunk do-not-reply at jboss.com
Tue Mar 20 08:02:30 EDT 2007


Hello,

I have a problem with entity relationships and foreign key constraints. I want to get all child entities deleted when the parent entity is deleted. Therefore I used the CasecadeType.REMOVE, but it doesn't work.

Here is code fragment:


  | @Entity
  | @Table(name = "Users")
  | public class User implements Serializable {
  | 
  |     private int id;
  | 
  |     private List<ChatMessage> sentChatMessages;
  |     private List<ChatMessage> receivedChatMessages;
  | 
  |     @Id
  |     @GeneratedValue(strategy = GenerationType.IDENTITY)
  |     @Column(name = "id", updatable = false, nullable = false)
  |     public int getId() {
  |         return id;
  |     }
  | 
  |     @OneToMany(mappedBy = "sender", cascade = {CascadeType.REMOVE}, fetch = FetchType.LAZY)
  |     public List<ChatMessage> getSentChatMessages() {
  |         return this.sentChatMessages;
  |     }
  | 
  |     public void setSentChatMessages(List<ChatMessage> sentChatMessages) {
  |         this.sentChatMessages = sentChatMessages;
  |     }
  | 
  |     @OneToMany(mappedBy = "receipient", cascade = {CascadeType.REMOVE}, fetch = FetchType.LAZY)
  |     public List<ChatMessage> getReceivedChatMessages() {
  |         return this.receivedChatMessages;
  |     }
  | 
  |     public void setReceivedChatMessages(List<ChatMessage> receivedChatMessages) {
  |         this.receivedChatMessages = receivedChatMessages;
  |     }
  | }
  | 


  | @Entity
  | @Table(name = "ChatMessages")
  | public class ChatMessage implements Serializable {
  | 
  |     private User sender;
  | 
  |     private User receipient;
  | 
  |     private String message;
  | 
  |     @ManyToOne()
  |     @JoinColumn(name = "sender", referencedColumnName = "id", nullable = false)
  |     public User getSender() {
  |         return this.sender;
  |     }
  | 
  |     public void setSender(User sender) {
  |         this.sender = sender;
  |     }
  | 
  |     @ManyToOne()
  |     @JoinColumn(name = "receipient", referencedColumnName = "id", nullable = false)
  |     public User getReceipient() {
  |         return this.receipient;
  |     }
  | 
  |     public void setReceipient(User receipient) {
  |         this.receipient = receipient;
  |     }
  | }
  | 

When I try to delete a user, I exspect that all his messages are also deleted, but this does not happen. Instead I get the following exception:

12:12:43,703 ERROR [JDBCExceptionReporter] ERROR: update or delete on "users" violates foreign key constraint "fk278c74e4eedef195" on "chatmessages"
  Detail: Key (id)=(12) is still referenced from table "chatmessages".
12:12:43,703 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
	at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
	at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
	at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:218)
	at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2414)
	at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2632)
	at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
	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.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:515)
	at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
	at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
	at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
	at org.jboss.tm.TxManager.commit(TxManager.java:240)
	at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
	at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
	at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
	at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:263)
	at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
	at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
	at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:828)
	at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:681)
	at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:358)
	at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:412)
	at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:239)
Caused by: java.sql.BatchUpdateException: Batch-Eintrag 0 delete from Users where id=12 wurde abgebrochen.  Rufen Sie getNextException auf, um die Ursache zu erfahren.
	at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2478)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1298)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:347)
	at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2540)
	at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:519)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
	... 41 more

What can I do? I tried to remove all user's messages with the entityManager before removing the user, but I had no luck.

I use JBoss 4.0.5 and a Postgres 8.1 database.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4029741#4029741

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4029741



More information about the jboss-user mailing list