我有一个数据库结构,需要多对多的映射。我使用@ManyToMany
注释实现了它,如下所示:
@Entity
@Table(name="trans.documentfiles")
public class DocumentFile extends GenericEntity
{
private DocumentType documentType;
private DocumentFormat documentFormat;
private Set<DocumentGroup> docgroups;
/**
* @return the documentType
*/
@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name = "filetypeid", nullable = false)
public DocumentType getDocumentType()
{
return documentType;
}
/**
* @param documentType the documentType to set
*/
public void setDocumentType(DocumentType documentType)
{
this.documentType = documentType;
}
/**
* @return the docgroups
*/
@ManyToMany(mappedBy = "files")
public Set<DocumentGroup> getDocgroups()
{
return docgroups;
}
/**
* @param docgroups the docgroups to set
*/
public void setDocgroups(Set<DocumentGroup> docgroups)
{
this.docgroups = docgroups;
}
}
DocumentGroup.java如下
@Entity
@Table(name="trans.document")
public class DocumentGroup extends GenericEntity
{
private Set<DocumentFile> files = new HashSet<DocumentFile>();
/**
* @return the files
*/
@ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL )
@JoinTable(name = "trans.documentlink",
joinColumns = {@JoinColumn(name = "documentid", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name = "fileid", referencedColumnName="id")})
public Set<DocumentFile> getFiles()
{
return files;
}
/**
* @param files the files to set
*/
public void setFiles(Set<DocumentFile> files)
{
this.files = files;
}
}
我在sql中创建了DocumentFile
表和DocumentType
表之间的关系。我没有在sql中创建DocumentFile
和DocumentGroup
之间的任何关系。我想删除ocumentLinks
表中的记录而不删除DocumentGroup
记录或DocumentFile
记录或DocumentType
记录。我想在一个单独的帖子中删除父母。
每当我的删除代码运行时,我都会收到DocumentType
记录的外键约束违规。
删除代码如下
@Override
public boolean deleteOnlyLinks(long documentId, String nodeId)
{
Criteria docFile = null;
try
{
docFile = getCriteria(DocumentGroup.class);
docFile.createAlias("files", "file");
docFile.add(Restrictions.eq("file.id", documentId));
docFile.add(Restrictions.eq("nodeId", nodeId));
List<DocumentObjectLink> lst = docFile.list();
/*for (DocumentObjectLink documentObjectLink : lst)
{*/
getCrntSession().delete(lst.get(0));
//}
} catch (Exception e)
{
logger.debug("Error while deleting" + documentId);
throw new DMSException("Error while deleting" + documentId,ErrorCode.DELETE,e);
}
return true;
}
请让我知道如何解决这个问题..
由于
EDIT ::
1:35:11.706 E|SqlExceptionHelper |The DELETE statement conflicted with the REFERENCE constraint "FK_documentfiles_documenttype". The conflict occurred in database "DMS", table "trans.documentfiles", column 'filetypeid'.