-->
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.  [ 1 post ] 
Author Message
 Post subject: unidirectional many-to-one association on a join table
PostPosted: Thu Aug 30, 2007 4:38 am 
Newbie

Joined: Thu Oct 07, 2004 6:50 pm
Posts: 2
Hibernate version: 3.2.4.sp1

Name and version of the database you are using: MySQL 5.0.27

Hi,

I'm trying to model a unidirectional many-to-one association on a join table (as per chapter 7.3.2 of the documentation):

Code:
<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <join table="PersonAddress"
        optional="true">
        <key column="personId" unique="true"/>
        <many-to-one name="address"
            column="addressId"
            not-null="true"/>
    </join>
</class>

<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
</class>


Everything works fine if I create Person, Address and the connection between them in the same transaction:

Code:
Address address = new Address();
dao.saveAddress(address);

Person person = new Person();
person.setAddress(address);
dao.savePerson(person);


The save() methods use saveOrUpdate() to persist the objects.

The Address record is created, the Person record is created and one record is inserted to PersonAddress.

However, when I work with a Person (which doesn't currently have an Address) created in another session and try to associate it with a new or existing Address, saving the Person will not create the relation data:

Code:
// In another session ...
Person person = new Person();

// flush() etc

Address address = new Address();
dao.saveAddress(address);

person.setAddress(address);
dao.savePerson(person);


What's happening is this: The new Address is created in the DB, the Person record is updated, but instead of inserting a record into PersonAddress, Hibernate is submitting an Update to PersonAddress, with the Person's id in the where clause. Since no Address has been specified for this Person yet, there's nothing to update.

No error message is shown, it just fails silently.

This is a quite common use case, e.g. when using a GUI where the user can associate a Person with an existing Address. I guess I'm missing something fundamental here, which is probably easy to fix.

Thanks in advance!

Nils
[/code]


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

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.