簡體   English   中英

休眠-屬性是多對一的並且同時構成?

[英]Hibernate - Property being many-to-one and component at the same time?

我是Hibernate的新手,從MyBatis遷移。 我有這個Customer bean,我正在嘗試使用xml映射保存(插入)。 這是我的代碼的簡短版本:

豆子:

public class Customer {
    private Integer id;
    private String code;
    private SystemObject systemObject = new SystemObject();    
}

public class SystemObject {

    private Integer objectId; 
    private Integer userIdCreate; 
    private Integer userIdUpdate; 
    private Date createDate;
    private Date updateDate;    
    private Workspace workspace = new Workspace();    
}

public class Workspace {
    private Integer id;
    private String code;
    private String name;
}

如您所見,客戶有一個SystemObject。 一個SystemObject是一個通用對象,整個代碼中的所有bean都有一個。 SystemObject具有一個工作區。

問題是,該表CUSTOMER,有對象的引用,以及工作區 在“對象世界”中,我通過執行getSystemObject()。getWorkspace()。getId()獲得工作區ID,但是我不知道如何在Hibernate中做到這一點。

表(當前在PostgreSQL上):

create table CUSTOMER (
   CUSTOMER_ID          INT4                 not null,
   OBJECT_ID            INT4                 not null,
   WORKSPACE_ID         INT4                 not null,
   CODE                 VARCHAR(100)         not null,
   constraint PK_CUSTOMER primary key (CUSTOMER_ID),
   constraint AK_CUS_CODE unique (WORKSPACE_ID, CODE)
);

create table OBJECT (
   OBJECT_ID            INT4                 not null,
   WORKSPACE_ID         INT4                 not null,
   USER_ID_CREATE       INT4                 not null,
   USER_ID_UPDATE       INT4                 not null,
   CREATE_DATE          DATE                 not null,
   UPDATE_DATE          DATE                 not null,
   constraint PK_OBJECT primary key (OBJECT_ID)
);

create table WORKSPACE (
   WORKSPACE_ID         INT4                 not null,
   CODE                 VARCHAR(100)         not null,
   NAME                 VARCHAR(100)         not null,
   constraint PK_WORKSPACE primary key (WORKSPACE_ID),
   constraint AK_WS_CODE unique (CODE)
);

alter table CUSTOMER
   add constraint FK_CUS_OBJ foreign key (OBJECT_ID)
      references OBJECT (OBJECT_ID)
      on delete restrict on update restrict;

alter table CUSTOMER
   add constraint FK_CUS_WS foreign key (WORKSPACE_ID)
      references WORKSPACE (WORKSPACE_ID)
      on delete restrict on update restrict;

alter table OBJECT
   add constraint FK_OBJ_WS foreign key (WORKSPACE_ID)
      references WORKSPACE (WORKSPACE_ID)
      on delete restrict on update restrict;

而且,這是我對Customer對象的映射:

<class name="ar.com.portal.bean.Customer" table="CUSTOMER">
    <id name="id" column="CUSTOMER_ID" >
        <generator class="increment"></generator>
    </id>
    <property name="code" column="CODE" />
    <many-to-one name="systemObject" class="ar.com.framework.base.SystemObject" column="OBJECT_ID" 
            unique="true" not-null="true" lazy="false" cascade="all" ></many-to-one>
</class>

主要代碼:

Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();

Customer anew = new Customer();
anew.setCode("new test");
SystemObjectManager.prepareSystemObject(anew); //this line sets all values for the systemObject inside Customer
anew.getSystemObject().getWorkspace().setId(1);
session.save(anew);
transaction.commit();

這樣,休眠模式嘗試同時插入Customer和SystemObject這兩個對象,但是失敗,因為Customer在WORKSPACE_ID列中為null:

(抱歉,錯誤是西班牙語)

Caused by: org.postgresql.util.PSQLException: ERROR: el valor null para la columna «workspace_id» viola la restricción not null   Detail: La fila que falla contiene (3, 8, null, 'new test').

然后,我嘗試了這種方式。 在SystemObject中添加了getWorkspaceId()(只是為了嘗試簡化一個步驟),並將映射更改為:

<class name="ar.com.portal.bean.Customer" table="CUSTOMER">
    <id name="id" column="CUSTOMER_ID" >
        <generator class="increment"></generator>
    </id>
    <property name="code" column="CODE" />
    <component name="systemObject" class="ar.com.framework.base.SystemObject" >
        <property name="workspaceId" column="WORKSPACE_ID" />
    </component>
    <many-to-one name="systemObject" class="ar.com.framework.base.SystemObject" column="OBJECT_ID" 
            unique="true" not-null="true" lazy="false" cascade="all" ></many-to-one>        
</class>

那甚至還沒有開始:

Caused by: org.hibernate.MappingException: Duplicate property mapping of systemObject found in ar.com.portal.bean.Customer

但是我知道這可行,因為如果我刪除“多對一”,則INSERT語句中會出現WORKSPACE_ID列的值(但由於沒有OBJECT_ID而失敗)

所以...我有這個屬性,我需要它同時成為多對一關系和一個組件,但是Hibernate不喜歡這樣。 還有另一種方法可以解決這種特殊情況? 還是我必須使用命名查詢編寫自定義插入?

豆應該

public class Customer {
    private Integer id;
    private String code;
    private SystemObject systemObject = new SystemObject();    
    private Workspace workspace= new Workspace ();    
}

然后映射應該是

<class name="ar.com.portal.bean.Customer" table="CUSTOMER">
    <id name="id" column="CUSTOMER_ID" >
        <generator class="increment"></generator>
    </id>
    <property name="code" column="CODE" />
    <component name="workspace" class="ar.com.framework.base.Workspace" >
        <property name="workspaceId" column="WORKSPACE_ID" />
    </component>
    <many-to-one name="systemObject" class="ar.com.framework.base.SystemObject" column="OBJECT_ID" 
            unique="true" not-null="true" lazy="false" cascade="all" ></many-to-one>        
</class>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM