-->
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.  [ 5 posts ] 
Author Message
 Post subject: Foreign key not-null="true" throwing PropertyValu
PostPosted: Fri Apr 13, 2007 11:39 am 
Newbie

Joined: Thu Mar 08, 2007 12:59 pm
Posts: 9
All,
Basically I want Foreign key automatically setup from Parent class
if possible.


Here is two mappings:

Code:

from class Person mapping file:

   <set name="vehicles" table="Vehicles" cascade="save-update">
      <key column="ssn"/>
      <one-to-many class="com.dmv.Vehicle"/>
   </set>

from class Vehicle mapping file:

    <property name="ssn" type="string">
      <column name="SSN" length="9" not-null="true"/>
    </property>



and Here is test code:

Code:
                /* save basic person info */
      personDAO.save(person);

      Vehicle vehicle = new Vehicle();
      
      vehicle.setColor("Silver");

      vehicle.setMakeModel("Ford/Mustang");

      vehicle.setPlate("XXX 3344");

      vehicle.setYear("2007");

      vehicle.setMileage("100");
      
      Set<Vehicle> vehicles = new HashSet<Vehicle>();
      
      vehicles.add(vehicle);
      
      person.setVehicles(vehicles);
      /*add vehicle info to this person */
      personDAO.update(person);


And it throw PropertyValueException because of not-null property ssn from Vehicle class. If i remove not-null="true", then this works fine. but I want to make sure this SSN is not-null property.
Calling vehicle.setSSN(ssn); throws another exception as it try to update ssn property with null value first, and then update ssn property with correct value.

Can anybody knows how to fix this problem?

Many thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:56 pm 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi Babo70,

when you specify for the FK not-null="true", you have to specify it in your code as well: before updating the person, set it as the owner of the vehicle:

Code:
  vehicles.add(vehicle);
   person.setVehicles(vehicles);
      /*add vehicle info to this person */

  vehicle.setPerson (person); // I assume that Vehicle has a Person object
  personDAO.update(person);

chucky

Don't forget to rate if this helps
------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 3:09 pm 
Newbie

Joined: Thu Mar 08, 2007 12:59 pm
Posts: 9
One credit from me first.
I didn't have person in Vehicle. it was unidirectional.

Vehicles in Person. But No Person in Vehicle. Vehicle only get referece of Person by SSN.

I am not sure this design is good or bad. any thoughts on this?

Unidirectional <one-to-many> is not recommended from Hibernate manual but I don't know why?.


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 3:10 pm 
Newbie

Joined: Thu Mar 08, 2007 12:59 pm
Posts: 9
One credit from me first.
I didn't have person in Vehicle. it was unidirectional.

Vehicles in Person. But No Person in Vehicle. Vehicle only get referece of Person by SSN.

I am not sure this design is good or bad. any thoughts on this?

Unidirectional <one-to-many> is not recommended from Hibernate manual but I don't know why?.


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 3:45 pm 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi Babo70,
you're right. The manual also keeps the not-null contraint only on the DB level. Not in the mapping file.
I think that would also be enough to make sure your constraint on SSN is not null. However, follow their recommendation and use a join table instead of a FK to join the 2 tables.

chucky


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