我是Hibernate + Derby的新手......我在谷歌中看到过这个问题,但是没有看到合适的分辨率。
以下代码适用于mysql,但是当我在derby上尝试这个时,我得到例外:
(每个标签有两组文件,反之亦然 - 很多文件)
Tags.java
@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.java
@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 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 getAutoTags()
{
return autoTags;
}
我向数据库添加了一些数据,但是当在Derby上运行时,这些异常会出现(不使用mysql)
例外
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)我做错了什么?
2)当我在两个类之间有2对多关系时,有没有正确级联的方法?
谢谢!
答案 0 :(得分:1)
以下代码适用于MySQL,但是当我在derby上尝试这个时,我得到例外:
我的猜测是你没有使用MySQL的引用完整性(即不是InnoDB引擎),因此约束违规没有被“触发”。但错误就在那里。
实际上,问题在于您应该将mappedBy
置于双向关联的同一侧,例如Tags
上的ManyToMany
(并将其从Files
):
@Entity
public class Tags implements Serializable {
@ManyToMany(mappedBy = "userTags")
@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<Files> getUserFiles() {
return userFiles;
}
@ManyToMany(mappedBy = "autoTags")
public Set<Files> getAutoFiles() {
return autoFiles;
}
//...
}
Files
成为:
@Entity
public class Files implements Serializable {
@ManyToMany
public Set<Tags> getUserTags() {
return userTags;
}
@ManyToMany
@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<Tags> getAutoTags() {
return autoTags;
}
// ...
}
通过这些更改,以下测试通过(在测试方法之外创建session
):
@Test
public void removeFiles() {
Files files = (Files) session.get(Files.class, 1L);
session.delete(files);
session.flush();
Query q = session.createQuery("from Files f where f.id = :id");
q.setParameter("id", 1l);
Files result = (Files) q.uniqueResult();
assertNull(result);
}
并生成以下查询:
Hibernate: select files0_.id as id100_0_ from Files files0_ where files0_.id=? Hibernate: delete from AUTOTAGS_FILES where FILES_ID=? Hibernate: delete from Files_TAGS where userFiles_id=? Hibernate: delete from Files where id=? Hibernate: select files0_.id as id100_ from Files files0_ where files0_.id=?
使用Derby进行测试。