-->
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.  [ 13 posts ] 
Author Message
 Post subject: Mapping 1 column to multiple properties
PostPosted: Tue Nov 02, 2004 1:00 pm 
Newbie

Joined: Tue Nov 02, 2004 12:55 pm
Posts: 5
Hi,

I am attempting to map a database column to two properties in a hibernate object (see example below). One of those properties is "read-only".

I would like a foreign key ID column to map to both a property for the ID and a property for the object represented in the foreign key table. That way I can query either the ID or the actual object itself. I will only be setting the ID.

See "property_id" column below in the example. I have marked the mapped property as "insert=false" and "update=false" so you cannot set these values in code.

There never is a value for the "property" object. Is this unsupported or am I missing something obvious?

Thanks,

Brent


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

<hibernate-mapping>
<class name="UTDocPropertyValue" table="doc_property_value" lazy="true">
<id name="id" column="doc_property_value_id">
<generator class="native" />
</id>

<property name="documentId" column="document_id" />
<property name="propertyId" column="property_id" />
<property name="propertyValue" column="doc_property_value" />

<many-to-one name="property" column="property_id" insert="false" update="false" class="UTProperty" />

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 5:02 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Just add

Code:
<property name="property_id" column="property_id" />


to your mapping.

Since the many-to-one is marked false for insert and update this works fine.

Just as an exercise you can remove the false for insert and update with the additional property you'll get an error up on creation of the SessionFactory.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 3:59 pm 
Newbie

Joined: Tue Nov 02, 2004 12:55 pm
Posts: 5
Hmm, maybe I don't understand. If I do what you suggested, I have the following mapping file:

Code:
<property name="documentId" column="document_id" />
<property name="propertyId" column="property_id" />
<property name="property_id" column="property_id" />

<property name="propertyValue" column="doc_property_value" />

<many-to-one name="property" column="property_id" insert="false" update="false" class="UTProperty" />


This does not allow me to retrieve the actual UTProperty object that is mapped through the foreign key: "property_id".

Two separate properties (propertyId, property_id) are mapped to the same column (property_id). But I still cannot access the property which represents my UTProperty object. It is always null.

Any suggestions?

Thanks,

Brent


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 5:37 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
What's the point of having this two lines?
Code:
<property name="propertyId" column="property_id" />
<property name="property_id" column="property_id" />


I'd say you have to remove one of them.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 5:58 pm 
Newbie

Joined: Tue Nov 02, 2004 12:55 pm
Posts: 5
OK, so now I understand what you were getting at. I already had a line like you were referring to in your first post. So we you said "add", this line was really a duplicate. Unfortunately, it still does not work.

Which takes us back to the original problem. The value for "property", which is of type UTProperty, is always null. I am using hibernate 2.1.4.

When I do have what I think is the correct XML:

Code:
<property name="documentId" column="document_id" />
<property name="propertyId" column="property_id" />

<property name="propertyValue" column="doc_property_value" />

<many-to-one name="property" column="property_id" insert="false" update="false" class="UTProperty" />



My class looks like this:

Code:
public class UTDocPropertyValue {

   private Integer documentId;
   private Integer propertyId;
   private String propertyValue;
   private UTProperty property;
...
// Getters and setters for properties
}


The value for "property" which is of type "UTProperty" is always null.

Thanks,

Brent


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 6:35 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
What happens when you remove the property 'propertyId' mapped to the FK? Can you then retrieve instances of UTDocPropertyValue containing an instance of UTProperty ok?

Why do you need to do this (map a property to the FK of an associated object)? What are you trying to achieve?

If a UTDocPropertyValue has an associated UTProperty then can't you refer to it's id property through the UTProperty instead? There is no need to keep a separate copy in UTDocPropertyValue; it's redundant.

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 7:23 pm 
Newbie

Joined: Tue Nov 02, 2004 12:55 pm
Posts: 5
Yes, if I remove the "propertyId" property, it returns the actual instantiated version of UTDocPropertyValue.

I was trying to get this working so I could "get" and "set" just the id value, but there were times that I might want to query (not update) the actual UTDocPropertyValue object.

I could obviously load this myself, but I was just seeing if hibernate would do this.

Brent


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 8:01 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
Get and set the FK in an associated object? But why would you want to do this - Hibernate takes care of this for you through the mapping.

If you want to change the id of an associated object, for example in a 1-1 between A and B, you assign an instance of B to A by calling setB(b) on A (and if the relationship is bidirectional then also call setA(a) on B to maintain the relationship in both directions)

Hibernate takes care of managing the FKs between rows in the tables - this is not something that you should be concerned with or try to manipulate yourself - it is transparent to your code.

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 8:32 pm 
Newbie

Joined: Tue Nov 02, 2004 12:55 pm
Posts: 5
No, I want to get and set the ID directly in my primary object.

Here is the document table:

document_id
name
property_id

My use case allows me to know the property_id through an external mechanism.

I can accomplish managing this value through the getters and setters.

Document.setPropertyId(Integer )
Integer Document.getPropertyId()

But in a separate use case, I also wanted to be able to be able to query the actual property object as well, as opposed to having to load it.

UTProperty UTDocument.getProperty()

So I will always be "setting" the ID directly, but in some cases I may want to "get" just the ID, in other cases I may want to "get" the whole physical corresponding object.

Like I said, I can manually load the corresponding object, but I thought hibernate would do both for me.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 2:05 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
I've some mappings where I have the same requirement (id property and association). I just noticed, that my hbm.xml files do have allways the association first and then the property. So maybe the following works:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="UTDocPropertyValue" table="doc_property_value" lazy="true">
<id name="id" column="doc_property_value_id">
<generator class="native" />
</id>

<property name="documentId" column="document_id" />
<property name="propertyValue" column="doc_property_value" />

<many-to-one name="property" column="property_id" insert="false" update="false" class="UTProperty" />

<property name="propertyId" column="property_id" />

</class>
</hibernate-mapping>


HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 02, 2006 9:07 pm 
Newbie

Joined: Sat Dec 02, 2006 9:05 pm
Posts: 2
I know this is an old thread, but I need to solve the exact same problem. Is there a solution to this issues (specially in Hibernate 3)?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 03, 2006 2:50 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
You could use the formula attribute on the many-to-one to achieve this. Here is something that works for me.
DocumentRelations has a many-to-one relation with Document class. In the DocumentRelations object, I can set the documentID and be able to retrieve docRelations.getDocument() and get the same document object. My mapping files are

Document.hbm.xml
Code:
<hibernate-mapping package="com.rajasaur.hibernate3.forum935920">
<class name="Document" table="Document">

   <id name="id" type="int" unsaved-value="0">
        <column name="id" sql-type="int" not-null="true" />
        <generator class="increment" />
   </id>

   <property name="name" />
</class>
</hibernate-mapping>


and DocRelations.hbm.xml is
Code:
<hibernate-mapping package="com.rajasaur.hibernate3.forum935920">
<class name="DocumentRelations" table="DocumentRelations">

   <id name="id" type="int" unsaved-value="0">
        <column name="id" sql-type="int" not-null="true" />
        <generator class="increment" />
   </id>

   <property name="documentID" column="document_id"/>

   <many-to-one class="Document" name="document"
                insert="false" update="false">
        <formula>
            (select d.id from Document d where d.id = document_id)
        </formula>
    </many-to-one>

</class>
</hibernate-mapping>


Checkout the formula on the many-to-one in the DocumentRElations.hbm.xml. Doing a Testcase like
Code:
        Document doc = (Document) s.load(Document.class, new Integer(1));
        DocumentRelations docRelations = (DocumentRelations) s.createQuery("from DocumentRelations dr" +
                " where dr.documentID = 1").uniqueResult();
        System.out.println("Document Name: " + docRelations.getDocument().getName());
        assertTrue("Doc Object is the same", doc == docRelations.getDocument());


passes just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 11:37 am 
Newbie

Joined: Sat Dec 02, 2006 9:05 pm
Posts: 2
Thanks for the response, rajasaur. I will try iur your solution.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.