【问题标题】:Hibernate One-to-Many relationship data insertion errorHibernate 一对多关系数据插入错误
【发布时间】:2012-02-20 06:00:07
【问题描述】:

我有项目类 POJO

@Entity
@Table(name = "project", catalog = "primavera")
public class Project implements java.io.Serializable {

    private Integer projectId;
    private Date endDate;
    private String projectDesc;
    private String projectName;
    private String projectTitle;
    private Date startDate;
    private Set<Task> tasks = new HashSet<Task>(0);

    public Project() {
    }

    public Project(Date endDate, String projectDesc, String projectName,
            String projectTitle, Date startDate, Set<Task> tasks) {
        this.endDate = endDate;
        this.projectDesc = projectDesc;
        this.projectName = projectName;
        this.projectTitle = projectTitle;
        this.startDate = startDate;
        this.tasks = tasks;
    }

    @Id
    @GeneratedValue
    @Column(name = "project_id")
    public Integer getProjectId() {
        return this.projectId;
    }

    public void setProjectId(Integer projectId) {
        this.projectId = projectId;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "endDate", length = 19)
    public Date getEndDate() {
        return this.endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    @Column(name = "projectDesc")
    public String getProjectDesc() {
        return this.projectDesc;
    }

    public void setProjectDesc(String projectDesc) {
        this.projectDesc = projectDesc;
    }

    @Column(name = "projectName")
    public String getProjectName() {
        return this.projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    @Column(name = "projectTitle")
    public String getProjectTitle() {
        return this.projectTitle;
    }

    public void setProjectTitle(String projectTitle) {
        this.projectTitle = projectTitle;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "startDate", length = 19)
    public Date getStartDate() {
        return this.startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "project_task", joinColumns = { @JoinColumn(name = "project_id") }, inverseJoinColumns = { @JoinColumn(name = "task_id") })
    public Set<Task> getTasks() {
        return this.tasks;
    }

    public void setTasks(Set<Task> tasks) {
        this.tasks = tasks;
    }

}

与任务类 POJO 有关系

@Entity
@Table(name = "task", catalog = "primavera")
public class Task implements java.io.Serializable {

    private Integer taskId;
    private Integer depth;
    private Double duration;
    private String durationUnit;
    private Date endDate;
    private Integer parentId;
    private Integer percentDone;
    private Integer priority;
    private Date startDate;
    private Integer taskIndex;
    private String taskName;

    public Task() {
    }

    public Task(Integer depth, Double duration, String durationUnit,
            Date endDate, Integer parentId, Integer percentDone,
            Integer priority, Date startDate, Integer taskIndex,
            String taskName) {
        this.depth = depth;
        this.duration = duration;
        this.durationUnit = durationUnit;
        this.endDate = endDate;
        this.parentId = parentId;
        this.percentDone = percentDone;
        this.priority = priority;
        this.startDate = startDate;
        this.taskIndex = taskIndex;
        this.taskName = taskName;
    }

    public Task(String string, Object object) {
        // TODO Auto-generated constructor stub
    }

    @Id
    @GeneratedValue
    @Column(name = "task_id")
    public Integer getTaskId() {
        return this.taskId;
    }

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

    @Column(name = "depth")
    public Integer getDepth() {
        return this.depth;
    }

    public void setDepth(Integer depth) {
        this.depth = depth;
    }

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

    public void setDuration(Double duration) {
        this.duration = duration;
    }

    @Column(name = "durationUnit")
    public String getDurationUnit() {
        return this.durationUnit;
    }

    public void setDurationUnit(String durationUnit) {
        this.durationUnit = durationUnit;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "endDate", length = 19)
    public Date getEndDate() {
        return this.endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    @Column(name = "parentId")
    public Integer getParentId() {
        return this.parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    @Column(name = "percentDone")
    public Integer getPercentDone() {
        return this.percentDone;
    }

    public void setPercentDone(Integer percentDone) {
        this.percentDone = percentDone;
    }

    @Column(name = "priority")
    public Integer getPriority() {
        return this.priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "startDate", length = 19)
    public Date getStartDate() {
        return this.startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

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

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

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

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

}

我正在尝试使用 Json 通过以下代码将数据分配给我的 POJO

Set<Task> taskdata = new HashSet<Task>();
        taskdata.add(new Task(null,null,null,endD1,null,null,null,startD1,null,null));

jsonObject.put("projectTitle", jsonObject.getString("title"));
        jsonObject.put("projectName", jsonObject.getString("name"));
        jsonObject.put("projectDesc", jsonObject.getString("description").replace("\u200b", ""));
        jsonObject.put("startDate", startD);
        jsonObject.put("endDate", endD);
        jsonObject.put("tasks", taskdata);

Project project = (Project) JSONObject.toBean(jsonObject, Project.class);

但它似乎没有工作,即使我将 Set 作为任务数据传递,而且我也收到类似的错误

35802 [http-8085-4] 错误 org.hibernate.property.BasicPropertyAccessor - 类中的 IllegalArgumentException:com.kintu.projectmgt.model.Task,属性的 getter 方法:taskId

请帮我找出导致我的数据无法完全插入数据库的问题。

【问题讨论】:

  • 检查这个答案,可能是一个休眠错误:stackoverflow.com/questions/3631349/…。尝试设置hibernate.cache.use_structured_entries=true
  • 不是解决方案还是同样的错误。但还有一件事是我使用 JSON 将数据分配给与 Task 类有关系的 Project 类的正确方法?还是我必须使用其他一些技术来完全插入数据,它们之间具有一对多的关系。
  • 应该没问题,但是您可以轻松调试它并检查是否在Project创建的Project实例中设置了所有必要的字段@
  • 当我创建项目实例时,我得到了我分配给任务的所有值。意味着它现在是完美的,当休眠以insert into primavera.project (endDate, projectDesc, projectName, projectTitle, startDate) values (?, ?, ?, ?, ?) 执行项目插入查询并在该错误之后再次 33752 [http-8085-3] ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: com.kintu。 projectmgt.model.Task,属性的getter方法:taskId

标签: java json hibernate jakarta-ee


【解决方案1】:

尝试添加类似的策略

@GeneratedValue(strategy=GenerationType.AUTO)

@GeneratedValue(strategy=GenerationType.TABLE)

通常@GeneratedValue在默认模式下用于写入Long。
而且OneToMany 可能期望在另一个模型类中出现ManyToOne

【讨论】:

  • 没有解决方案仍然出现错误。实际上我认为我将值从 JsonObject 分配给 Project 的方式是不正确的。你有什么解决办法吗
猜你喜欢
  • 2018-12-29
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
相关资源
最近更新 更多