我有两个表由以下实体对象层次结构表示:
@Entity
@Table(name = Transport.TABLE_NAME)
@DiscriminatorColumn(name="transport_type", discriminatorType = DiscriminatorType.INTEGER)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class Transport {
...
private Date departure;
}
@Entity
@DiscriminatorValue("1")
public class Bicycle extends Transport {
...
@OneToOne(mappedBy = "transport", fetch = FetchType.LAZY)
private BikePassenger passenger;
}
@Entity
@DiscriminatorValue("2")
public class Car extends Transport {
...
@OneToMany(mappedBy = "transport", fetch = FetchType.EAGER)
private List<CarPassanger> passengers;
}
@Entity
@Table(name = Passenger.TABLE_NAME)
@DiscriminatorColumn(name="passenger_type", discriminatorType = DiscriminatorType.INTEGER)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class Passenger {
...
private int passengerGUID;
}
@Entity
@DiscriminatorValue("1")
public class BicyclePassenger extends Passenger {
...
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "transportid")
private Bicycle transport;
}
@Entity
@DiscriminatorValue("2")
public class CarPassenger extends Passenger {
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "transportid")
private Car transport;
}
现在使用JPA Criteria API(或至少JPA),我该怎么做:
我认为#1应该有很好的解决方案,但我只能为每个子类型提供2个子选择。这看起来很难看。
最后第三个问题 - 它是一个好模型吗?从OOP的角度来看,它看起来还不错,但从ORM的角度来看,查询的容易程度看起来不太好......
P.S。我正在使用hibernate JPA 2.1