假设以下类模型:(从内存中写入)
@Entity
@Table(name="BULLETIN_MESSAGE")
public class BulletinMessage {
@OneToMany
@JoinColumn(name="BULLETIN_ID")
private List<MeteoMessage> messages;
}
@MappedSuperclass
public abstract class MeteoMessage {
private String firLocation;
}
@Entity
@Table(name="METAR_MESSAGE")
@PrimaryKeyJoinColumn(name="metar_id")
public class MetarMessage extends MeteoMessage {
}
@Entity
@Table(name="AIRMET_MESSAGE")
@PrimaryKeyJoinColumn(name="airmet_id")
public class AirmetMessage extends MeteoMessage {
}
我想让所有AftnMessages从特定的firLocation加入MetarMessages。但是当我试着写:
Criteria criteria = session.createCriteria(BulletinMessage.class, "bulletin").createAlias("messages", "meteo", JoinType.LEFT_OUTER_JOIN);
criteria.add(Restrictions.eq("meteo.firLocation", "EPWA"));
我得到以下sql:
select ... from BULLETIN_MESSAGE b
LEFT OUTER JOIN AIRMET_MESSAGE a on ....
LEFT OUTER JOIN METAR_MESSAGE m on ...
WHERE a.firLocation = ?
请注意,hibernate在'where'表达式中选择了AIRMET_MESSAGE。如何强制hibernate在'where'表达式中选择METAR_MESSAGE:
WHERE.firLocation =?
答案 0 :(得分:1)
MeteoMessage
必须是一个实体,因此可以使用@Entity
进行注释,而不是@MappedSuperclass
,以便能够创建与之关联。
然后你应该可以使用
criteria.add(Restrictions.eq("meteo.class", MetarMessage.class));