【发布时间】:2015-09-24 07:36:05
【问题描述】:
我有一个包含与少数实体的多态关联的实体。它的定义:
@Entity
@Table(name = "notifications")
public class Notification implements java.io.Serializable {
//some fields ...
//Polymorphic association
@Any (metaColumn = @Column(name = "target_type"), fetch = FetchType.LAZY)
@AnyMetaDef(idType = "integer", metaType = "string",
metaValues = {
@MetaValue(targetEntity = Task.class, value = "Task"),
@MetaValue(targetEntity = AdminNote.class, value = "AdminNote"),
@MetaValue(targetEntity = UserCancelRequest.class, value = "UserCancelRequest"),
//@MetaValue(targetEntity = TransferInfo.class, value = "TransferInfo"),
//@MetaValue(targetEntity = NoteSubscription.class, value = "NoteSubscription")
})
@JoinColumn(name = "target_id")
@Getter
@Setter
private NotificationAssociation target;
接口NotificationAssociation表示的我的多态关联:
public interface NotificationAssociation {
/**
* Return actual type of associated entity
* @return one of {@link NotificationTargetType}
*/
String getTargetType();
/**
* Return associated entity id. Used in queries
* @return target entity id
*/
Integer getTargetId();
}
这行得通。但我需要通过静态元模型 API 从标准中的 Notification 类访问 target。我的元模型:
@StaticMetamodel(Notification.class)
public class Notification_ {
public static volatile SingularAttribute<Notification, Integer> id;
public static volatile SingularAttribute<Notification, Integer> postedById;
public static volatile SingularAttribute<Notification, Integer> adminId;
public static volatile SingularAttribute<Notification, NotificationAssociation> target;
public static volatile SingularAttribute<Notification, Date> hiddenAt;
public static volatile SingularAttribute<Notification, Date> createdAt;
}
在 HQL/JPQL 我可以访问它, 但在元模型中 target 为空,当我通过 Root#getModel() 获取模型时,属性列表中不存在 target 。 我的问题是我可以这样做还是有其他解决方案。感谢您的帮助
【问题讨论】:
标签: java hibernate jpa criteria-api