无法使用Hibernate模板将数据插入到mysql服务器

时间:2012-01-02 09:00:27

标签: java hibernate spring jpa

stackoverflow论坛会员我需要你的帮助。我无法将我的JPA POJO对象插入我的数据库。

我的JPA POJO

private Integer taskId;
private Integer taskDepth;
private Double taskDuration;
private String taskDurationUnit;
private Date taskEndDate;
private Integer taskIndex;
private String taskName;
private Integer taskParentId;
private Integer taskPercentDone;
private Integer taskPriority;
private Date taskStartDate;

public Task() {
}

public Task(Integer taskDepth, Double taskDuration,
        String taskDurationUnit, Date taskEndDate, Integer taskIndex,
        String taskName, Integer taskParentId, Integer taskPercentDone,
        Integer taskPriority, Date taskStartDate) {
    this.taskDepth = taskDepth;
    this.taskDuration = taskDuration;
    this.taskDurationUnit = taskDurationUnit;
    this.taskEndDate = taskEndDate;
    this.taskIndex = taskIndex;
    this.taskName = taskName;
    this.taskParentId = taskParentId;
    this.taskPercentDone = taskPercentDone;
    this.taskPriority = taskPriority;
    this.taskStartDate = taskStartDate;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "TASK_Id", unique = true, nullable = false)
public Integer getTaskId() {
    return this.taskId;
}

public void setTaskId(Integer taskId) {
    this.taskId = taskId;
}

@Column(name = "TASK_Depth")
public Integer getTaskDepth() {
    return this.taskDepth;
}

public void setTaskDepth(Integer taskDepth) {
    this.taskDepth = taskDepth;
}

@Column(name = "TASK_Duration", precision = 22, scale = 0)
public Double getTaskDuration() {
    return this.taskDuration;
}

public void setTaskDuration(Double taskDuration) {
    this.taskDuration = taskDuration;
}

@Column(name = "TASK_DurationUnit")
public String getTaskDurationUnit() {
    return this.taskDurationUnit;
}

public void setTaskDurationUnit(String taskDurationUnit) {
    this.taskDurationUnit = taskDurationUnit;
}

@Temporal(TemporalType.DATE)
@Column(name = "TASK_End_Date", length = 10)
public Date getTaskEndDate() {
    return this.taskEndDate;
}

public void setTaskEndDate(Date taskEndDate) {
    this.taskEndDate = taskEndDate;
}

@Column(name = "TASK_Index")
public Integer getTaskIndex() {
    return this.taskIndex;
}

public void setTaskIndex(Integer taskIndex) {
    this.taskIndex = taskIndex;
}

@Column(name = "TASK_Name")
public String getTaskName() {
    return this.taskName;
}

public void setTaskName(String taskName) {
    this.taskName = taskName;
}

@Column(name = "TASK_ParentId")
public Integer getTaskParentId() {
    return this.taskParentId;
}

public void setTaskParentId(Integer taskParentId) {
    this.taskParentId = taskParentId;
}

@Column(name = "TASK_Percent_Done")
public Integer getTaskPercentDone() {
    return this.taskPercentDone;
}

public void setTaskPercentDone(Integer taskPercentDone) {
    this.taskPercentDone = taskPercentDone;
}

@Column(name = "TASK_Priority")
public Integer getTaskPriority() {
    return this.taskPriority;
}

public void setTaskPriority(Integer taskPriority) {
    this.taskPriority = taskPriority;
}

@Temporal(TemporalType.DATE)
@Column(name = "TASK_Start_Date", length = 10)
public Date getTaskStartDate() {
    return this.taskStartDate;
}

public void setTaskStartDate(Date taskStartDate) {
    this.taskStartDate = taskStartDate;
}

Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property Name:java.lang.String to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property StartDate:java.lang.String to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property EndDate:java.lang.String to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property Duration:java.lang.Integer to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property DurationUnit:java.lang.String to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property PercentDone:java.lang.Integer to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property Priority:java.lang.Integer to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property parentId:java.lang.Integer to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property index:java.lang.Integer to bean of class com.gantt.model.Task Jan 2, 2012 2:09:15 PM net.sf.json.JSONObject toBean WARNING: Tried to assign property depth:java.lang.Integer to bean of class com.gantt.model.Task

我使用Hibernate Reverse Engineering生成了我的JPA POJO。无法理解我的JPA POJO对象有什么问题。

我传递的JSON对象是 { "Name": "Final New task", "StartDate": "2012-01-02T00:00:00", "EndDate": "2012-01-06T00:00:00", "Duration": 4, "DurationUnit": "d", "PercentDone": 5, "Priority": 1, "parentId": 3, "index": 1, "depth": 3 }

仍然无法将我的json对象映射到JPA POJO对象,并且总是抛出上面的错误。 是否存在JSONObject数据类型和JPA POJO Object数据类型的问题。

任何人都有解决这个问题的方法,请帮我解决。

我正在使用Hibernate JPA与我的数据库进行交互。

Yogendra Singh 高级程序员 Kintudesigns.com

3 个答案:

答案 0 :(得分:0)

POJO代码不完整,缺少类标题和@Entity注释。可能它粘贴得很糟糕。

答案 1 :(得分:0)

您确定表格字段的名称是“TASK_Name”,“TASK_Start_Date”,“TASK_End_Date”等吗?他们不只是被称为“名称”,“Start_Date”,“End_Date”?如果是这样,那么注释@Column的属性名称将是错误的。

答案 2 :(得分:0)

实际上,当您使用此库时,看到的警告消息不是问题

将JSON解析为Java Objects但它们不是错误。现在,为什么对象没有存储到

数据库是Hibernate的一个问题。您正在使用的实体类没有问题。

查看配置文件。如果存在休眠问题,那么必须有一些错误的输出日志。