@OneToMany关系属性未填写

时间:2014-07-07 10:26:45

标签: jpa jpql named-query table-per-subclass

我已经实现了加入的多表继承

有一个'父'表 pois 和两个子表: xPois yPois ,反过来我有一个抽象 PoiDao 类以及 XPoiDao YPoiDao 类扩展 PoiDao

poi可能有多个预订,但保留只属于一个poi。

子表中定义的命名查询DAO适用于在相应(直接)表层次结构中定义的属性。父表与另一个名为 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;
    ...
    }

1 个答案:

答案 0 :(得分:0)

你在那里FetchType.LAZY。当您尝试访问它时,您是否获得一个空列表?调试器可能不会触发获取请求。