-->
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.  [ 3 posts ] 
Author Message
 Post subject: which mapping shud I use and how..
PostPosted: Fri Oct 21, 2005 3:11 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

I am new to hibernate and needed help on which mapping I shud use.

I have class Vehicle. This class contains an object of type VehicleType.
There can be many vehicles of one particular type. I was using a many-to-one unidirectional mapping between Vehicle and VehicleType with a foreign key in Vehicle mapping to the primary key in Vehicle Type. I was using a field in Vehicle class and in its table called FKey_vtID which was mapped to the primary key of the vehicle. I want to avoid using this extra field which will need to be checked. Vehicle Type class and table have no reference to the Vehicle. Am I using the correct mapping. IF i want to avoid using the foreign key column and still have them mapped. How shud my mapping files look like.

I wud be grateful for any comments.
Thanx
Shefali


Top
 Profile  
 
 Post subject: Re: which mapping shud I use and how..
PostPosted: Fri Oct 21, 2005 3:42 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Code:
public class Vehicle {

  private Long id;
  private VehicleType type;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
     this.id= id;
  }

  public VehichleType getVehicleType() {
    return this.type;
  }

  public void setVehicleType(VehicleType vType) {
     this.type = vType;
  }

}


public class VehicleType {

  private Long id;

  private String description;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
     this.id= id;
  }
   public String getDescription() {
     return description;
  }

  public void setDescription(String description) {
     this.description= description;
 
}

The only this special on the mapping is on the Vehicle Mapping

Vehicle.hbm.xml

...
<many-to-one name="type" column="VEHICLE_TP_ID"/>


_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: do i need to save Vehicletype explicityly when saving vehicl
PostPosted: Fri Oct 21, 2005 6:11 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

Thank you for replying to my message, the example u gave was simple and understandable and so very useful.

I now removed the extra field i kept in the class as the foreign key.
But when I save vehicle I will have to check to see if the vehicle type is already there in the vehicle type table, if not explicitly save it. I am doing this by retrieving the vehicleType from the vehicle and checking to see if its id has some value if not first save the vehicle type and then the vehicle, otherwise just save the vehicle. Also I do not have getters and setters for VehicleType in my vehicle class. Do i really need them? Am I doing something wrong??

Thanx again
Shefali


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