2012-01-02 21 views
0

我使用JPA,春,Ext JS的4 MVC和MYSQL5.0服务器如何分配JSON对象到Hibernate POJO的+ Ext JS的4 +的Java +春天

一个我面临的问题是,我不能够使用Hibernate + JPA将我的数据插入数据库。 我使用JSON将数据发送到服务器端,然后此对象与数据库交互并将json对象插入数据库。

我送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 

我的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; 
}  

当我通过上面的JSON数据没有得到插入到我的数据库。 我的Hibernate查询大火像

`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(? , ?, ?, ?, ?, ?, ?, ?, ?, ?)` 

没有得到插入到我的数据库。由于我的班级id是autoincreemented记录创建空名称,startDate,endDate,parentId

所以我的问题是,我做的事情是错的。我的休眠Pojo映射有什么问题吗?如果是,那么有解决这个问题的任何人可能会回复我的线程。

回答

0

我建议你把你的问题分成几个方面。

  1. 您是否尝试过手动创建POJO(无需JSON转换)并将其存储在数据库中? 如果它能正常工作,那么你的映射是可以的,如果没有的话 - POJO在这里不相关,修复你的JPA映射。

  2. 假设它的工作,问题必须转换您的JSON(毕竟,在服务器上它将json(字符串)转换为Java对象并创建您的POJO),也许它不能转换日期,或者你的框架不是正确设置。这种或那种方式,这应该是由 喜爱的调试:)

轻易透露我记得我用直接Web远程(DWR)项目从ExtJS的基于Java的服务器发送数据(数据以JSON格式发送),所以我无法详细说明您的转换方法。

希望这会有所帮助

+0

感谢mark的回复。我已经做了必要的更改,但仍然出现错误'2012年1月2日下午2时9分15秒net.sf.json.JSONObject toBean 警告:试图将属性Name:java.lang.String分配给类com.gantt的bean .model.Task 2012年1月2日下午2:09:15 net.sf.json.JSONObject toBean 警告:试图将属性StartDate:java.lang.String分配给类com.gantt.model.Task的bean Jan 2,2012 2:09:15 PM net.sf.json.JSONObject toBean 警告:试图将属性EndDate:java.lang.String分配给类com.gantt.model.Task的bean?您是否有任何解决方案?? – yaryan997 2012-01-02 08:48:54

+0

嗯,实际上我认为解决方案是写在你的警告中的:JSONObject无法转换你试图发送给POJO的JSON。我想你应该发送类似{your_class_name_that_match_your_pojo_class_name {here_comes_your_json_as_you_described_above}} – 2012-01-02 10:17:22

+0

是否有任何示例,你必须这样做?我是否需要将所有Json对象添加到我的休眠Pojo的对象中,然后将它传递给我的类 – yaryan997 2012-01-02 10:45:09