如何使用“构造函数”在“选择子句”中为多个表的选定列编写HQL JOIN查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用“构造函数”在“选择子句”中为多个表的选定列编写HQL JOIN查询相关的知识,希望对你有一定的参考价值。
我正在使用Constructor()
在选择条款中为多个表的选定列编写HQL JOIN查询
我有以下实体:
实体1:NotificationObject.java
@Entity
@Table(name="notification_object")
public class NotificationObject implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy=GenerationType.IDENTITY )
@Column( columnDefinition="INT(10) UNSIGNED" )
private Integer id;
@Column( name="entity_type_id", columnDefinition="TINYINT UNSIGNED", nullable=false )
private Short entityTypeId;
@Column( name="entity_id", columnDefinition="INT(10) UNSIGNED", nullable=false )
private Integer entityId;
@DateTimeFormat( pattern="yyyy-MM-dd" )
@Temporal( TemporalType.TIMESTAMP )
@CreationTimestamp
@Column( name="created_on"/*, nullable=false*/ )
private Date createdOn;
@OneToMany( mappedBy = "notificationObject" )
private Set<Notification> notifications = new LinkedHashSet<>();
public NotificationObject() {}
public NotificationObject(Short entityTypeId, Integer entityId) {
this.entityTypeId = entityTypeId;
this.entityId = entityId;
}
// Getters and Setters
}
实体2:NotificationChange.java
@Entity
@Table(name="notification_change")
public class NotificationChange implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(columnDefinition="INT(10) UNSIGNED")
private Integer id;
@ManyToOne( fetch=FetchType.LAZY )
@JoinColumn(
name="notification_object_id", nullable=false,
foreignKey=@ForeignKey(name="fk_notification_change_notification_object_noti_object_id")
)
private NotificationObject notificationObject;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(
name="actor_id", columnDefinition="INT(10) UNSIGNED", nullable=false,
foreignKey=@ForeignKey(name="fk_notification_change_user_user_id")
)
private User actor;
public NotificationChange() {}
public NotificationChange( User actor ) {
this.actor = actor;
}
// Getters and Setters
}
实体3:Notification.java
@Entity
@Table(name="notification")
public class Notification implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy=GenerationType.IDENTITY )
@Column( columnDefinition="INT(10) UNSIGNED" )
private Integer id;
@ManyToOne( fetch=FetchType.LAZY )
@JoinColumn(
name="notification_object_id", nullable=false,
foreignKey=@ForeignKey(name="fk_notification_notification_object_notification_object_id")
)
private NotificationObject notificationObject;
@ManyToOne( fetch=FetchType.LAZY )
@JoinColumn(
name="notifier_id", columnDefinition="INT(10) UNSIGNED", nullable=false,
foreignKey=@ForeignKey(name="fk_notification_user_user_id")
)
private User notifier;
@Column( name="is_seen", nullable=false )
private boolean isSeen;
@Column( name="is_viewed", nullable=false )
private boolean isViewed;
public Notification() {}
public Notification( User notifier, boolean isSeen, boolean isViewed ) {
this.notifier = notifier;
this.isSeen = isSeen;
this.isViewed = isViewed;
}
// Getters and Setters
}
实体4:User.java
@Entity
@Table(name="user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="user_id")
private String user_id;
// Extra fields
@OneToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="emp_id", columnDefinition="INT(10) UNSIGNED")
private Employee employee;
@OneToMany( mappedBy="notifier" )
private Set<Notification> notifications = new LinkedHashSet<>();
public User() {}
public User(String user_id) {
this.user_id = user_id;
}
// Getters and Setters
}
实体5:Employee.java
@Entity
@Table(name="employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
public Employee() { }
public Employee( String emp_id ) {
this.emp_id = emp_id;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="emp_id")
private String emp_id;
@Column(name="first_name")
private String first_name;
@Column(name="last_name")
private String last_name;
// Extra fields
@OneToOne(mappedBy="employee")
@JsonBackReference
private User user;
// Getters and Setters
}
DTO 1:Notify.java
public class Notify {
private Integer notificationObjectId, notificationId, notifierId, actorId, entityId;
private Short entityTypeId;
private String notifierName, actorName, message, notificationLink;
private Date createdOn;
private boolean isSeen, isViewed;
public Notify() {}
public Notify ( Integer notificationObjectId, Integer notificationId, Integer notifierId, Integer actorId,
Integer entityId, Short entityTypeId, String notifierName, String actorName, String message,
String notificationLink, Date createdOn, boolean isSeen, boolean isViewed ) {
// Set Values Here
}
public Notify (Integer notificationObjectId, Integer notificationId, Integer notifierId, String notifierName,
Integer actorId, String actorName, Integer entityId, Short entityTypeId,
Date createdOn, boolean isSeen, boolean isViewed ) {
// Or Here
}
// Getters and Setters
}
我在JOINs很弱。
我想为实体的选定字段编写HQL JOIN查询,以便在Constructor()
DTO的Select子句中形成Notify.java
。
我尝试过的:
查询1
final String GET_NOTIFICATIONS_FOR_USER =
"select new support.dto.Notify ( no.id, n.id, Integer.parseInt( n.notifier.user_id ), "
+ "concat ( n.notifier.employee.first_name, ' ', n.notifier.employee.last_name ), "
+ "Integer.parseInt( nc.actor.user_id ), concat( nc.actor.employee.first_name, ' ', nc.actor.employee.last_name ), "
+ "no.entityId, no.entityTypeId, no.createdOn, n.isSeen, n.isViewed ) "
+ "from Notification n, NotificationObject no, NotificationChange nc, User u, Employee e "
+ "where n.notifier.user_id = :notifierId";
查询2
final String GET_NOTIFICATIONS_FOR_USER =
"select new support.dto.Notify ( no.id, n.id, Integer.parseInt( n.notifier.user_id ), "
+ "concat ( n.notifier.employee.first_name, ' ', n.notifier.employee.first_name ), "
+ "Integer.parseInt( nc.actor.user_id ), concat( nc.actor.employee.first_name, ' ', nc.actor.employee.last_name ), "
+ "no.entityId, no.entityTypeId, no.createdOn, n.isSeen, n.isViewed ) "
+ "from NotificationChange nc inner join nc.notificationObject no "
+ "inner join no.notifications n "
+ "where n.notifier.user_id = :notifierId";
我正在接受以下异常
org.hibernate.internal.util.ReflectHelper.getConstructor(ReflectHelper.java:309)中的org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:174)中的java.lang.NullPointerException。 hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144)位于org.hibernate.hql.internal的org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1091)。 antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2328)org.hibernate.hql.internal.antlr.HqlSqlWalker.selectExprList(HqlSqlBaseWalker.java:2194)org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker。 java:1476)org.hibernate上的org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:573)org.hibernate.hql.internal.antlr.HqlSqlWalker.selectStatement(HqlSqlBaseWalker.java:301) org.hibernate.hql.internal.ast.QueryTranslatorImp中的.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) l.analyze(QueryTranslatorImpl.java:262)org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java: 142)atg.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:115)位于org.hibernate.engine.query的org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:76)。 org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)中的org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)中的.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)。位于support.service.MasterServiceImpl的support.service.MasterServiceImpl.getNotifications(MasterServiceImpl.java:158)的support.DAO.MasterDaoImpl.getNotifications(MasterDaoImpl.java:115)上的hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1821) $$ FastClassBySpringCGLIB $$ a355463b.invoke()at org.springframework.cglib.proxy.MethodProxy.invoke(Meth odProxy.java:204)org.springframework上的org.springframework.aop.frame.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)位于org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)的.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvceptorInterceptor.java:92)org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy) .java:653)atsupport.service.MasterServiceImpl $$ EnhancerBySpringCGLIB $$ 8c2728e2.getNotifications()at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at the support.controller.WebSocketController.hello(WebSocketController.java:91) .reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.in vg(Method.java:601)org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:185)org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:104) org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:447)位于org.springframework.messaging的org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:443) org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler上的org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:408)中的.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:82) org.springframework.messaging.support中的.handleMessage(AbstractMethodMessageHandler.java:346)。 ExecutorSubscribableChannel $ SendTask.run(ExecutorSubscribableChannel.java:135)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)at java .lang.Thread.run(Thread.java:722)
该错误告诉您Hibernate无法找到Notify
构造函数。
此外,您不能在HQL查询中添加Integer.parseInt
。使用ResultSet中的预期类型,并从传入参数在构造函数内部执行转换。
以上是关于如何使用“构造函数”在“选择子句”中为多个表的选定列编写HQL JOIN查询的主要内容,如果未能解决你的问题,请参考以下文章
javascript 如何在JavaScript中为构造函数的所有实例添加属性或方法?
如何在 swig & python 中为没有默认构造函数的 std::pair<> 创建接口?