Java Hibernate添加额外的sql语句
我正在开发一个restful ws,其方法之一是在数据库中插入一些数据。信息是通过Json传递的,起初我认为可能是Java Hibernate添加额外的sql语句,java,json,rest,Java,Json,Rest,我正在开发一个restful ws,其方法之一是在数据库中插入一些数据。信息是通过Json传递的,起初我认为可能是Json不正确,当我调试应用程序时,服务器端正在正确构建对象,然后我将该对象传递给hibernate saveOrUpdate()。情况就是这样: Hibernate: update CargasEnc set Fecha=?, HojaRutaID=?, CargaTipoID=? where CargaEncID=? Hibernate: update CargasDet set
Json
不正确,当我调试应用程序时,服务器端正在正确构建对象,然后我将该对象传递给hibernate saveOrUpdate()。情况就是这样:
Hibernate: update CargasEnc set Fecha=?, HojaRutaID=?, CargaTipoID=? where CargaEncID=?
Hibernate: update CargasDet set Averiado=?, Cambio=?, Entrega=?, EnvaseID=?, Lleno=?, Retiro=?, Vacio=? where CargaDetID=?
Hibernate: update CargasDet set Averiado=?, Cambio=?, Entrega=?, EnvaseID=?, Lleno=?, Retiro=?, Vacio=? where CargaDetID=?
Hibernate: update CargasDet set Averiado=?, Cambio=?, Entrega=?, EnvaseID=?, Lleno=?, Retiro=?, Vacio=? where CargaDetID=?
Hibernate: update CargasDet set Averiado=?, Cambio=?, Entrega=?, EnvaseID=?, Lleno=?, Retiro=?, Vacio=? where CargaDetID=?
Hibernate: update CargasDet set Averiado=?, Cambio=?, Entrega=?, EnvaseID=?, Lleno=?, Retiro=?, Vacio=? where CargaDetID=?
Hibernate: update CargasDet set Averiado=?, Cambio=?, Entrega=?, EnvaseID=?, Lleno=?, Retiro=?, Vacio=? where CargaDetID=?
Hibernate: update CargasDet set CargaEncID=null where CargaEncID=?
最后一行很奇怪,为什么将cargaEncId设置为null,当我使用log4j检查时,cargaEncId的值为1654
我尝试的另一件事是在服务器上使用测试客户机。我从数据库中获得了相同的对象,更改了一些项,并且saveOrUpdate()
成功了,hibernate除了最后一个sql语句外,执行了与以前相同的sql语句
这是我的两门课:
package com.designfreed.entities;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "CargasEnc")
public class Carga {
@Id
@GeneratedValue
@Column(name = "CargaEncID")
private Long id;
@Column(name = "Fecha")
private Date fecha;
@ManyToOne
@JoinColumn(name = "CargaTipoID")
private CargaTipo tipo;
@Column(name = "HojaRutaID")
private Long hojaRutaId;
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name = "CargaEncID")
private List<ItemCarga> items;
public Carga() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getFecha() {
return fecha;
}
public void setFecha(Date fecha) {
this.fecha = fecha;
}
public CargaTipo getTipo() {
return tipo;
}
public void setTipo(CargaTipo tipo) {
this.tipo = tipo;
}
public Long getHojaRutaId() {
return hojaRutaId;
}
public void setHojaRutaId(Long hojaRutaId) {
this.hojaRutaId = hojaRutaId;
}
public List<ItemCarga> getItems() {
return items;
}
public void setItems(List<ItemCarga> items) {
this.items = items;
}
@Override
public String toString() {
return "Carga [id=" + id + ", fecha=" + fecha + ", tipo=" + tipo + ", hojaRutaId=" + hojaRutaId + ", items="
+ items + "]";
}
}
package com.designfreed.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "CargasDet")
public class ItemCarga {
@Id
@GeneratedValue
@Column(name = "CargaDetID")
private Long id;
@Column(name = "EnvaseID")
private Integer envaseId;
@Column(name = "Lleno")
private Integer lleno;
@Column(name = "Vacio")
private Integer vacio;
@Column(name = "Averiado")
private Integer averiado;
@Column(name = "Retiro")
private Integer retiro;
@Column(name = "Entrega")
private Integer entrega;
@Column(name = "Cambio")
private Integer cambio;
public ItemCarga() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getEnvaseId() {
return envaseId;
}
public void setEnvaseId(Integer envaseId) {
this.envaseId = envaseId;
}
public Integer getLleno() {
return lleno;
}
public void setLleno(Integer lleno) {
this.lleno = lleno;
}
public Integer getVacio() {
return vacio;
}
public void setVacio(Integer vacio) {
this.vacio = vacio;
}
public Integer getAveriado() {
return averiado;
}
public void setAveriado(Integer averiado) {
this.averiado = averiado;
}
public Integer getRetiro() {
return retiro;
}
public void setRetiro(Integer retiro) {
this.retiro = retiro;
}
public Integer getEntrega() {
return entrega;
}
public void setEntrega(Integer entrega) {
this.entrega = entrega;
}
public Integer getCambio() {
return cambio;
}
public void setCambio(Integer cambio) {
this.cambio = cambio;
}
@Override
public String toString() {
return "ItemCarga [id=" + id + ", envaseId=" + envaseId + ", lleno=" + lleno + ", vacio=" + vacio
+ ", averiado=" + averiado + ", retiro=" + retiro + ", entrega=" + entrega + ", cambio=" + cambio + "]";
}
}
package com.designfreed.entities;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入javax.persistence.CascadeType;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.FetchType;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.manytone;
导入javax.persistence.OneToMany;
导入javax.persistence.Table;
@实体
@表(name=“CargasEnc”)
公共级卡加{
@身份证
@生成值
@列(name=“CargaEncID”)
私人长id;
@列(name=“Fecha”)
私人约会费恰;
@许多酮
@JoinColumn(name=“CargaTipoID”)
私人卡加蒂波;
@列(name=“HojaRutaID”)
私人长霍贾鲁塔德;
@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
@JoinColumn(name=“CargaEncID”)
私人清单项目;
公共汽车(){
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公开日期getFecha(){
返回fecha;
}
公共无效setFecha(日期fecha){
this.fecha=fecha;
}
公共汽车公司{
返回提坡;
}
公共无效setTipo(CargaTipo tipo){
this.tipo=tipo;
}
公共长getHojaRutaId(){
返回hojaRutaId;
}
公共无效setHojaRutaId(长hojaRutaId){
this.hojaRutaId=hojaRutaId;
}
公共列表getItems(){
退货项目;
}
公共无效集合项(列表项){
这个项目=项目;
}
@凌驾
公共字符串toString(){
返回“Carga[id=“+id+”,fecha=“+fecha+”,tipo=“+tipo+”,hojaRutaId=“+hojaRutaId+”,items=”
+项目+“]”;
}
}
包com.designfreed.entities;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.Table;
@实体
@表(name=“CargasDet”)
公共类ItemCarga{
@身份证
@生成值
@列(name=“CargaDetID”)
私人长id;
@列(name=“EnvaseID”)
私有整数envaseId;
@列(name=“Lleno”)
私有整数leno;
@列(name=“Vacio”)
私有整数vacio;
@列(name=“Averiado”)
私有整数平均数;
@列(name=“Retiro”)
私有整数retiro;
@列(name=“Entrega”)
私有整数恩特雷加;
@列(name=“Cambio”)
私有整数cambio;
公共项目CARGA(){
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共整数getEnvaseId(){
返回envaseId;
}
public void setEnvaseId(整数envaseId){
this.envaseId=envaseId;
}
公共整数getLleno(){
返回lleno;
}
公共空集合编号(整数集合编号){
this.lleno=lleno;
}
公共整数getVacio(){
返回真空;
}
公共无效setVacio(整数vacio){
this.vacio=vacio;
}
公共整数getAveraido(){
返回阿维利亚多;
}
公共void setAveriado(整数averiado){
this.averiado=averiado;
}
公共整数getRetiro(){
返回retiro;
}
公共void setRetiro(整数retiro){
this.retiro=retiro;
}
公共整数getEntrega(){
返回恩特雷加;
}
公共void setEntrega(整数entrega){
this.entrega=entrega;
}
公共整数getCambio(){
返回坎比奥;
}
公共void setCambio(整数cambio){
this.cambio=cambio;
}
@凌驾
公共字符串toString(){
返回“ItemCarga[id=“+id+”,envaseId=“+envaseId+”,lleno=“+lleno+”,vacio=“+vacio
+“,averiado=“+averiado+”,retiro=“+retiro+”,entrega=“+entrega+”,cambio=“+cambio+””;
}
}
非常感谢你的帮助