1

2 つのオブジェクトがあります。DocumentDocumentBatch

書類

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);
....}

休止状態のマッピング:

書類

   <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 には Document のリストがあります

ドキュメントには、DocumentBatch の PK である batchId があります。DocumentBatch のリストの Hibernate マッピングで設定しました

逆=「偽」

Document では、多対一のリレーション セット insert="false" update="false"

しかし、Document obj を保存しようとすると、その DocumentBatch は保存されません。

の解き方。誰かが助けてくれたら....みんなが良い週末を過ごせますように。

オラクル DB :

ドキュメントの

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));
4

1 に答える 1

3

私があなたを正しく理解していれば。あなたが示したスニペットから、あなたが経験している動作は正しいと言えます。問題は次のとおりです。

しかし、Document obj を保存しようとすると、その DocumentBatch は保存されません。

NOT INSERT と NOT UPDATE の明示的な設定があるため:

<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;
....}

この場合、2 つの異なるプロパティが同じ列にマップされているため、次のようなことが発生しています。duplicated column exception 私の期待が正しい場合は、次のようにマッピングを変更します。

<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"/>

したがって、はinsertupdatedocumentBatchに使用されますが、batchId は readonly のみに使用されます。

于 2012-11-16T16:22:58.250 に答える