[英]How to fetch data from two tables in hibernate with spring rest api and return in single object to url
I am using Spring restful api with hibernate. 我正在使用Spring Restful API和Hibernate。 And am fetching data from two tables using two entity classes named Employee and Second.
并且正在使用两个名为Employee和Second的实体类从两个表中获取数据。 I want to get the result in a list from both tables and want to return that in a single json object.
我想从两个表的列表中获取结果,并希望在单个json对象中返回该结果。
Here is my DAO class 这是我的DAO课程
// Method to get the result from employee table
@SuppressWarnings("unchecked")
public List<Employee> getEntityList() throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
List<Employee> employeeList = session.createCriteria(Employee.class)
.list();
tx.commit();
session.close();
return employeeList;
}
// Method to get the result from second table
@SuppressWarnings("unchecked")
public List<Second> getSecondList() throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
List<Second> secondList = session.createCriteria(Second.class)
.list();
tx.commit();
session.close();
return secondList;
}
My service class 我的服务等级
@Autowired
DataDao dataDao;
public List<Employee> getEntityList() throws Exception {
return dataDao.getEntityList();
}
public List<Second> getSecondList() throws Exception {
return dataDao.getSecondList();
}
Here is my RestController 这是我的RestController
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody
List<Employee> getEmployee() {
List<Employee> employeeList = null;
try {
employeeList = dataServices.getEntityList();
} catch (Exception e) {
e.printStackTrace();
}
return employeeList;
}
Here the data is coming from only one table employee but i want to get data from second table too and want to return that data in employeeList. 在这里,数据仅来自一个表的雇员,但我也想从第二个表中获取数据,并希望在employeeList中返回该数据。 W
w ^
What should i do please suggest me. 我该怎么办,建议我。 Thanx in advance
提前感谢
I think you probably need this kind of example 我想你可能需要这种例子
@RestController
public class EmployeeRestController {
@RequestMapping(value = "/employees")
public Wrapper getEmployees() {
Wrapper wrapper = getWrapper();
return wrapper;
}
public Wrapper getWrapper() {
Wrapper wrapper = new Wrapper();
List<Employee> employees = getEmployee();
List<Organizations> organizations = getOrg();
wrapper.setEmployees(employees);
wrapper.setOrganizations(organizations);
return wrapper;
}
public List<Employee> getEmployee() {
Employee employee1 = new Employee(101, "abc", "abc", "SE");
Employee employee2 = new Employee(102, "def", "def", "SE");
Employee employee3 = new Employee(103, "xyz", "xyz", "SE");
List<Employee> employees = new ArrayList<Employee>();
employees.add(employee1);
employees.add(employee2);
employees.add(employee3);
return employees;
}
public List<Organizations> getOrg() {
Organizations organizations1 = new Organizations();
organizations1.setName("Google");
Organizations organizations2 = new Organizations();
organizations2.setName("Facebook");
Organizations organizations3 = new Organizations();
organizations3.setName("Apple");
List<Organizations> organizations = new ArrayList<Organizations>();
organizations.add(organizations1);
organizations.add(organizations2);
organizations.add(organizations3);
return organizations;
}
}
public class Wrapper {
private List<Employee> employees;
private List<Organizations> organizations;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public List<Organizations> getOrganizations() {
return organizations;
}
public void setOrganizations(List<Organizations> organizations) {
this.organizations = organizations;
}
}
Here Organization and Employee are two bean classes which are set into the wrapper classes. 这里的Organization和Employee是设置在包装器类中的两个bean类。
So in your case, you get those two different object from two table and wrap them up in one Wrapper class and send it back. 因此,在您的情况下,您将从两个表中获得这两个不同的对象,并将它们包装在一个Wrapper类中,然后将其发送回去。
I hope this might help you!! 希望对您有帮助!!
@java developer. @java开发人员。
The issue is you cannot collect queries output as a single fetch. 问题是您不能将查询输出作为单个访存收集。 You need to create a separate value object class which map to results from both the queries iteratively.
您需要创建一个单独的值对象类,该对象类以迭代方式映射到两个查询的结果。
Then you need to map this value object class as a return type for spring rest API. 然后,您需要将此值对象类映射为spring rest API的返回类型。 The sample example is shown below: The client class is composing profile and adviser from two different tables.
示例示例如下所示:客户端类是从两个不同的表组成个人档案和顾问。 Also please take note how parseClientSpreadsheet method is setting clientprofiles and clientadvisors.
另外,请注意parseClientSpreadsheet方法如何设置客户端配置文件和客户端顾问。
public class Client extends ReportModel {
private static final long serialVersionUID = 2292996835674522338L;
private ClientProfile profile;
private List<Advisor> advisor;
public ClientProfile getProfile() {
return profile;
}
public void setProfile(ClientProfile profile) {
this.profile = profile;
}
public List<Advisor> getAdvisor() {
return advisor;
}
public void setAdvisor(List<Advisor> advisor) {
this.advisor = advisor;
}
}
@RequestMapping(method = RequestMethod.GET, value = "/list")
public List<Client> getClients() {
List<Client> clients;
// Call the client service and load client data from the database and
// return the list of clients.
try {
clients = clientService.getClients();
} catch (Exception e) {
file_error_logger.error("Exception while reading clients", e);
throw e;
}
if (logger.isDebugEnabled()) logger.debug("Get client details successful.");
return clients;
}
public List<Client> parseClientSpreadsheet(String clientDetailsFilePath,
int numberOfSheets) throws ClientDataNotFoundException {
List<Client> clientList = new ArrayList<Client>();
// if the row is valid, parse the data
if (isValidRow(row, lastColumn,
ClientDataConstants.CLIENT_REQUIRED_COLUMNS)) {
ClientProfile clientProfile;
List<Advisor> advisorList = new ArrayList<Advisor>();
Client client = new Client();
// Set client profile object values
clientProfile = setClientProfileObject(row);
// set Advisor list
advisorList = setAdvisorList(row, lastColumn);
// set Client object
String[] additionalRecipients = row
.getCell(
Integer.parseInt(reportMailerConfigurationService
.getPropertyValue(ClientDataConstants.ADDITIONAL_RECIPIENTS_CELLNUMBER)))
.toString().split(";");
List<String> additionalRecipientsList = new ArrayList<String>();
// max 4 additional recipients are allowed. So if more are
// found, take 4 in array and ignore the rest
for (int i = 0; i < additionalRecipients.length; i++) {
if (!additionalRecipients[i].isEmpty()) {
additionalRecipientsList.add(additionalRecipients[i]);
if (additionalRecipientsList.size() == 4) {
break;
}
}
}
client.setProfile(clientProfile);
client.setAdvisor(advisorList);
client.setadditional_recipients(additionalRecipientsList);
// add the client in the collection
clientList.add(client);
}
}
return clientList;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.