0

I'm using Hibernate + HSQL on JBOSS server, I need to saveOrUpdate() an Object which has an ID represented by another class:

public class RideId implements java.io.Serializable {

    private int beginPop;
    private int endPop;
    private String requestUser;

public RideId() {
}

public RideId(int beginPop, int endPop, String requestUser) {
    this.beginPop = beginPop;
    this.endPop = endPop;
    this.requestUser = requestUser;
}
...

so, "RideID" is the ID of the entity "Ride"

public class Ride implements java.io.Serializable {

private RideId id;
private User userByRequestUser;
private User userByAcceptUser;
private Pop pop;
private Boolean rideStatus;

public Ride() {
}

public Ride(RideId id, User userByRequestUser, Pop pop) {
    this.id = id;
    this.userByRequestUser = userByRequestUser;
    this.pop = pop;
}

public Ride(RideId id, User userByRequestUser, User userByAcceptUser,
        Pop pop, Boolean rideStatus) {
    this.id = id;
    this.userByRequestUser = userByRequestUser;
    this.userByAcceptUser = userByAcceptUser;
    this.pop = pop;
    this.rideStatus = rideStatus;
}
...

how can I saveOrUpdate() a new Object of type Ride?

Thanks everyone and sorry for my english!

Lorenzo Barbagli
  • 1,241
  • 2
  • 16
  • 34
  • By using saveOrUpdate method, Where is your error? – commit Apr 09 '13 at 08:39
  • is this a duplicated question? I guess you are after something like composite ID. http://stackoverflow.com/questions/2301259/hibernate-composite-key – spiritwalker Apr 09 '13 at 08:47
  • yes, is a composite ID, but the classes are right, caming from a reverse engeneering already made by hibernate, I can't save a new object of type Ride because I don't know how to instanciate his ID! @Fenil Shah: I'm able to use saveOrUpdate method, but on entities that have a unique id (id composed by only 1 coloumn), with a composite id I don't know, for example, on which object call saveOrUpdate – Lorenzo Barbagli Apr 09 '13 at 09:00
  • First of all you need to be sure there is one to one mapping between your Ride and RideId class in your configuration file. Now you just have to pass Ride class object to your saveOrUpdate method as you have RideId object in Ride class hibernate will fetch your composite key data from RideId object and save it. – commit Apr 09 '13 at 09:51
  • @FenilShah `RideId` shouldn't be a mapped class. – dcernahoschi Apr 09 '13 at 09:53
  • @dcernahoschi Look at Ride class. He has RideId object in it, so RideId must be a mapping class. – commit Apr 09 '13 at 10:03
  • 1
    @FenilShah No. Please check the hibernate reference: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch05.html#mapping-declaration-id. Scroll a little bit to the section "Composite identifier". The composite id is not an `@Entity`, nor a `` – dcernahoschi Apr 09 '13 at 10:11
  • @dcernahoschi oh ya you are right, thanks for make me understand. – commit Apr 09 '13 at 10:28
  • @dcernahoschi & FenilShah thank you for answering, I don't use annotations because I'm using hbm mapping classes, I know that RideId isn't an entity but it's however represented by a Java class. – Lorenzo Barbagli Apr 09 '13 at 15:17

1 Answers1

0

It's simple. You need to create first a new RideId, assign it to a new Ride and call saveOrUpdate with the Ride.

RideId id = new RideId(1, 2, "someuser");
Ride ride = new Ride(id, ...);
session.saveOrUpdate(ride);
dcernahoschi
  • 14,968
  • 5
  • 37
  • 59