简体   繁体   English

如何将JSON对象分配给Hibernate Pojo + ext js 4 + Java + spring

[英]How to assign JSON Object to Hibernate Pojo + ext js 4 + Java + spring

I am using JPA, Spring, Ext js 4 mvc and mysql5.0 server 我正在使用JPA,Spring,Ext js 4 mvc和mysql5.0服务器

One problem i am facing is that i am not able to insert my data to database using Hibernate + JPA. 我面临的一个问题是我无法使用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. 我使用JSON将数据发送到服务器端,然后此对象与数据库进行交互并将json对象插入数据库。

I am sending the json data as 我正在发送json数据为

"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 我的Hibernate POJO是

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. 当我传递上述JSON数据时,没有任何内容插入到我的数据库中。 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 由于我的班级ID是自动递增的,因此将使用空名称,startDate,endDate,parentId创建记录

So my question is that what the things i am doing wrong. 所以我的问题是我做错了什么。 Is there any problem with my hibernate Pojo mappings . 我的休眠Pojo映射有什么问题吗? 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? 您是否尝试过手动创建POJO(不进行JSON转换)并将其存储在数据库中? If it works, your mappings are OK, if not - the POJO is not relevant here, fix your JPA mappings. 如果可行,您的映射就可以了,否则-POJO与此处无关,请修复您的JPA映射。

  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. 假设它可以正常工作,那么问题就必须是转换JSON(毕竟,它将在服务器上的某个位置将json(字符串)转换为Java Object并创建您的POJO),也许它无法转换日期,或者您的框架未正确设置。 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. 我记得我曾经使用Direct Web Remoting( DWR )项目将数据从ExtJS发送到基于Java的服务器(数据以JSON格式发送),所以我无法真正详细说明您的转换方法。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM