抽象スーパークラスAにBとCの2つのサブクラスがあるモデルをどのように実装しますか。Bは別のクラスXと双方向の1対多の関係にあります。
ディスクリミネーターなしで、アノタイオン(xmlなし)を使用して、結合されたサブクラスマッピングを使用しています。ほぼ完全に機能しますが、XクラスではBのIDが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;
}