我有以下实体关系
EMP实体
@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
为零的服务器接收的时候?
我怎样才能解决这个问题?