HQL如何查询return实体?
HQL query how to return entity?
您好,我正在努力让这个查询成功执行。我有这 2 个实体,并且都有它们适当的 getter 和 setter。
@Entity
@Table(name="customer")
public class Customer {
@Id
@Column(name="Customer_Code")
private String customer_Code;
@Column(name="Customer_Name")
private String customer_Name;
}
@Entity
@Table(name="project")
public class Project {
@Id
@Column(name="Project_Code")
public String project_Code;
@Column(name="Project_Customer")
public String project_Customer;
@Column(name="Project_Description")
public String project_Description;
@Column(name="Project_Pastel_Prefix")
public String project_Pastel_Prefix;
@Column(name="Project_Name")
public String project_Name;
}
十这是我的控制器方法:
// need to inject the session factory
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Customer> getCustomers() {
// get the current hibernate sessio
Session currentSession = sessionFactory.getCurrentSession();
// create a query ... sort by last name
Query<Customer> theQuery =
currentSession.createQuery("Query goes here",
Customer.class);
// execute query and get result list
List<Customer> customers = theQuery.getResultList();
// return the results
return customers;
}
我正在尝试执行此查询“SELECT DISTINCT Customer.* FROM Customer, Project WHERE Customer_Code=Project_Customer ORDER BY Customer_Name "
我试过以下方法:
- "select 将 Customer 与 Customer 区别为 cus,Project 为 pro where
cus.customer_code = pro.project_Customer 按 cus.customer_Name 排序"
- "select 不同于客户 cus.customer_Code、cus.customer_Name
作为客户,项目作为专业人士,其中 cus.customer_code = pro.project_Customer 按 cus.customer_Name 排序
- "来自客户客户,项目专家,其中 cus.customer_Code =
pro.project_Customer 按 cus.customer_Name 排序
但没有任何效果。我通常会收到错误 无法使用请求的结果类型 [com.timesheet_Webservice.entity.Customer]
为具有多个 return 的查询创建类型化查询
这似乎意味着我没有像使用简单的 "from Customer" 查询那样获取客户实体的实例。如果是这样,我如何 return 客户实体?如果不是那我做错了什么?
当您执行 Customer.*
时,查询应该是 select customer_Code
和 customer_Name
这两个 String
对象。但是您期待一个 Customer
实体作为结果。
查询 select 一个像这样的 Customer
对象。
select distinct cus from Customer as cus, Project as pro where cus.customer_code = pro.project_Customer order by cus.customer_Name
您好,我正在努力让这个查询成功执行。我有这 2 个实体,并且都有它们适当的 getter 和 setter。
@Entity
@Table(name="customer")
public class Customer {
@Id
@Column(name="Customer_Code")
private String customer_Code;
@Column(name="Customer_Name")
private String customer_Name;
}
@Entity
@Table(name="project")
public class Project {
@Id
@Column(name="Project_Code")
public String project_Code;
@Column(name="Project_Customer")
public String project_Customer;
@Column(name="Project_Description")
public String project_Description;
@Column(name="Project_Pastel_Prefix")
public String project_Pastel_Prefix;
@Column(name="Project_Name")
public String project_Name;
}
十这是我的控制器方法:
// need to inject the session factory
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Customer> getCustomers() {
// get the current hibernate sessio
Session currentSession = sessionFactory.getCurrentSession();
// create a query ... sort by last name
Query<Customer> theQuery =
currentSession.createQuery("Query goes here",
Customer.class);
// execute query and get result list
List<Customer> customers = theQuery.getResultList();
// return the results
return customers;
}
我正在尝试执行此查询“SELECT DISTINCT Customer.* FROM Customer, Project WHERE Customer_Code=Project_Customer ORDER BY Customer_Name "
我试过以下方法:
- "select 将 Customer 与 Customer 区别为 cus,Project 为 pro where cus.customer_code = pro.project_Customer 按 cus.customer_Name 排序"
- "select 不同于客户 cus.customer_Code、cus.customer_Name 作为客户,项目作为专业人士,其中 cus.customer_code = pro.project_Customer 按 cus.customer_Name 排序
- "来自客户客户,项目专家,其中 cus.customer_Code = pro.project_Customer 按 cus.customer_Name 排序
但没有任何效果。我通常会收到错误 无法使用请求的结果类型 [com.timesheet_Webservice.entity.Customer]
为具有多个 return 的查询创建类型化查询这似乎意味着我没有像使用简单的 "from Customer" 查询那样获取客户实体的实例。如果是这样,我如何 return 客户实体?如果不是那我做错了什么?
当您执行 Customer.*
时,查询应该是 select customer_Code
和 customer_Name
这两个 String
对象。但是您期待一个 Customer
实体作为结果。
查询 select 一个像这样的 Customer
对象。
select distinct cus from Customer as cus, Project as pro where cus.customer_code = pro.project_Customer order by cus.customer_Name