These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Is it impossible to map this using many-to-one?
PostPosted: Wed Dec 06, 2006 1:10 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi! I'm using NH 1.2.0 Beta 2.

I have the following tables:

Table1: Cargo
Fields: EMP_ID, CODIGO, DESCRICAO. TIPO

Table 2: TipoCargo
Fields: EMP_ID, CODIGO, DESCRICAO

I want to map a many-to-one association between Cargo and TipoCargo.

I'm trying this:

Table Cargo
Code:
...
<class name="Cargo" table="cargo" lazy="true">
    <composite-id access="field" unsaved-value="any">
        <key-property name="EmpId" column="EMP_ID" type="Int32" />
        <key-property name="Codigo" column="CODIGO" type="Int32" />
    </composite-id>

    <property column="DESCRICAO" type="String" name="Descricao" />
   
    <!-- Tipo -->   
    <many-to-one class="TipoCargo" name="Tipo" cascade="none">     
      <column name="EMP_ID" />
      <column name="TIPO" />     
    </many-to-one>
      
</class>
...


Table TipoCargo
Code:
<class name="TipoCargo" table="tipocargo" lazy="true">
    <composite-id access="field">
        <key-property name="EmpId" column="EMP_ID" type="Int32" />
        <key-property name="Codigo" column="CODIGO" type="Int32" />
    </composite-id>

    <property column="DESCRICAO" type="String" name="Descricao" />
</class>
...


As you can see, the association many-to-one is into the Cargo mapping. When I try to INSERT (Save) a Cargo object, I'm getting the following error:

Quote:
NHibernate.ADOException: could not insert: [xxx.DTO.Cargo#xxx.DTO.Cargo] ---> MySql.Data.MySqlClient.MySqlException: Coluna 'EMP_ID' especificada duas vezes


The SQL generated is something like this:

Quote:
INSERT INTO cargo (emp_id, codigo, descricao, emp_id, tipo) VALUES (1, 1, 'teste', 1, 1)


As you can see, the EMP_ID field is duplicated! I think this is because the association, right?

So, how can I do it? Any Idea?

Tks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 3:16 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
You need to either (A) use key-many-to-one instead of key-property (and remove your many-to-one), or (B) use insert="false" and update="false" tags in your many-to-one mapping.

Enjoy.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 3:12 am 
Newbie

Joined: Tue May 18, 2004 10:51 am
Posts: 9
Location: Bolivia
Hello,

You should replace your table naming strategy.


Table1: Cargo
Fields: EMP_ID, CODIGO, DESCRICAO. TIPO

Table 2: TipoCargo
Fields: EMP_ID, CODIGO, DESCRICAO

:-)... Table2: TipoCargo, has a primary key called "EMP_ID"????? please change it to..... ummm... let's say, "TIPO_ID".

See. You have the same field name for "Cargo" too.

Hope this helps.


Saludos,
-Rory


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.