GVKun编程网logo

如何在Select子句中使用构造函数为多个表的所选列编写HQL JOIN查询

5

在本文中,我们将为您详细介绍如何在Select子句中使用构造函数为多个表的所选列编写HQLJOIN查询的相关知识,此外,我们还会提供一些关于c#–WMI查询的select子句中的数学运算、mysql中

在本文中,我们将为您详细介绍如何在Select子句中使用构造函数为多个表的所选列编写HQL JOIN查询的相关知识,此外,我们还会提供一些关于c# – WMI查询的select子句中的数学运算、mysql中LEFT JOIN查询两个表的差集、php – select子句中的MYSQL错误1064、postgresql – 在另一个SELECT的WHERE子句中使用SELECT的有用信息。

本文目录一览:

如何在Select子句中使用构造函数为多个表的所选列编写HQL JOIN查询

如何在Select子句中使用构造函数为多个表的所选列编写HQL JOIN查询

我正在使用 在选择子句中*多个表的选定列 编写HQL JOIN查询Constructor() *

我有以下 实体

实体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查询,以形成 DTO的Constructor() “选择子句 ” 。 我尝试过的
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.org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:174)处的org.hibernate.internal.util.ReflectHelper.getConstructor(ReflectHelper.java:309)处的java.lang.NullPointerException位于org.hibernate.hql.internal的hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144)位于org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1091)。在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2194)的antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2328)的org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:573)处的org.hibernate.hql.internal.antlr。org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)的HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(HqlSqlBaseWalker.java:249)
262)在org.hibernate.engine的org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)的org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:76)上的.query.spi.HQLQueryPlan。(HQLQueryPlan.java:115)org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache
.java:150),位于org.hibernate.internal.AbstractSessionImpl处。getHQLQueryPlan(AbstractSessionImpl.java:298)。在org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1821)在support.DAO.MasterDaoImpl.getNotifications(MasterDaoImpl.java:115)上的createQuery(AbstractSessionImpl.java:236)在support.service.MasterServiceImpl.getNotifications(MasterServiceImpls)
.java:158)在support.service.MasterServiceImpl $$ FastClassBySpringCGLIB $$
a355463b.invoke()在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)在org.springframework.aop.framework.CglibAopProxy在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)处的$
CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor。
org.springframework.aop.framework。org.springframework.aop.framework.CglibAopProxy
$
DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)上的ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)在support.service.MasterServiceImpl
$$ EnhancerBySpringCGLIB $
8c2s28e()。在sun.reflect.NativeMethodAccessorImpl.invoke(本机方法)处的controller.WebSocketController.hello(WebSocketController.java:91)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)处的sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethod)
org.springframework.messaging.handler的java.lang.reflect.Method.invoke(Method.java:601)的java:43)(org.springframework.messaging.handler的java.lang.reflect.Method.invoke(Method.java:601)
.invocation。org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.invoke(InvocableHandlerMethod.java:104)在org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:447)
java:443)在org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:408)在org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:408)在org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch
org.springframework.messaging.support的.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:346)。Java的ExecutorSubscribableChannel
$
SendTask.run(ExecutorSubscribableChannel.java:135)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)在java.util.concurrent.ThreadPoolExecutor
$ Worker.run(ThreadPoolExecutor.java:603)在Java
.lang.Thread.run(Thread.java:722)

答案1

小编典典

该错误告诉您Hibernate无法找到Notify 构造函数。

另外,您不允许Integer.parseInt在您的HQL查询中添加。使用ResultSet中的预期类型,并根据传入的参数在构造函数中进行强制转换。

c# – WMI查询的select子句中的数学运算

c# – WMI查询的select子句中的数学运算

在我的控制台应用程序(.NET)中,我正在执行WMI查询,如下所示:

ManagementObjectSearcher query;
        ObjectQuery oq;
        ManagementObjectCollection objectCollection;
        try
        {
            oq = new ObjectQuery("SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem");
            query = new ManagementObjectSearcher(oq);
            objectCollection = query.Get();
        }
        catch
        {
            return null;
        }
        return objectCollection;

我正在对收集的数据进行划分,以便在我的应用程序中使用.目前它对我来说非常合适.
我正在为我的应用程序使用多个wmi查询.为了使一个方法从config执行我所有的wmi查询,我需要在查询的select子句中进行除法.
我需要执行如下WMI查询:

SELECT ((TotalVisibleMemorySize)/1024) as TotalVisibleMemorySize1,((FreePhysicalMemory)/1024) as FreePhysicalMemory1 FROM Win32_OperatingSystem

对于此查询,我收到错误无效的查询.
此查询中是否存在语法错误,或者是否无法在WMi查询的select子句中进行除法?

解决方法

似乎WMI不支持复杂查询.您最好的选择是捕获wmi结果并根据需要进行转换:

foreach (ManagementObject mo in objectCollection)
{
Console.WriteLine("Total Memory = {0} MB",Convert.ToInt32(mo.GetPropertyValue("TotalVisibleMemorySize"))/1024);
}

mysql中LEFT JOIN查询两个表的差集

mysql中LEFT JOIN查询两个表的差集

今天在做一个微信墙的抽奖程序的时候遇到了一个问题,我需要查询量表的差集,业务情形是这样的 一个表用来保存抽奖用户的( 一个人可能会有多条数据),而另一张表保存的是中奖的用户,我需要报么有中奖的所用用户查找出来,刚开始用的是where进行多表关联查询

   今天在做一个微信墙的抽奖程序的时候遇到了一个问题,我需要查询量表的差集,业务情形是这样的

  一个表用来保存抽奖用户的( 一个人可能会有多条数据),而另一张表保存的是中奖的用户,我需要报么有中奖的所用用户查找出来,刚开始用的是where进行多表关联查询,但是最终发现当中奖表中么有数据的时候是查找不到任何信息的,改用LEFT JOIN 用这个就很简单了,

 代码如下  

DESC SELECT * 
FROM  `enet_wall_list` AS l
LEFT JOIN  `enet_wall_lottery` AS lottery ON l.openid = lottery.openid
WHERE l.weid =63
AND lottery.id IS NULL 
GROUP BY l.openid

  左连接查询,查找相等的,如果没有的lottery表中会用NULL字段直接判断下就可以过滤一下数据了,然后得到需要的数据即可 。

  例子

  环境:A/B 两表为关联表。关联字段,pid。A表为主表,数据多于B表。现查询A表中存在,B表中不存在的数据

 代码如下  

SELECT * FROM `A` a LEFT JOIN  `B`  b  ON a.`pid`=b.`pid` where b.`pid` IS NULL and LENGTH(a.`pid`)

  讲述下以上SQL所用到的知识:

  1、LEFT JOIN ON : left join 左边的表为主表,主表中的每条数据都会显示。右边的表中如果没有数据,,则表示为null

  2、LENGTH 计算字符串的长度

php – select子句中的MYSQL错误1064

php – select子句中的MYSQL错误1064

因为我是初学者所以我在这个查询中遇到语法错误.

$sql = "SELECT * FROM registration WHERE email = ".$email." AND password = ".$password."";

解决方法:

请尝试这个,你应该使用单引号.

$sql =“SELECT * FROM registration WHERE email =’”.$email.“’AND password =’”.$password.“’”;

postgresql – 在另一个SELECT的WHERE子句中使用SELECT

postgresql – 在另一个SELECT的WHERE子句中使用SELECT

我已经在Postrgresql的libpq上创建了一个草案远程应用程序.它表现良好,但我已经分析了应用程序的一般功能.对于我生成的每个最终业务结果,我会调用40 select子句(通过tcpip).

我有来自sql-Server的回忆,提醒我最小化远程应用程序和数据库之间的交互次数.分析了我的选择后,我认为我可以使用连接将此数字减少为3个SELECT子句.但我不记得在另一个SELECT中使用SELECT结果的语法.

例如.:

SELECT * FROM individual
INNER JOIN publisher
ON individual.individual_id = publisher.individual_id
WHERE individual.individual_id = 'here I would like to use the results of a another select'

这个其他SELECT只是那种:

SELECT identifier FROM another_table WHERE something='something'

这是简化的表格布局,对于不同的item_types拒绝了很多次…(3种完全不同的类型,因此如果优化了3个SQL查询).

table passage
  id_passage PK
  business_field_passage bytea

table item
  id_item PK
  id_passage FK
  business_field_item text

table item_detail
  id_item_detail PK
  id_item FK
  business_field_item_detail text
  image_content bytea

一个id_passage有几个id_item.
一个id_item有几个id_item_detail.

你会怎么写的?
描述将一个选项重定向到另一个选项(如果有)的操作的名称是什么?

这是你的目标吗?确保要比较的字段具有可比性(即两个字段都是数字,文本,布尔值等).
SELECT * FROM Individual
INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
WHERE Individual.IndividualId = (SELECT someID FROM table WHERE blahblahblah)

如果您希望根据多个值进行选择:

SELECT * FROM Individual
INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
WHERE Individual.IndividualId IN (SELECT someID FROM table WHERE blahblahblah)

关于如何在Select子句中使用构造函数为多个表的所选列编写HQL JOIN查询的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于c# – WMI查询的select子句中的数学运算、mysql中LEFT JOIN查询两个表的差集、php – select子句中的MYSQL错误1064、postgresql – 在另一个SELECT的WHERE子句中使用SELECT的相关信息,请在本站寻找。

本文标签: