-
1. Re: relationship does not prevent from deleting referenced e
micho Feb 21, 2007 7:34 AM (in response to micho)jboss-4.0.4.GA_1 is used
-
2. Re: relationship does not prevent from deleting referenced e
onormann Feb 21, 2007 11:48 AM (in response to micho)Try something like this:
@ManyToMany( targetEntity = Usergroup.class, cascade={CascadeType.MERGE, CascadeType.REFRESH})
Best regards -
3. Re: relationship does not prevent from deleting referenced e
alexg79 Feb 21, 2007 11:54 AM (in response to micho)Have you checked whether the correct foreign keys are in place in the database? If not, have you tried to drop the tables and allow Hibernate (which I assume you're using) to generate the tables again?
On a side note, a few non related tips for you:
1. You don't need an explicit constructor if the sole constructor is public and has no arguments. Calling super() is useless if the class only extends java.lang.Object.
2. When defining relationships, you don't need to specify targetEntity when using generics.
3. You don't EVER have to specify strategy = GenerationType.AUTO, since it's the default anyway.
4. Always use English for variable and method names. Makes your software more consistent and maintainable. -
4. Re: relationship does not prevent from deleting referenced e
micho Feb 21, 2007 1:06 PM (in response to micho)Thanks a lot for the help.
In the DB there are no foreign keys.
Inspecting the Database, I saw, that the tabletype is MyISAM whitch I think doenot support foreign keys.
How can I tell Hibernate to create the tables with type innodb?
I tried dropping the tables. The uniqueconstraint, which wasn´t working before now works correct, the referential integrity not. -
5. Re: relationship does not prevent from deleting referenced e
alexg79 Feb 22, 2007 6:45 AM (in response to micho)You can instruct MySQL to create InnoDB tables by default.
Just putdefault-table-type=innodb
under [mysqld], and you're set. -
6. Re: relationship does not prevent from deleting referenced e
micho Feb 22, 2007 7:47 AM (in response to micho)thanks a lot,
that works fine now, and with innodb tables the referential integritay is not violated.
But I´, not sure, how to act here correctly.
I catch two (identical?) runtimeexceptions - why two?java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=nbmicho/143, BranchQual=, localId=143] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update) java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=nbmicho/143, BranchQual=, localId=143] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update)
before two ERRORS one with a stacktrace are reportedERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`test/benutzer_benutzergruppe`, CONSTRAINT `FKFB569FDA36A7B8DE` FOREIGN KEY (`benutzerGruppeListe_sid`) REFERENCES `benutzergruppe` (`sid`)) ERROR [AbstractFlushingEventListener] Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
-
7. Re: relationship does not prevent from deleting referenced e
micho Feb 22, 2007 8:26 AM (in response to micho)I try to detect the constraintviolation now via
catch (RuntimeException e) { if (e.getCause().getCause().getCause().getClass().toString().equals("class org.hibernate.exception.ConstraintViolationException") ) { DO SOMETHING
is that OK?
But there are two things I don´t understand.
1) why are there two identical exceptions? Is that a problem?
2) I guarded the call of the remove request with a try-catch block, but the exception is not cougth in this catchblocktry { em.remove(deleteBg); } catch (Exception e) { logger.error("...
-
8. Re: relationship does not prevent from deleting referenced e
micho Feb 22, 2007 3:57 PM (in response to micho)for point 1, the double Exception, I detected, that only one exception was thrown.
the loggger.error produces double output,
logger.debug doesnot.
But I still don´t know, why this happens and wyh I can´t catch the exception when calling em.remove