操作hibernate多主鍵的問題?(2)

zhengzhiyong發表於2003-08-25
關於多主鍵的操作,又出現了以下問題,請大家指教:

現在我的資料庫有個表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 兩列不是外來鍵,只是普通的主鍵,則沒有任何問題。

請大家幫忙看一下

相關文章