Hibernate join query and JSON related issues

by kpkmd54461 on 2012-03-07 23:08:05

### Problem Analysis

The issue lies in the mismatch between the JSON response format generated by your code and the expected JSON format required by your ExtJS page. Specifically:

1. **Generated JSON Format**:

```json

{

"totalCount": 2,

"results": [

[1, "001", "SAXException", "检查笔录录入 记录", "hesai", {}, "checkRecord.jsp"],

[2, "001", "检查笔录录入记录", "hesai", {}, "checkRecord.jsp"]

]

}

```

The `results` field is an array of arrays, which is not a valid JSON object structure for most frameworks like ExtJS.

2. **Expected JSON Format**:

```json

{

"totalCount": 2,

"results": [

{"id": 1, "bookkind": "001", "remark": "SAXException", "createtime": "..."},

{"id": 2, "bookkind": "001", "remark": "检查笔录录入记录", "createtime": "..."}

]

}

```

Here, `results` should be an array of objects, where each object represents a row from the database query.

3. **Error**: The SQL query might also have issues, as indicated by the `java.sql.SQLException: ORA-00907` error, which typically means a missing parenthesis in the SQL statement.

---

### Solution Approach

#### Step 1: Fix the SQL Query

Ensure the SQL query is syntactically correct. Based on the provided query:

```sql

select a.dbid, a.bookkind, a.remark, a.createman, a.createtime, b.bookurl

from BookList a, BookKind b

where a.bookkind = b.bookkind

```

This query seems fine, but double-check for any missing parentheses or other syntax errors. If the error persists, verify the table structure and column names.

#### Step 2: Convert Query Results to JSON Objects

The current implementation converts the query result into a list of arrays (`List`). To match the expected JSON format, you need to convert each row into a map (or a custom DTO class).

Here’s how you can modify the DAO layer:

```java

@SuppressWarnings("unchecked")

public List> getBookOrgList() {

String queryString = "select a.dbid, a.bookkind, a.remark, a.createman, a.createtime, b.bookurl " +

"from BookList a, BookKind b " +

"where a.bookkind = b.bookkind";

Query query = this.createQuery(queryString);

List results = query.list();

List> bookOrgList = new ArrayList<>();

for (Object[] row : results) {

Map resultMap = new HashMap<>();

resultMap.put("id", row[0]); // dbid

resultMap.put("bookkind", row[1]); // bookkind

resultMap.put("remark", row[2]); // remark

resultMap.put("createman", row[3]); // createman

resultMap.put("createtime", row[4]); // createtime

resultMap.put("bookurl", row[5]); // bookurl

bookOrgList.add(resultMap);

}

return bookOrgList;

}

```

#### Step 3: Modify the Action Layer

In the action layer, ensure the JSON response matches the expected format. Use the modified DAO method to generate the JSON string:

```java

public String list() throws Exception {

try {

List> bookorgList = bookListService.getBookOrgList();

this.setTotalCount(bookorgList.size());

net.sf.json.JsonConfig jsonConfig = new net.sf.json.JsonConfig();

net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(bookorgList);

this.jsonString = "{totalCount:" + this.getTotalCount() + ",results:" + jsonArray.toString() + "}";

Struts2Utils.renderJson(jsonString);

System.out.println("++++====" + jsonString);

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

```

#### Step 4: Verify the Output

After these changes, the output JSON should look like this:

```json

{

"totalCount": 2,

"results": [

{"id": 1, "bookkind": "001", "remark": "SAXException", "createman": "hesai", "createtime": "...", "bookurl": "checkRecord.jsp"},

{"id": 2, "bookkind": "001", "remark": "检查笔录录入记录", "createman": "hesai", "createtime": "...", "bookurl": "checkRecord.jsp"}

]

}

```

This format should now be compatible with your ExtJS page.

---

### Additional Notes

1. **Debugging the SQL Error**: If the `ORA-00907` error persists, carefully review the SQL query for missing parentheses or incorrect syntax.

2. **Performance Considerations**: If the dataset is large, consider paginating the results to improve performance.

3. **Error Handling**: Add proper error handling and logging to identify and resolve issues more effectively.

By following these steps, you should be able to resolve the JSON format mismatch and display the data correctly on your ExtJS page.