hibernate:插入带有外部“id类”的实例

时间:2013-04-09 08:32:30

标签: java hibernate insert save identifier

我在JBOSS服务器上使用Hibernate + HSQL,我需要saveOrUpdate()一个具有由另一个类表示的ID的Object:

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;
}
...

所以,“RideID”是实体“Ride”的ID

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;
}
...

如何saveOrUpdate()一个新的Ride类型的对象?

谢谢大家,对不起我的英语!

1 个答案:

答案 0 :(得分:0)

这很简单。您需要先创建一个新的RideId,将其分配给新的Ride并使用saveOrUpdate致电Ride

RideId id = new RideId(1, 2, "someuser");
Ride ride = new Ride(id, ...);
session.saveOrUpdate(ride);