Hibernate使用一对多映射连接子类

时间:2013-02-08 15:50:14

标签: java hibernate

如何实现一个抽象超类A有2个子类B和C的模型.B与另一个类X具有双向一对多关系。

我使用的连接子类映射没有鉴别器和anotaions(没有xml)。 它的工作方式几乎完美但是B中的id在X类中为null。 X类可以从超类中获取id吗?或者它是否在B中搜索id。

超级A

import javax.persistence.*;

import org.hibernate.annotations.Cascade;

@Entity
@Table(name="t_trip")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Trip {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
private String name;
private boolean publicTrip;
private String description;

public Trip(){

}

public Trip(String name, String description, boolean publicTrip) {
    this.name = name;
    this.description = description;
    this.publicTrip = publicTrip;
 }
}

B小组

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.*;
import org.hibernate.annotations.Cascade;
@Entity
@Table(name = "t_repeatedtrip")
@PrimaryKeyJoinColumn(name = "id")
 public class RepeatedTrip extends Trip {
private Date startMoment;
private String intervalType;
private int tripInterval;
private int tripFrequency;
@OneToMany(mappedBy = "repeatedTrip")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private List<TripExecutionMoment> tripExecutionMoments = new ArrayList<TripExecutionMoment>();

public RepeatedTrip() {
}

public RepeatedTrip(String name, String description,boolean publicTrip, Date startMoment,
        String intervalType, int interval, int frequency) {
    super(name, description, publicTrip);
    this.startMoment = startMoment;
    this.intervalType = intervalType;
    this.tripInterval = interval;
    this.tripFrequency = frequency;
}
}

OneToMany相关类X

package be.kdg.teamB.model.entities;

import java.util.Date;

import javax.persistence.*;

import org.hibernate.annotations.Cascade;

@Entity
@Table(name = "t_tripexecutionmoment")
public class TripExecutionMoment {
@Id
@GeneratedValue
private int id;
private Date executionPointOfTime;
@ManyToOne
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn(name = "repeatedTripId")
private RepeatedTrip repeatedTrip;

public TripExecutionMoment() {
}

public TripExecutionMoment(Date executionPointOfTime) {

    this.executionPointOfTime = executionPointOfTime;
}

    return executionPointOfTime;
}

public void setExecutionPointOfTime(Date executionPointOfTime) {
    this.executionPointOfTime = executionPointOfTime;
}

public RepeatedTrip getRepeatedTrip() {
    return repeatedTrip;
}

public void setRepeatedTrip(RepeatedTrip repeatedTrip) {
    this.repeatedTrip = repeatedTrip;
}



 }

在此方法中调用代码:

public RepeatedTrip createRepeatedTrip(String name, String description,boolean publicTrip,
        Date startMoment, String intervalType, int interval,
        int frequency) {

    session = HibernateUtil.getSessionFactory().getCurrentSession();
    tx = session.beginTransaction();

    RepeatedTrip trip = new RepeatedTrip(name, description,publicTrip, startMoment,
            intervalType, interval, frequency);
    int intervalDefiner;
    Date date = trip.getStartMoment();
    switch(intervalType){
    case "D":
        intervalDefiner = Calendar.DATE;
        break;
    case "M":
        intervalDefiner = Calendar.MONTH;
        break;
    case "W":
        intervalDefiner = Calendar.WEEK_OF_YEAR;
        break;
    case "Y":
        intervalDefiner = Calendar.YEAR;
        break;
    default:
        intervalDefiner = Calendar.DATE;
        break;
    }
    for(int i=0; i<frequency;i++){

        TripExecutionMoment tripExecutionMoment = new TripExecutionMoment(date);
        trip.getTripExecutionMoments().add(tripExecutionMoment);
        date = nextDate(intervalDefiner, interval, date);
    }


    session.saveOrUpdate(trip);
    tx.commit();
    return trip;

}

1 个答案:

答案 0 :(得分:0)

这就是我的想法。您正在将TripExecutionMoment添加到RepeatedTrip:

trip.getTripExecutionMoments().add(tripExecutionMoment);

但是你没有初始化双向关联的所有者方,这是Hibernate唯一关心的方法:

tripExecutionMoment.setRepeatedTrip(trip);