Hibernate Mapping会引发Constraint违规

时间:2014-12-01 06:26:14

标签: java sql-server hibernate

我有一个数据库结构,需要多对多的映射。我使用@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中创建DocumentFileDocumentGroup之间的任何关系。我想删除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'.

0 个答案:

没有答案