这里有两个实体,一个是Customer,另一个是Order,一个客户可以获得多个Order,最后我使用springmvc在控制器中返回一个Customer对象,但它总是给我sqlgrammer错误,jackson-bind是否可以处理一对多的问题? 这是控制器中的代码片段:
package src;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("/Users/mykong/Documents/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getElementsByTagName("id").item(0).getTextContent());
System.out.println("First Name : " + eElement.getElementsByTagName("UploadURL").item(0).getTextContent());
// System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
// System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
// System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
和错误跟踪
org.springframework.web.util.NestedServletException:Request 处理失败;嵌套异常是 org.hibernate.exception.SQLGrammarException:无法执行 声明
客户实体
@RequestMapping(value = "/getCustomer", method = RequestMethod.GET)
public @ResponseBody Customer getCustomer(){
Customer customer = new Customer();
customer.setRecord("Crabime");
customer.setGender("male");
Order order = new Order("vegitable", 12.1);
Order order1 = new Order("fruit", 3.2);
Set<Order> set = new HashSet<Order>();
set.add(order);
set.add(order1);
customer.setOrders(set);
customerService.save(customer);
return customer;
}
这是订单:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String record;
private String gender;
@OneToMany(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, mappedBy = "customer")
private Set<Order> orders;
public Customer(){}
...omit setter and getter...
**更新:**也许这是tomcat缓存问题,我记得当时重启了服务器而没有任何改变,但在关闭项目几个小时后重新打开它,好吧!
答案 0 :(得分:1)
对于这个错误,建议的方法是添加spring.jpa.show-sql=true
来打印sql语句,然后你可以检查sql。典型的它包含关键字,例如“命令”