操作hibernate多主键的问题?(2)

03-08-25 zhengzhiyong
关于多主键的操作,又出现了以下问题,请大家指教:

现在我的数据库有个表reservation(暂叫主表)和另外两个表person,flight(暂叫从表)关联,reservation有三个主键,

(reservation_id,person_id_fk,flight_id_fk),其中person_id_fk,flight_id_fk同时也是外键,关联person,flight表。

我把我的hbm,value object class,主键类,调用方法贴出来:

reservation.hbm.xml: mapping 文件

<hibernate-mapping>

<class

name="airline.hibernate.Reservation"

table="reservations"

>

<composite-id name="comp_id" class="airline.hibernate.ReservationPK">

<key-property name="reservationId" column="reservation_id" type="int" />

<key-property name="personIdFk" column="person_id_fk" type="int" />

<key-property name="flightIdFk" column="flight_id_fk" type="int" />

</composite-id>

.............

.........

.......

</class>

</hibernate-mapping>

Reservation.java:持久类

package airline.hibernate;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;

import org.apache.commons.lang.builder.HashCodeBuilder;

import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */

public class Reservation implements Serializable {

/** identifier field */

private airline.hibernate.ReservationPK comp_id; //主键类

/** persistent field */

private java.util.Date registrationUtc;

/** nullable persistent field */

private String comment;

//其他列的定义

.......

.......

.......

}

ReservationPK.java : 主键类

package airline.hibernate;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;

import org.apache.commons.lang.builder.HashCodeBuilder;

import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */

public class ReservationPK implements Serializable {

private int reservationId;

private int personIdFk;

private int flightIdFk;

......

.....

.....

equasl(){

.....

}

hashCode(){

......

}

}

测试方法:

public void testAddRe() throws HibernateException{

Person person = testLoadPerson(); //load person

Flight flight = testLoadFlight(); //load flight

ReservationPK compid = new ReservationPK();

compid.setReservationId(100);

compid.setPersonIdFk(1); //personID既是主键也是外键

compid.setFlightIdFk(100); //flightID既是主键也是外键

Reservation re = new Reservation();

re.setComp_id(compid);

re.setRegistrationUtc(new java.util.Date());

re.setComment("love yo1");

re.setFlight(flight);

re.setPerson(person);

Session s = sessionFactory.openSession();

Transaction tx = null;

try{

tx = s.beginTransaction();

s.save(re,compid); //or s.saveOrUpdate(re);

try{

tx.commit(); //commit是抛出 ArrayIndexOutOfBoundsException异常

}catch (ArrayIndexOutOfBoundsException abe) {

abe.printStackTrace() ;

}

}catch(HibernateException he){

if ( tx!=null ){

tx.rollback();

}

throw he;

}finally{

s.close();

}

}

以上主表的delete,load均没问题.load 如下:

re = (Reservation)s.load( Reservation.class, compid );

但是在save和saveOrUpdate()使出现异常,打出的hql语句如下:

insert into reservations (registration_utc, comment, reservation_id, person_id_fk, flight_id_fk) values (?, ?, ?, ?, ?)

如果 person_id_fk,flight_id_fk 两列不是外键,只是普通的主键,则没有任何问题。

请大家帮忙看一下

robbin
2003-08-25 16:43

zhengzhiyong
2003-08-25 18:22
谢谢,robbin :)

如果说有问题,可是load,delete时都没有问题,偏偏save()有问题,

真搞不明白???

zhengzhiyong
2003-08-27 16:45
请大家看看上面的问题,帮帮忙!

还有就是,如果我想更新表主键的值和其他列的值,用hibernate怎没作?

session.save(persistent);这样更新其他列可以,如果把主键重新赋值,

则提示错误,找不到制定列?

请各位帮忙看看!

谢谢

猜你喜欢