Java 如何实现具有泛型关系的多态JPA实体
我试图使用JPA2.0创建具有泛型关系的多态实体。应该有两个表,一个事件表和一个通知表。在这些表中是相互关联的具体实体,如下所示:Java 如何实现具有泛型关系的多态JPA实体,java,hibernate,jpa-2.0,metamodel,Java,Hibernate,Jpa 2.0,Metamodel,我试图使用JPA2.0创建具有泛型关系的多态实体。应该有两个表,一个事件表和一个通知表。在这些表中是相互关联的具体实体,如下所示: Event <---------- Notification<X extends Event> | | LoginEvent <------ LoginNotification extends Notification<LoginEvent> 当我尝试在条件查询中执行联接时,我得到一
Event <---------- Notification<X extends Event>
| |
LoginEvent <------ LoginNotification extends Notification<LoginEvent>
当我尝试在条件查询中执行联接时,我得到一个错误:
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<LoginNotification> query = cb.createQuery(LoginNotification.class);
Root<LoginNotification> root = query.from(LoginNotification.class);
// This line complains: Type mismatch: cannot convert from
// Join<LoginNotification,Event> to Join<LoginNotification,LoginEvent>
Join<LoginNotification, LoginEvent> join =
root.join(LoginNotification_.event, JoinType.INNER);
根据一些帖子,泛型关系不起作用(),但通过使用@ManyToOne(optional=false,targetEntity=Event.class)
注释,我们可以让它们正常工作。不幸的是,泛型似乎破坏了JPA条件查询
对于如何执行此查找,是否有任何建议?我可以在代码中使用LoginNotification.getEvent()
,但在JPA元模型联接中不能使用LoginNotification.event
。除了使用泛型实现这一点,还有什么替代方法
@Pascal Thivent-您能回答这个问题吗?一个解决方案是避免使用“join”函数,而是执行完全交叉连接:
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<LoginNotification> query = cb.createQuery(LoginNotification.class);
Root<LoginNotification> notfRoot = query.from(LoginNotification.class);
Root<LoginEvent> eventRoot = query.from(LoginEvent.class);
...
query.where(cb.equals(notfRoot.get(Notification_.event), eventRoot.get(Event_.id)), ...(other criteria));
EntityManager em=getEntityManager();
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery=cb.createQuery(LoginNotification.class);
Root notfRoot=query.from(LoginNotification.class);
Root eventRoot=query.from(LoginEvent.class);
...
其中(cb.equals(notfRoot.get(Notification.event)、eventRoot.get(event.id))、…(其他条件));
我认为一个好的查询优化器应该能很快做到这一点,但如果有人对这种方法的效率有任何见解,我会非常乐意听到 我试过你的通用代码,@logan 但我最终发现最简单的方法是让
T
implementsSerializable
@Entity
public class IgsSubject extends BasicObject implements Serializable{
private static final long serialVersionUID = -5387429446192609471L;
好极了,不管怎样,我过去总是用这种方式在SQL中编写连接。
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<LoginNotification> query = cb.createQuery(LoginNotification.class);
Root<LoginNotification> root = query.from(LoginNotification.class);
// This line complains: Type mismatch: cannot convert from
// Join<LoginNotification,Event> to Join<LoginNotification,LoginEvent>
Join<LoginNotification, LoginEvent> join =
root.join(LoginNotification_.event, JoinType.INNER);
public abstract class LoginNotification_ extends Notification_ {
// Adding this Removes Type mismatch error, but causes run-time error
public static volatile SingularAttribute<LoginNotification, LoginEvent> event;
...
}
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<LoginNotification> query = cb.createQuery(LoginNotification.class);
Root<LoginNotification> notfRoot = query.from(LoginNotification.class);
Root<LoginEvent> eventRoot = query.from(LoginEvent.class);
...
query.where(cb.equals(notfRoot.get(Notification_.event), eventRoot.get(Event_.id)), ...(other criteria));
@Entity
public class IgsSubject extends BasicObject implements Serializable{
private static final long serialVersionUID = -5387429446192609471L;