首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将JSON对象赋值给Hibernate Pojo + ext js 4+ Java + spring

如何将JSON对象赋值给Hibernate Pojo + ext js 4+ Java + spring
EN

Stack Overflow用户
提问于 2012-01-02 06:08:47
回答 1查看 1.8K关注 0票数 0

我使用的是JPA、Spring、Extjs4MVC和mysql5.0服务器

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

我将json数据发送给

代码语言:javascript
运行
复制
"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是

代码语言:javascript
运行
复制
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查询触发如下

代码语言:javascript
运行
复制
`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自动递增时,记录将被创建为空名称、startDate、endDate、parentId

所以我的问题是我做错了什么。我的hibernate Pojo映射有什么问题吗?如果是,那么任何解决这个问题的人都可以回复我的线程。

EN

回答 1

Stack Overflow用户

发布于 2012-01-02 06:38:51

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

  1. 您是否尝试过手动创建POJO (没有从JSON转换)并将其存储在DB中?如果它工作,您的映射是可以的,如果不是- POJO在这里不相关,那么修复您的JPA映射。
  2. 假设它有效,问题必须是将您的JSON (毕竟,在服务器上的某个位置将json (string)转换为Java对象并创建您的POJO),可能它无法转换日期,或者您的框架没有正确设置。无论如何,您最喜欢的调试器都应该很容易发现这一点:)

我记得我使用了一个Direct (杜尔)项目将数据从ExtJS发送到基于ExtJS的服务器(数据以JSON格式发送),所以我不能详细说明您的转换方法。

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8697921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档