JPA : Not able to persist record due to incorrect values being passed from client to server

谁都会走 提交于 2019-12-13 06:23:51

问题


I have the following Entity relationship

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;

Dept Entity

@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;

Jersey REST code

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

The following JSON return from server when I execute getAllEmployees() method

  {"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"}]}

The problem I am facing is when I tried to create a new record, deptNo is 0 even though what is passed from browser is 20.

Code snippet for creating a new record.

   $(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"}',
    })
     });
    });

Data being passed from browser

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

What could be the reason deptNo is zero when being received in server?

How can I resolve the issue?


回答1:


See Json format problem same issue that is continuation of this post

JSON data convert to Java Object



来源:https://stackoverflow.com/questions/20164282/jpa-not-able-to-persist-record-due-to-incorrect-values-being-passed-from-clien

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!