java - Hibernate - Property being many-to-one and component at the same time? -


i'm new hibernate, migrating mybatis. i've customer bean, i'm trying save (insert) using xml mapping. shorter version of code:

beans:

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

as can see, customer has systemobject. systemobject general object, beans across code has one. systemobject has workspace.

the problem is, table customer, has reference object, , workspace too. in "objects world", workspace id doing getsystemobject().getworkspace().getid(), don't know how in hibernate

tables (currently on 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; 

and, mapping customer object:

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

code in main:

session session = hibernatesessionmanager.getsessionfactory().getcurrentsession(); transaction transaction = session.begintransaction();  customer anew = new customer(); anew.setcode("new test"); systemobjectmanager.preparesystemobject(anew); //this line sets values systemobject inside customer anew.getsystemobject().getworkspace().setid(1); session.save(anew); transaction.commit(); 

this way, hibernate tries insert both objects, customer , systemobject, fails because customer has null @ workspace_id column:

(sorry, error in spanish)

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'). 

then, tried way. added getworkspaceid() in systemobject (just try , simplify 1 step) , changed mapping this:

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

that doesn't start:

caused by: org.hibernate.mappingexception: duplicate property mapping of systemobject found in ar.com.portal.bean.customer 

but know work, because if delete "many-to-one", value workspace_id column present in insert statement (but fails not having object_id)

so... have property, need many-to-one relationship , component @ same time, hibernate doesn't that. there way solve particular situation? or i've write custom insert using named query?

the bean should be

public class customer {     private integer id;     private string code;     private systemobject systemobject = new systemobject();         private workspace workspace= new workspace ();     } 

and mapping should be

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

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -