-->
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: How to map class including <set> of abstract classes?
PostPosted: Thu Mar 02, 2006 2:01 pm 
Newbie

Joined: Fri Jan 20, 2006 1:32 pm
Posts: 11
Hello,
I have a class Garage and I want it to have a <set> of Vehicles (abstract class). Concrete vehicles are "Car extends Vehicle" and "Truck extends Vehicle".

Following mapping doesn't work:
Code:
Caused by: java.sql.BatchUpdateException: Table 'database.Vehicle' doesn't exist


Garage
Code:
   <class name="Garage" table="garage">
      <id name="garageId" type="integer" column="garage_id">
         <generator class="increment" />
      </id>

      <property ...>

      <set name="vehicles">
         <key column="garage_id"></key>
         <one-to-many
            class="Vehicle" />
      </set>

   </class>


abstract Vehicle
Code:
   <class name="Vehicle" abstract="true">

      <id name="id" type="integer" column="id">
         <generator class="increment" />
      </id>
      
      <property ...>

      <union-subclass name="Car"
         table="car">
      </union-subclass>

      <union-subclass name="Truck"
         table="truck">
      </union-subclass>

   </class>


Thank you
Lucas


Top
 Profile  
 
 Post subject: Re: How to map class including <set> of abstract class
PostPosted: Thu Mar 02, 2006 4:01 pm 
Newbie

Joined: Fri Jan 20, 2006 1:32 pm
Posts: 11
Well, I've put this (inverse, cascade) into mapping and it's ok

Code:
      <set name="vehicles" inverse="true" cascade="all">
         <key column="garage_id"></key>
         <one-to-many
            class="Vehicle" />
      </set>


But now I have always garage_id NULL, even when inverse="true" so set of vehicles dedicated to garage is always empty.

Any ideas?

Thank you


Top
 Profile  
 
 Post subject: Re: How to map class including <set> of abstract class
PostPosted: Thu Mar 02, 2006 4:05 pm 
Beginner
Beginner

Joined: Thu Feb 16, 2006 9:53 am
Posts: 24
inverse attribute is usefull for bidirectional association.
Is there a "garage" attribute in Vehicle.java?

Could you provide the full mapping files and the code used to create objects, please?

Alexis


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 4:37 pm 
Newbie

Joined: Fri Jan 20, 2006 1:32 pm
Posts: 11
Garage
Code:
   <class name="Garage" table="garage">
      <id name="garageId" type="integer" column="garage_id">
         <generator class="increment" />
      </id>
      <property name="name" />

      <set name="vehicles" inverse="true" cascade="all">
         <key column="garage_id"></key>
         <one-to-many
            class="Vehicle" />
      </set>

   </class>


Vehicle
Code:
   <class name="Vehicle" abstract="true">

      <id name="id" type="integer" column="id">
         <generator class="increment" />
      </id>
      
      <property name="name" type="string" />

      <union-subclass name="Car"
         table="car">
      </union-subclass>

      <union-subclass name="Truck"
         table="truck">
      </union-subclass>
   </class>


Garage.java
Code:
public class Garage implements Serializable {
   
    private Set<Vehicle> vehicles;
.
.


Vehicle.java
Code:
public abstract class Vehicle implements Serializable {
   
    private Integer garage_id;
    .
    .


Creating objects
Code:
            tx = session.beginTransaction();

            Garage garage = new Garage("name");

            Vehicle vehicle = new Car("name");
           
            //session.save(vehicle);
           
            Set<Vehicle> set = new HashSet<Vehicle>();
            c.add(vehicle);
            garage.setVehicles(set);
           
            session.save(garage);
            tx.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 5:15 pm 
Beginner
Beginner

Joined: Thu Feb 16, 2006 9:53 am
Posts: 24
If your association is bidirectional (which seems to be your intention), you should declare a Garage type attribute (instead of an integer) and map it in Vehicle.hbm.xml.
(oversight?)

Something like:
Code:
<many-to-one name="garage"
                     class="Garage"
                     column="GARAGE_ID"/>


Moreover your Set is initialisation is quite ugly.
Just write :
Code:
private Set<Vehicle> vehicles = new HashSet();


If you actually choose to set the inverse & cascade attributes on the Garage side, you should add a "addVehicle(...)" method which would link the vehicle to the the garage:
Code:
public void addVehicle(Vehicle v){
     vehicles.add(v);
     v.setGarage(this);
}


So your main method becomes:
Code:
tx = session.beginTransaction();
Garage garage = new Garage("name");
Vehicle vehicle = new Car("name");
garage.addVehicle(vehicle);
session.save(garage);
tx.commit();


To conclude, may I advise you to read the following sections of the Reference Manual:
1.3.5. Bi-directional associations
1.3.6. Working bi-directional links

of course the reading of the full manual may be usefull ;)


Alexis
Don't forget to rate ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 8:54 am 
Newbie

Joined: Fri Jan 20, 2006 1:32 pm
Posts: 11
Well, I solved it:

Hibernate manual says:

table per concrete-class (union-subclass)
Code:
<one-to-many> (for inverse="true" only)


So I cannot make unidirectional <one-to-many> using union-subclass aproach.
(but thank you for hints)

Lukas


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.