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

只愿长相守 提交于 2019-12-11 23:39:06

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/8697921/how-to-assign-json-object-to-hibernate-pojo-ext-js-4-java-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!