如何将JSON对象分配给Hibernate Pojo + ext js 4 + Java + spring [英] How to assign JSON Object to Hibernate Pojo + ext js 4 + Java + spring

查看:155
本文介绍了如何将JSON对象分配给Hibernate Pojo + ext js 4 + Java + spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPA,Spring,Ext js 4 mvc和mysql5.0服务器



我遇到的一个问题是我无法将我的数据插入数据库使用Hibernate + JPA。
我使用JSON将数据发送到服务器端,然后这个对象与数据库交互并将json对象插入数据库。



我发送json数据为

 Id:null,
名称:新任务,
StartDate:2010-02-13T05:30:00,
EndDate:2010-02-13T05:30:00,
持续时间:0,
持续时间:d,
PercentDone:0,
ManuallyScheduled:false,
优先级:1,
parentId:22,
index:2,
depth:2,
checked:null

我的Hibernate POJO是

  private int id; 
private Date startDate;
private Date endDate;
private int percentDone;
private String name;
private int priority;
私人双持续时间;
private String durationUnit;
private int index;
private int depth;
private int parentId;

/ **
* @返回id
* /
@Id
@GeneratedValue
@Column(name =TASK_Id )
public int getId(){
return id;
}
/ **
* @param id要设置的
* /
public void setId(int Id){
this.id = ID;
}

/ **
* @返回startDate
* /
@Temporal(TemporalType.DATE)
@Column(name =TASK_Start_Date)
public Date getStartDate(){
return startDate;
}
/ **
* @param startDate startDate设置
* /
public void setStartDate(Date StartDate){
this.startDate =开始日期;
}

/ **
* @返回endDate
* /
@Temporal(TemporalType.DATE)
@Column(name =TASK_End_Date)
public Date getEndDate(){
return endDate;
}
/ **
* @param endDate endDate设置
* /
public void setEndDate(Date EndDate){
this.endDate =结束日期;
}

/ **
* @返回percentDone
* /
@Column(name =TASK_Percent_Done)
public int getPercentDone(){
return percentDone;
}
/ **
* @param percentDone percentDone设置
* /
public void setPercentDone(int PercentDone){
this.percentDone = PercentDone;
}

/ **
* @返回名称
* /
@Column(name =TASK_Name)
public String getName(){
return name;
}
/ **
* @param名称设置名称
* /
public void setName(String Name){
this.name =名称;
}

/ **
* @返回优先级
* /
@Column(name =TASK_Priority)
public int getPriority(){
返回优先级;
}
/ **
* @param优先级设置
* /
public void setPriority(int Priority){
this.priority =优先;
}

/ **
* @返回持续时间
* /
@Column(name =TASK_Duration)
public double getDuration(){
return duration;
}
/ **
* @param持续时间设置
* /
public void setDuration(double Duration){
this.duration =持续时间;
}

/ **
* @返回durationUnit
* /
@Column(name =TASK_DurationUnit)
public String getDurationUnit(){
return durationUnit;
}
/ **
* @param durationUnit要设置的持续时间单位
* /
public void setDurationUnit(String DurationUnit){
this.durationUnit = DurationUnit;
}

/ **
* @返回索引
* /
@Column(name =TASK_Index)
public int getIndex(){
return index;
}
/ **
* @param索引设置
* /
public void setIndex(int index){
this.index =指数;
}

/ **
* @返回深度
* /
@Column(name =TASK_Depth)
public int getDepth(){
return depth;
}
/ **
* @param深度设置
* /
public void setDepth(int depth){
this.depth =深度;
}

/ **
* @返回parentId
* /
@Column(name =TASK_ParentId)
public int getParentId(){
return parentId;
}
/ **
* @param parentId要设置的parentId
* /
public void setParentId(int parentId){
this.parentId = parentId的;
}

当我传递上述JSON数据时,没有任何东西被插入到我的数据库。
我的hibernate查询触发像

 `Hibernate:插入TASK(TASK_Depth,TASK_Duration,TASK_DurationUnit,TASK_End_Date,TASK_Index, TASK_Name,TASK_ParentId,TASK_Percent_Done,TASK_Priority,TASK_Start_Date)
值(?,?,?,?,?,?)?
pre>

没有任何东西插入到我的数据库。由于我的类id是自动认证的,所以创建的记录为空名,startDate,endDate,parentId



所以我的问题是我做错了什么事情。我的hibernate Pojo映射有什么问题吗?如果是的话,任何一个解决这个问题的人都可以回复我的帖子。

解决方案

我建议你把你的问题分成您是否尝试手动创建POJO(不从JSON转换)并将其存储在数据库中?
如果它有效,你的映射是可以的,如果没有,POJO在这里是不相关的,修复你的JPA映射。


  • 假设它有效,问题必须是转换您的JSON(毕竟,服务器上某处将json(string)转换为Java Object并创建您的POJO),也许它无法转换日期,或者您的框架未正确设置。一种或多种方法应该是您最喜欢的调试器容易显示的
    :)


  • 我记得我' ve使用直接Web远程处理( DWR )项目将数据从ExtJS发送到基于Java的服务器(数据以JSON格式发送),所以我无法真正阐述您的转换方法。



    希望这有助于


    I am using JPA, Spring, Ext js 4 mvc and mysql5.0 server

    One problem i am facing is that i am not able to insert my data to database using Hibernate + JPA. I am sending the data to server side using JSON and then this object interact with database and insert the json object to the database.

    I am sending the json data as

    "Id": null,
    "Name": "New task",
    "StartDate": "2010-02-13T05:30:00",
    "EndDate": "2010-02-13T05:30:00",
    "Duration": 0,
    "DurationUnit": "d",
    "PercentDone": 0,
    "ManuallyScheduled": false,
    "Priority": 1,
    "parentId": 22,
    "index": 2,
    "depth": 2,
    "checked": null
    

    my Hibernate POJO is

    private int id;
    private Date startDate;
    private Date endDate;
    private int percentDone;
    private String name;
    private int priority;
    private double duration;
    private String durationUnit;
    private int index;
    private int depth;
    private int parentId;
    
    /**
     * @return the id
     */
    @Id
    @GeneratedValue
    @Column(name = "TASK_Id")
    public int getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(int Id) {
        this.id = Id;
    }
    
    /**
     * @return the startDate
     */
    @Temporal(TemporalType.DATE)
    @Column(name = "TASK_Start_Date")
    public Date getStartDate() {
        return startDate;
    }
    /**
     * @param startDate the startDate to set
     */
    public void setStartDate(Date StartDate) {
        this.startDate = StartDate;
    }
    
    /**
     * @return the endDate
     */    
    @Temporal(TemporalType.DATE)
    @Column(name = "TASK_End_Date")
    public Date getEndDate() {
        return endDate;
    }
    /**
     * @param endDate the endDate to set
     */
    public void setEndDate(Date EndDate) {
        this.endDate = EndDate;
    }
    
    /**
     * @return the percentDone
     */    
    @Column(name = "TASK_Percent_Done")
    public int getPercentDone() {
        return percentDone;
    }
    /**
     * @param percentDone the percentDone to set
     */
    public void setPercentDone(int PercentDone) {
        this.percentDone = PercentDone;
    }
    
    /**
     * @return the name
     */
    @Column(name = "TASK_Name")
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String Name) {
        this.name = Name;
    }
    
    /**
     * @return the priority
     */
    @Column(name = "TASK_Priority")
    public int getPriority() {
        return priority;
    }
    /**
     * @param priority the priority to set
     */
    public void setPriority(int Priority) {
        this.priority = Priority;
    }
    
    /**
     * @return the duration
     */
    @Column(name = "TASK_Duration")
    public double getDuration() {
        return duration;
    }
    /**
     * @param duration the duration to set
     */
    public void setDuration(double Duration) {
        this.duration = Duration;
    }
    
    /**
     * @return the durationUnit
     */
    @Column(name = "TASK_DurationUnit")
    public String getDurationUnit() {
        return durationUnit;
    }
    /**
     * @param durationUnit the durationUnit to set
     */
    public void setDurationUnit(String DurationUnit) {
        this.durationUnit = DurationUnit;
    }
    
    /**
     * @return the index
     */
    @Column(name = "TASK_Index")
    public int getIndex() {
        return index;
    }
    /**
     * @param index the index to set
     */
    public void setIndex(int index) {
        this.index = index;
    }
    
    /**
     * @return the depth
     */
    @Column(name = "TASK_Depth")
    public int getDepth() {
        return depth;
    }
    /**
     * @param depth the depth to set
     */
    public void setDepth(int depth) {
        this.depth = depth;
    }
    
    /**
     * @return the parentId
     */
    @Column(name = "TASK_ParentId")
    public int getParentId() {
        return parentId;
    }
    /**
     * @param parentId the parentId to set
     */
    public void setParentId(int parentId) {
        this.parentId = parentId;
    }    
    

    when i am passing the above JSON data nothing get inserted to my database. my hibernate query fires like

    `Hibernate: insert into TASK(TASK_Depth, TASK_Duration, TASK_DurationUnit, TASK_End_Date, TASK_Index, TASK_Name, TASK_ParentId, TASK_Percent_Done, TASK_Priority, TASK_Start_Date)
    values( ? , ?, ?, ?, ?, ?, ?, ?, ?, ?)`
    

    nothing get inserted to my database. As my class id is autoincreemented the record gets created with empty name, startDate, endDate, parentId

    So my question is that what the things i am doing wrong. Is there any problem with my hibernate Pojo mappings . If yes then any one having solution to this problem may reply to my thread.

    解决方案

    I would suggest you to divide your issue into several areas.

    1. Have you tried to create a POJO manually (without conversion from JSON) and store it in your DB? If it works, your mappings are OK, if not - the POJO is not relevant here, fix your JPA mappings.

    2. Assuming it worked, the issue must be converting your JSON (after all, somewhere on server it converts json (string) to Java Object and creates your POJO), maybe it fails to convert dates, or your framework is not set up properly. One way or another this should be easily revealed by your favorite debugger :)

    I remember I've used a Direct Web Remoting (DWR) project to send data from ExtJS to the Java based server (the data was sent in a JSON format) so I can't really elaborate on your conversion method.

    Hope this helps

    这篇关于如何将JSON对象分配给Hibernate Pojo + ext js 4 + Java + spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆