-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: mapping problem
PostPosted: Mon Mar 06, 2006 10:24 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
i have a service table which consists of property service_id (primary key), date and service. i have another serviceType table which consists of propert service_id (primary key) and service_description.
the question is is it possible to map the service column(service table) with the service_description column (serviceType table) ??
i have do it in this way:

service.hbm.xml:

<list
name="serviceDescription"
lazy="false"
inverse="true"
cascade="none"
>

<key
column="DESCRIPTION"
>
</key>

<index>
</index>

<one-to-many
class="model.ServiceType"
/>

</list>


serviceType.hbm.xml:

<many-to-one
name="description"
class="model.Service"
cascade="none"
lazy="false"
update="true"
insert ="true"
foreign-key="FK1_SERVICE_TYPE"
column="DESCRIPTION"
not-null="true"
/>


i noticed that the description is mapped to service_id, how to make it to make to the service column in service table??
thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 06, 2006 10:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use the property-ref attribute. It's desecribed in the ref docs.

Code tags make code easier to read than blue tags :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 06, 2006 11:55 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
i have modified the coding in serviceType.hbm.xml as below:

Code:
<many-to-one
            name="description"
            class="model.Service"
            cascade="none"
            lazy="false"
            update="true"
            insert ="true"
            foreign-key="FK1_SERVICE_DESCRIPTION"
            column="DESCRIPTION"
            [color=blue]property-ref="service"[/color]
            not-null="true"
        />


but seem like 'description' still mapped to the service_id(primary key).
do i need to add
Code:
property-ref="service"
in my one-to-many mapping??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 12:23 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The property-ref goes in the <key> of the list. Putting it in the many-to-one means that the primary key of the service_type table is being matched with the service property of the service table, which is presumably wrong.

I'm curious to know what having an empty index in a list does? Why don't you use set instead of list, if there's no index?

Also, is the primary key of the serviceType table really called service_id? Won't that get confusing, seeing as the primary key of the service table has the same name? I'd recommend changing the column name of the primary key of the serviceType table to serviceType_id or similar.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 2:14 am 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
i have modified my coding in service.hbm.xml as below:

Code:
<list
            name="serviceDescription"
            lazy="false"
            inverse="true"
            cascade="none"
              
        >

            <key
                column="DESCRIPTION"
                property-ref="service"
            >
            </key>
           
            <index>
            </index>
           
            <one-to-many
                  class="model.ServiceType"
            />

        </list>


but when i check in the mysql administrator the description column still mapped to the service_id column. so what do i need to do?? anything regarding to the persistence class???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 4:15 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm afraid that I know next to nothing about schema generation tools, I generate all my tables by hand. First thing to check is that you're dropping/generating the schema: if you generated the schema with the old mapping and haven't dropped it since, hibernate doesn't detect mistakes and fix it, you have to tell the schema generation tool to start over.

Assuming you've done that, all I can recommend is to read up on the foreign-key and property-ref attributes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 11:40 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
sorry coz i still cant make it. i have tried to map between user_department in user table with auth_department in authorizer table. if i wrote in this way,

user.hbm.xml:
Code:
<many-to-one
            name="user_department"
            class="model.Authorizer"
            cascade="none"
            lazy="false"
            update="true"
            insert ="true"
            unique="true"
            column="DEPARTMENT"
            not-null="true"
        />   


authorizer.hbm.xml:
Code:
<property
            name="auth_department"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="DEPARTMENT"
            />
        </property>

   <one-to-one
         name="auth_department"
         class="model.User"
         property-ref="user_department"
   />


it will only map the sv_department with the auth_id(primary key in authorizer table).

if i wrote in this way:
user.hbm.xml:
Code:
<many-to-one
            name="user_department"
            class="model.Authorizer"
            cascade="none"
            lazy="false"
            update="true"
            insert ="true"
            unique="true"
            column="DEPARTMENT"
            property-ref="auth_department"           
            not-null="true"
        />   


authorizer.hbm.xml:
Code:
<property
            name="auth_department"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="DEPARTMENT"
            />
        </property>

   <one-to-one
         name="auth_department"
         class="model.User"
         property-ref="user_department"
   />


it will show me the error:
duplicate property mapping: department

any suggestion how to do it??? thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 12:54 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
One-to-one is hardcoded to use primary keys. Use many-to-one unique="true" property-ref="user_department" to accomplish a one-to-one in this situation.

Why don't you use a set though?
Code:
<class name="Authorizer" ...>
  ...
  <set name="Users" inverse="true">
    <key column="DEPARTMENT"/>
    <one-to-many class="User" property-ref="Department"/>
  </set>
...
</class>

<class name="User" ...>
  ...
  <many-to-one name="Department" class="Department" column="DEPARTMENT"/>
  ..
</class>
I think that should accomplish the same thing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 2:58 am 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
when i wrote my authorizher.hbm.xml in this way:

Code:
<set
      name="department"
      inverse="true">
      
      <key column="DEPARTMENT"
          />
   
      <one-to-many
      class="model.User"
     property-ref="user_department"
       />
   </set>

the error "attribute "property-ref" must be declared for element type "one-to-many"" show on the line property-ref. by the way, i have assigned another property "department" in the same xml file. is it necessary??how to fix my error of property-ref??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 4:47 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Property-ref isn't applicable to one-to-many, my mistake. It goes on the key element, instead. Oops.

You can't have more than one non-key reference to a "column" (belonging to a particular table), but you can usually get around this by using "formula". You can't have more than one proerty with a given name in a given class. Note that my <set> idea was only that, an idea: it may be that a simple many-to-one relationship is what you want. If you can't figure out which parts of the mapping you need and which parts you don't, you could post your entire mapping and I'll have a look at it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 8:20 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
thanx, tenwit!!

this is my user.hbm.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="model.User"
        table="USER"
    >

        <id
            name="empNo"
            column="EMP_NO"
            type="java.lang.String"
        >
            <generator class="assigned">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-User.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="firstName"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="FIRST_NAME"
            />
        </property>

        <property
            name="lastName"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="LAST_NAME"
            />
        </property>

        <property
            name="company"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="COMPANY"
            />
        </property>

            <many-to-one
            name="user_department"
            class="model.Authorizer"
            cascade="all"
            lazy="false"
            update="true"
            insert ="true"
            unique="true"
            column="DEPARTMENT"
            not-null="true"
        />   
    <property
            name="designation"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="DESIGNATION"
            />
        </property>

    <property
            name="extNo"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="EXT_NO"
            />
        </property>
       
        <list
            name="serviceInfoList"
            lazy="false"
            inverse="true"
            cascade="none"
        >

            <key
                column="EMP_NO"
            >
            </key>

            <index
                column="No"
                type="string"
            />

            <one-to-many
                  class="model.Service"
            />

        </list>
       

       <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-User.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>

authorizer.hbm.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="model.Authorizer"
        table="AUTHORIZER"
    >

        <id
            name="authoId"
            column="AUTHO_ID"
            type="java.lang.String"
        >
            <generator class="assigned">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Authorizer.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

      
        <property
            name="roles"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="ROLES"
            />
        </property>

   <property
            name="name"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="NAME"
            />
        </property>
       
        <property
            name="department"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="DEPARTMENT"
            />
        </property>

   <set
      name="auth_department"
      inverse="true"
      cascade="true"
      >
      
      <key column="DEPARTMENT"
          property-ref="department"
          />
   
      <one-to-many
      class="model.User"
       />
   </set>
   
    <property
            name="date"
            type="date"
            update="true"
            insert="true"
        >
            <column
                name="DATE"
            />
        </property>
       
     <property
            name="state"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="STATE"
            />
        </property>

    <property
            name="authoRemarks"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="AUTHO_REMARKS"
            />
        </property>

       <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Authorizer.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


i want to map user_department to auth_department. can u have a look on it, pls?? thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 9:16 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You need two more changes.

1) In the User mapping, add property-ref="department" to the user_department element.

2) In the Authorizer mapping, change the auth_department's property-ref to "user_department".

I'm not certain if the authorizer has to be saved before you can save users into the authorizer's auth_department set. It probably does, so that the department column is populated, allowing hibernate to populate the corresponding column in the user table.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 9:39 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
i have modified the files as below:
user.hbm.xml:
Code:
<many-to-one
            name="user_department"
            class="model.Authorizer"
            cascade="all"
            lazy="false"
            update="true"
            insert ="true"
            unique="true"
            column="DEPARTMENT"
            property-ref="department"
            not-null="true"
        />   


authorizer.hbm.xml:
Code:
<property
            name="department"
            type="string"
            update="true"
            insert="true"
        >
            <column
                name="DEPARTMENT"
            />
        </property>

   <set
      name="auth_department"
      inverse="true"
      cascade="true"
      >
      
      <key column="DEPARTMENT"
          property-ref="user_department"
          />
   
      <one-to-many
      class="model.User"
       />
   </set>


the errors ahown when i tried to run schema export:

org.hibernate.MappingException: property not found: user_department on entity model.Authorizer
org.hibernate.MappingException: property not found: user_department on entity model.Authorizer


what's wrong again??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 10:25 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hmmm. It seems that property-ref on <key> doesn't do what I expected it to. Ok, the next thing to try is to edit authorizer's auth_department set again: remove that property-ref, and change your one-to-many to be many-to-many unique="true" property-ref="user_department". That approach works for turning many-to-ones into one-to-ones, and according to the ref docs, it also converts many-to-manys to one-to-manys (see ref docs, section 6.2.4. "Collections of values and many-to-many associations").


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 10:44 pm 
Beginner
Beginner

Joined: Tue Feb 07, 2006 10:39 pm
Posts: 46
ops, something very strange happened. after i have modified authorizer.hbm.xml as below:

Code:
<set
      name="auth_department"
      inverse="true"
      cascade="true"
      >
      
      <key column="DEPARTMENT"
          />
   
      <many-to-many
      class="model.User"
      unique="true"
      property-ref="user_department"
       />
   </set>


i found that there is another table called "auth_department" added which consists of a primary key(elt) and property :department:. there is a foreign key founded which map AUTH_ID(Authorizer) to DEPARTMENT(auth_department). what's going on?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.