-->
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.  [ 6 posts ] 
Author Message
 Post subject: I can't get this relationsip to work and I'm lost for ideas.
PostPosted: Thu May 03, 2007 4:50 am 
Newbie

Joined: Thu May 03, 2007 3:58 am
Posts: 7
Location: London
Hello

I have two classes, Organisation and Address. Organisation look something like this:

...
private int id;
private Address registeredAddress;
private Set<Address> locations;
...

Address like this:

...
private int id;
private String address;
private String city;
...

Now the tables. There are two, ORGANISATIONS and ADDRESSES. ORGANISATIONS looks something like this:

ORG_ID <PK>
ORG_NAME
...

The ADDRESSES table looks something like this:

ADD_ID <PK>
ORG_ID <FK> (not null)
ADDRESS
CITY
REGISTERED_ADDRESS (boolean, defaults to false, not null)
...

Now the rules:
* Organisation must have a registered address, this is not enforced on the database level but in the DAO or domain.
* Address can not belong to more then one Organisation.
* One organisation can have 0..* locations.

As you can see in the object model the Address don't have a link to the Organisation. This is reversed in the database, I.E. the ORGANISATION table don't have any key mapping to the ADDRESSES table.

When a user creates a new organisation s/he will fill in all required information, including the registered address for the organization (but probably not any locations). My problem is that I can't get hibernate to insert the registered address when it creates a new organisation. It tries to do an update on the ADDRESSES table rather then an insert (when I look at the printed SQL in the console).

I have tried a number of mapping options, such as many-to-on, one-to-one and one-to-many with different options on them and this mapping don't seem to be something common. I have also tried using the formula attribute with no success.

My current solution for the problem is to insert a new column into the ORGANISATION table with a FK to the ADDRESSES table for the registered address but I would rather not have the cross link. since there is already a link from the ORGANISATION to the ADDRESSES that I should be able to use, the SQL is simple enough.

I have control over both the database design and the domain design, but I would rather change the database design since it is much simpler to follow then the domain, which is rather complex.

I am using Hibernate3 and Oracle.

Any help would be greatly appreciated.

_________________
Tomas


Last edited by tomas@procserve on Thu May 03, 2007 5:09 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 5:02 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> My problem is that I can't get hibernate to insert the registered address when it creates a new organisation. It tries to do an update on the ADDRESSES table rather then an insert

Use cascade="all" , (save-update or anything else, that is appropriate ) for the office-to-registeredAddress association.

---------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 5:08 am 
Newbie

Joined: Thu May 03, 2007 3:58 am
Posts: 7
Location: London
I have tried that. Not tried other cascade opptions but they should be included in the cascade should they not? I have also tried to use the constrained attribute.

_________________
Tomas


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 5:14 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Can you post your mapping file ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 5:27 am 
Newbie

Joined: Thu May 03, 2007 3:58 am
Posts: 7
Location: London
The current incarnation of the mapping in the organisation.hbm.xml is this:
Code:
      <one-to-one name="registeredAddress"
         class="Address"
         cascade="all" constrained="true">
         <formula>from TPD_ADDRESSES a where a.ORG_ID = ORG_ID and a.REG_ADDRESS = 'y'</formula>
      </one-to-one>


and for the locations:
Code:
      <set name="locations">
         <key>
            <column name="ORG_ID" precision="22" scale="0" />
         </key>
         <one-to-many
            class="Address" />
      </set>


The address mapping file have no reference to the organisation, only a set of property mappings for the data values.

_________________
Tomas


Last edited by tomas@procserve on Fri May 04, 2007 11:50 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 11:48 am 
Newbie

Joined: Thu May 03, 2007 3:58 am
Posts: 7
Location: London
I have now given up this, it seems to be impossible to do this association as a mapping so I am now reverting to doing it in code. This is painful but here is what I am doing:

In saveOrUpdate I will have to break out all addresses from the organisation. I then cast them to a new class called AddressRecord which is a class that mirrors the table rather then the domain. This is then saved back to the database. This also means that I will have to remove addresses manually when they are changed in the organisation.

I the find methods I simply use some HQL to find all AddressRecords and then loop through the list to create the Address objects and also set the registered address whilst I'm in the loop.

This will work when it is done but it's pain full and exactly the kind of thing that I though Hibernate would be able to do.

If anyone has any ideas, either how to structure the database better, which our db developer wont be happy with but anyway, or how to map this it would be appreciated.

And pramodkp, thanks for your time and effort. I am still hoping you will come up with a solution :)

_________________
Tomas


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