【发布时间】:2019-06-12 09:19:42
【问题描述】:
您好,我正在努力让这个查询成功执行。我有这 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 distinct Customer from Customer as cus, Project as pro where cus.customer_code = pro.project_Customer order by cus.customer_Name"
- “从客户中选择不同的 cus.customer_Code、cus.customer_Name as cus, Project as pro where cus.customer_code = pro.project_Customer order by cus.customer_Name"
- “来自客户 cus, Project pro where cus.customer_Code = pro.project_Customer 按 cus.customer_Name 订购”
但是没有任何效果。我通常会收到错误Cannot create TypedQuery for query with multiple return using requested result type [com.timesheet_Webservice.entity.Customer]
这似乎意味着我没有像使用简单的“来自客户”查询那样获取客户实体的实例。如果是这种情况,我如何返回客户实体?如果不是,那我做错了什么?
【问题讨论】:
-
当您执行 Customer.* 时,查询应该选择 customer_Code 和 customer_Name 这两个 String 对象。但是您期望一个客户实体作为结果。
-
那么我如何将结果映射到客户实体?
-
试试
select distinct cus from Customer as cus, Project as pro where cus.customer_code = pro.project_Customer order by cus.customer_Name -
啊,太好了!请写一个答案,以便我接受它