我已经实现了Joined, Multiple Table Inheritance。
有一个“父”表pois和两个子表:xPois和yPois,反过来我有一个抽象的PoiDao类以及一个XPoiDao和一个扩展PoiDao的YPoiDao类。
一个 poi 可能有多个预留,但一个预留只属于一个 poi。
在子表 DAO 中定义的命名查询适用于在相应(直接)表层次结构中定义的属性。父表与另一个名为reservations的表有外键关系(表reservations保存表pois的外键)。问题是未获取此预订表中的记录。
在 MySql Workbench 中运行这个 SQL 语句可以获得所需的结果集:
SELECT * FROM xPois pp
LEFT JOIN pois p ON pp.poiId = p.poiId
LEFT JOIN reservations r ON p.poiId = r.poiId
WHERE pp.xPoiId = '2011';
在 Eclipse 中,当我在调试模式下检查 xDao 实例时,我可以看到{IndirectList: not instantiated} 。
如何使用 JPA 从此表中获取存储在 PoiDao 中的记录?
public abstract class PoiDao implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="poiId")
private Integer poiId;
@OneToOne(optional=false, cascade=CascadeType.ALL)
@JoinColumn(name="addressId",insertable=true,
updatable=true, unique=true, nullable=false)
private AddressDao address;
@Embedded
private GeoLocationDao geoLocation;
@Convert("poiTypeConverter")
private ServiceTypeEnum poiType;
@Column(name="operator")
private String operator;
@Column(name="reservable")
private boolean reservable;
@OneToMany(orphanRemoval=true, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name="poiId", insertable=true, updatable=true)
private List<ReservationDao> existingReservations;
...
}
@Entity
@Table(name="xPois")
@DiscriminatorValue("X")
@NamedQueries({
@NamedQuery(name="XPoiDao.findAll", query="SELECT p FROM XPoiDao p"),
@NamedQuery(name="XPoiDao.findByXPoiId",
query="SELECT pp FROM XPoiDao pp LEFT JOIN PoiDao p ON pp.poiId = p.poiId "
+ "LEFT JOIN ReservationDao r ON p.poiId = r.poiId WHERE pp.xPoiId = :xPoiId")
})
@ObjectTypeConverters({
@ObjectTypeConverter (
name="xPoiStatusConverter",
dataType=java.lang.String.class, // type in DB
objectType=XPoiStatusEnum.class, // Java type
conversionValues={
@ConversionValue(dataValue="FREE", objectValue="FREE"),
@ConversionValue(dataValue="OCCUPIED BY VALUE", objectValue="OCCUPIED_BY_VALUE"),
@ConversionValue(dataValue="OCCUPIED MANUALLY", objectValue="OCCUPIED_MANUALLY"),
@ConversionValue(dataValue="BLOCKED", objectValue="BLOCKED")
}
)
})
public class XPoiDao extends PoiDao implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2496267921294255723L;
// @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="xPoiId")
private String xPoiId;
@Convert("xPoiStatusConverter")
@Column(name="status")
private XPoiStatusEnum status;
@Embedded
private ContactDao contact;
// @OneToMany(orphanRemoval=true, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
// @JoinColumn(name="poiId",insertable=true,updatable=true)
// private List<ReservationDao> existingReservations;
@OneToMany(orphanRemoval=true, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name="parkingPoiId",insertable=true,updatable=true)
private List<OperatingHourDao> operatingHours;
...
}