How would you implement a model where an abstract superclass A has 2 subclasses, B and C. B has a bidirectional one-to-many relation with another class X.
I am using the joined subclass mapping without discriminator and with anotaions (no xml). It works almost perfectly but the id from B is null in the X class. Can the X class get the id from the superclass? Or is it searching for an id in B.
Superclass 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;
}
}
Subclass 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 related class 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;
}
}
Code is called in this method:
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;
}