Hi,
The following code works fine with mysql, but when I try this on derby i get exceptions:
( each Tag has two sets of files )
Tags.javaCode:
@Entity
@Table(name="TAGS")
public class Tags implements Serializable
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public long getId()
{
return id;
}
@ManyToMany(targetEntity=Files.class
)
@ForeignKey(name="USER_TAGS_FILES",inverseName="USER_FILES_TAGS")
@JoinTable(name="USERTAGS_FILES",
joinColumns=@JoinColumn(name="TAGS_ID"),
inverseJoinColumns=@JoinColumn(name="FILES_ID"))
public Set<data.Files> getUserFiles()
{
return userFiles;
}
@ManyToMany(mappedBy="autoTags",
targetEntity=data.Files.class)
public Set<data.Files> getAutoFiles()
{
return autoFiles;
}
Files.javaCode:
@Entity
@Table(name="FILES")
public class Files implements Serializable
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public long getId()
{
return id;
}
@ManyToMany(mappedBy="userFiles",
targetEntity=data.Tags.class)
public Set<data.Tags> getUserTags()
{
return userTags;
}
@ManyToMany(targetEntity=Tags.class
)
@ForeignKey(name="AUTO_FILES_TAGS",inverseName="AUTO_TAGS_FILES")
@JoinTable(name="AUTOTAGS_FILES",
joinColumns=@JoinColumn(name="FILES_ID"),
inverseJoinColumns=@JoinColumn(name="TAGS_ID"))
public Set<data.Tags> getAutoTags()
{
return autoTags;
}
now, I add some test data to the tables, and try to delete a "Files" object:
Code:
public void removeFile(Path path)
{
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
Files file=this.getFilesFromPath(path);
if (file!=null)
{
for (Tags tags : file.getUserTags())
tags.getUserFiles().remove(file);
session.flush();
this.removeAutoTags(path);
session.delete(file);
}
session.flush();
}
public void removeAutoTags(Path path)
{
Files files=this.getFilesFromPath(path);
files.getAutoTags().clear(); // clear all autoTags
}
with mysql this code works fine! But derby goes crazy...
Code:
SEVERE: DELETE on table 'FILES' caused a violation of foreign key constraint 'USER_FILES_TAGS' for key (3). The statement has been rolled back.
Jun 10, 2010 9:49:52 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [data.Files#3]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2712)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2895)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
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:1206)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:613)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at $Proxy13.flush(Unknown Source)
at data.HibernateORM.removeFile(HibernateORM.java:285)
at data.DataImp.removeFile(DataImp.java:195)
at booting.DemoBootForTestUntilTestClassesExist.main(DemoBootForTestUntilTestClassesExist.java:62)
1) what am I doing wrong?
2) is there any way of cascading properly when I have 2 many-to-many relationships between two classes?
Thanks!