JPA:由于从客户端传递到服务器的值不正确,无法保留记录

时间:2013-11-23 15:42:30

标签: java hibernate rest jpa jersey

我有以下实体关系

Emp Entity

@Entity
@Table(name = "EMP", schema = "SCOTT"
)
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Emp.findAllEmployees", query = "select e from Emp e left 
    join fetch e.deptNo order by e.empno desc")
})
public class Emp implements java.io.Serializable {
@Id
@Column(name = "EMPNO", unique = true, nullable = false, precision = 4,
scale = 0)
private short empno;
@ManyToOne
@JoinColumn(name = "DEPTNO", referencedColumnName = "DEPTNO")
private Dept deptNo;

部门实体

@Entity
@Table(name = "DEPT", schema = "SCOTT"
)
@XmlRootElement
public class Dept implements java.io.Serializable {
@Id
@Column(name = "DEPTNO", unique = true, nullable = false, precision = 2,
scale = 0)
private short deptNo;
@OneToMany(fetch=FetchType.LAZY,mappedBy = "deptNo")
@XmlTransient
private Set<Emp> empDeptno;

泽西岛REST代码

@POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public void create(Emp emp) {
         log.info("deptNo "+emp.getDeptNo().getDeptNo());
         getEmployeeService().create(emp);
    }

执行getAllEmployees()方法

时,从服务器返回以下JSON
  {"emp":[{"dept":{"deptNo":"10","dname":"ACCOUNTING","loc": 
  "NEW YORK"},"empno":"7934","ename":"MILLER","hiredate":
  "1982-01-23T00:00:00+03:00","job":"CLERK","mgr":"7782","sal":"1300"},
  {"dept":
  {"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7902","ename":
  "FORD",
  "hiredate":"1981-12-03T00:00:00+03:00","job":"ANALYST","mgr":"7566","sal":
  "3000"},{"dept": 
  {"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7900","ename":
  "JAMES", "hiredate":
  "1981-12-03T00:00:00+03:00","job":"CLERK","mgr":"7698","sal":"950"},
  {"dept":{"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7876",
  "ename":
  "ADAMS","hiredate":
  "1987-05-23T00:00:00+03:00","job":"CLERK","mgr":"7788","sal":"1100"},
  {"comm":"0","dept":      
  {"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7844","ename":
  "TURNER","hiredate":
  "1981-09-08T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1500"},
  {"dept":{"deptNo":"10","dname":"ACCOUNTING","loc":
  "NEW YORK"},"empno":"7839","ename":"KING","hiredate":
  "1981-11-17T00:00:00+03:00","job":"PRESIDENT","sal":"5000"},         
  {"dept":"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7788",
  "ename":"SCOTT","hiredate":
  "1987-04-19T00:00:00+03:00","job":"ANALYST","mgr":"7566","sal":"3000"},
   {"dept":{"deptNo":"10","dname":"ACCOUNTING","loc":
   "NEW YORK"},"empno":"7782","ename":"CLARK","hiredate":
   "1981-06-09T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2450"},
   {"dept":"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7698",
   "ename":
    "BLAKE","hiredate":
   "1981-05-01T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2850"},
   {"comm":"1400","dept": 
   {"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7654","ename":
    "MARTIN","hiredate":
   "1981-09-28T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1250"},
   {"dept":
   {"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7566","ename": 
    "JONES","hiredate":
   "1981-04-02T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2975"},
   {"comm":"500","dept": 
   {"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7521","ename":
   "WARD", "hiredate":
   "1981-02-22T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1250"},
    {"comm":"300","dept":
    {"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7499",
   "ename":
   "ALLEN","hiredate":"1981-02-20T00:00:00+03:00","job":"SALESMAN","mgr":
   "7698",
   "sal":"1600"},{"dept":
   {"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7369","ename":
    "SMITH","hiredate":
   "1980-12-17T00:00:00+03:00","job":"CLERK","mgr":"7902","sal":"800"}]}

我面临的问题是当我尝试创建新记录时,deptNo为0,即使从浏览器传递的内容是20。

用于创建新记录的代码段。

   $(document).ready(function () {
    $("#btnSubmit").click(function () {
    $.ajax({
        url: "http://localhost:8181/Test1/rest/employee/",
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: '{"empno":"9004",
     "ename":"JILL","job":"CS","mgr":"1111","sal":"800","comm":"202","deptNo":"20"}',
    })
     });
    });

从浏览器传递数据

{"empno":"9004",
"ename":"JILL","job":"CS","mgr":"1111","sal":"800","comm":"202","deptNo":"20"}

在服务器中收到deptNo为零时可能是什么原因?

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

请参阅Json格式问题同一问题,即此帖的延续

JSON data convert to Java Object