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

雅科布

我有以下实体关系

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

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

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

   $(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在服务器中接收到零可能是什么原因

我该如何解决该问题?

阿西夫·布托(Asif Bhutto)

请参阅Json格式问题,此问题是本文的续篇

JSON数据转换为Java对象

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将值从服务器传递到客户端的Javascript不起作用

TCP客户端/服务器消息发送不正确

EC 客户端和服务器共享密钥不匹配(可能是由于客户端 pub 密钥在生成服务器 shs 时格式不正确)

无法将客户端消息传递到服务器端

将var从服务器传递到客户端

如何抑制冒泡到客户端的EJBException的服务器端日志记录?

将AD用户从客户端传递到IIS服务器再传递到DB服务器

将数据从客户端传递到服务器端

jhipster客户端将实体过滤器传递到服务器端?

我如何将Java对象从服务器端传递到客户端

客户端数据到服务器端

从客户端将数据传递到服务器后,由于 Java Socket 中的 parseInt 导致“连接被拒绝:连接”错误?

在服务器上记录客户端JavaScript错误

SignalR从客户端到服务器的发送对象未正确反序列化

如何将jwt从swift客户端传递到node.js服务器

NumberFormatException,同时将udp从客户端传递到服务器的数据转换为

使用Flask将JSON数据从服务器传递到客户端

如何在Meteor中将数据从服务器传递到客户端

在RPCGen中将字符指针从客户端传递到服务器

流星将数据从客户端传递到服务器

如何将CSRF令牌从服务器传递到客户端?

将Facebook访问令牌从移动客户端传递到服务器并进行处理

如何安全地将Redux存储从服务器传递到客户端

将数组从NodeJS服务器传递到客户端?

NodeJS - 使用 ajax 将数组从客户端传递到服务器

如何与使用从客户端传递到服务器的参数用于ActionCable的通道保持连接?

Delphi EMS FireDAC:如何使用EMS将参数从客户端传递到服务器?

是否可以将变量从客户端脚本传递到服务器脚本?

如何使用 AJAX 将图像从服务器传递到客户端 Flask