gpt4 book ai didi

java - Hibernate映射列出双方关系。

转载 作者:行者123 更新时间:2023-12-02 07:27:04 26 4
gpt4 key购买 nike

我有两个对象; 文档DocumentBatch

文档

public class Document implements Serializable {

....
private String documentId; //PK of Document
private DocumentBatch documentBatch;
....}

文档批处理

public class DocumentBatch implements Serializable {


private String batchId;//PK of DocumentBatch

private List<Document> lDocuments = new LinkedList<Document>();
.....
public void insertDocument(Document document) {
lDocuments.add(document); // lDocuments is a list DocumentBatch
document.setDocumentBatch(this);
....}

hibernate 映射:

文档

   <class name="Document" table="DOCUMENTS">
.....
<id name="documentID" column="DOCUMENT_ID" type="string" />
<many-to-one name="documentBatch" class="DocumentBatch" not-null="false"
cascade="save-update" lazy="false" insert="false" update="false">
<column name="BATCH_ID" not-null="true" />
</many-to-one>
......
</class>

文档批处理

 <class name="DocumentBatch" table="DOCUMENT_BATCHES">
<id name="batchId" column="BATCH_ID" type="string" />
<list name="lDocuments" table="DOCUMENTS" cascade="all"
inverse="false" lazy="false" mutable="true">
<key not-null="true">
<column name="BATCH_ID" />
</key>
<list-index column="LIST_INDEX" base="0" />
<one-to-many class="Document" />
</list>
......
</class>

DocumentBatch有一个文档列表

文档有batchId,它是DocumentBatch的PK。我已经在 DocumentBatch 中的列表的 Hibernate 映射中设置了

反向=“假”

并在文档中设置多对一关系 insert="false"update="false"

但是当我尝试保存文档对象时,其 DocumentBatch 将不会被保存。

如何解决。如果有人可以帮忙......希望大家周末愉快。

Oracle 数据库:

文档

CREATE TABLE DOCUMENTS(
DOCUMENT_ID VARCHAR2(255 CHAR) NOT NULL,
BATCH_ID VARCHAR2(255 CHAR) NOT NULL,
...);


CREATE UNIQUE INDEX PK_DOCUMENT ON DOCUMENTS (DOCUMENT_ID);
ALTER TABLE DOCUMENTS ADD (CONSTRAINT PK_DOCUMENT PRIMARY KEY (DOCUMENT_ID) USING INDEX PK_DOCUMENT);

ALTER TABLE DOCUMENTS ADD (CONSTRAINT FK_DOCUMENT_BATCH_ID FOREIGN KEY (BATCH_ID) REFERENCES DOCUMENT_BATCHES (BATCH_ID) ON DELETE CASCADE);

DocumentBatch

CREATE TABLE DOCUMENT_BATCHES(
BATCH_ID VARCHAR2(255 CHAR) NOT NULL
...);

ALTER TABLE DOCUMENT_BATCHES ADD (
PRIMARY KEY (BATCH_ID));

最佳答案

如果我理解正确的话。从您显示的片段来看,我会说您所经历的行为是正确的。你说的问题是:

but when I try to save a Document obj, its DocumentBatch won't be saved.

因为有明确的设置不插入和不更新:

<many-to-one name="documentBatch" class="DocumentBatch" 
not-null="false" cascade="save-update" lazy="false"
insert="false" // Hibernate do not insert
update="false" // Hibernate do not update
>

那么结果是正确的。如果您想将关系保存到 DocumentBatch,那么只需更改该映射即可:

insert="true" // Hibernate DO insert
update="true" // Hibernate DO update

(或删除它们,因为默认值为 true),然后如果您将 DocumentBatch 分配给您的 Document 并保存它 - 所有内容都将正确保存。这应该可以解决它。

编辑:重复列异常

我猜测您的实体 Document 有另一个字段映射到 batch_id

<many-to-one name="documentBatch" class="DocumentBatch" not-null="false" 
cascade="save-update" lazy="false" insert="false" update="false">
<column name="BATCH_ID" not-null="true" />
</many-to-one>
// this is not in the snippet above, but I guess it is there
<property name="batchId" column="BATCH_ID"/>

并在代码中:

public class Document implements Serializable {
private String documentId; //PK of Document
private DocumentBatch documentBatch;
// this is not in the snippet above, but I guess it is there
private String batchId;
....}

如果是这种情况,则两个不同的属性将映射到同一列,这就是我们遇到以下问题的原因:重复列异常如果我的期望是正确的,请按以下方式更改映射:

<many-to-one name="documentBatch" class="DocumentBatch" not-null="false" 
cascade="save-update" lazy="false" > // insert and update removed
<column name="BATCH_ID" not-null="true" />
</many-to-one>
<property name="batchId" formula="[BATCH_ID]" insert="false" update="false"/>

因此,documentBatch 将用于插入更新,而batchId 仅用于只读。

关于java - Hibernate映射列出双方关系。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13420239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com