Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Joined Subclasses with One to Many mapping
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Superclass A</p> <pre><code>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; } } </code></pre> <p>Subclass B</p> <pre><code>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&lt;TripExecutionMoment&gt; tripExecutionMoments = new ArrayList&lt;TripExecutionMoment&gt;(); 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; } } </code></pre> <p>OneToMany related class X</p> <pre><code>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></pre> <p>Code is called in this method: </p> <pre><code>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&lt;frequency;i++){ TripExecutionMoment tripExecutionMoment = new TripExecutionMoment(date); trip.getTripExecutionMoments().add(tripExecutionMoment); date = nextDate(intervalDefiner, interval, date); } session.saveOrUpdate(trip); tx.commit(); return trip; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload