我有一個應用程序,其中有許多tomany關係。創建行的部分工作正常。現在我還需要檢索記錄並刪除聯繫,同時維護父母。休眠組合鍵只有foriegn鍵
爲此,我添加了另一個代表鏈接的類。該類不包含額外的列,而是包含使用其他類表示的ID。 id類包含外鍵。
我的問題是,許多註釋的@many也需要在id類中嗎?
我已經試過了,並同時列出它給了我一個「不是關聯」例外敵我別名
這是我的相關代碼:
@Entity
@Table(name="trans.documentlink")
public class DocumentObjectLink
{
@Id
private LinkId id;
/**
* @return the id
*/
public LinkId getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(LinkId id)
{
this.id = id;
}
}
@Embeddable
public class LinkId implements Serializable
{
//private String comment;
private DocumentGroup document;
private DocumentFile docFile;
public LinkId(DocumentFile fileId, DocumentGroup docId)
{
super();
this.docFile = fileId;
this.document = docId;
}
@Override
public boolean equals(Object obj) {
LinkId cp =(LinkId)obj;
if(this.docFile.equals(cp.getDocFile()) && this.document.equals(cp.getDocument())){
return true;
}else{
return false;
}
}
@Override
public int hashCode() {
return this.docFile.hashCode()+this.document.hashCode();
}
/**
* @return the documentFile
*/
@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name = "fileid" , nullable = false)
public DocumentFile getDocFile()
{
return docFile;
}
/**
* @param documentFile the documentFile to set
*/
public void setDocFile(DocumentFile documentFile)
{
this.docFile = documentFile;
}
/**
* @return the document
*/
@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name = "documentid", nullable = false)
public DocumentGroup getDocument()
{
return document;
}
/**
* @param document the document to set
*/
public void setDocument(DocumentGroup document)
{
this.document = document;
}
}
在那裏我得到的異常的代碼是:
@Override
public boolean deleteOnlyLinks(long documentId, String nodeId)
{
Criteria docFile = null;
try
{
docFile = getCriteria(DocumentObjectLink.class);
docFile.createAlias("id.docFile", "file");
docFile.createAlias("id.document", "doc");
docFile.add(Restrictions.eq("file.id", documentId));
docFile.add(Restrictions.eq("doc.nodeId", nodeId));
List<DocumentObjectLink> lst = docFile.list();
if(lst.size() > 0)
{
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;
}
唯一的例外是:
org.hibernate.QueryException: not an association: id.docFile
編輯::
我試圖叫ID的INT列添加到其心不是一個主鍵,但有一個生成的值的分貝。然後我搬到了foriegn鑰匙,documentobjectlink類,並試圖運行下面的代碼
@Override
public boolean deleteOnlyLinks(long documentId, String nodeId)
{
Criteria docFile = null;
try
{
docFile = getCriteria(DocumentObjectLink.class);
docFile.createAlias("docFile", "file");
docFile.createAlias("document", "doc");
docFile.add(Restrictions.eq("file.id", documentId));
docFile.add(Restrictions.eq("doc.nodeId", nodeId));
List<DocumentObjectLink> lst = docFile.list();
if(lst.size() > 0)
{
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;
}
現在我得到異常
not an association: docFile
從這個答案我明白,你希望我把ids,而不是實際的對象在關聯表id類。我會嘗試這樣做並更新問題 – kavita 2014-12-04 08:01:30