使用hibernate在Spring中持久保存表数据的Spring启动

时间:2018-06-12 12:26:16

标签: hibernate spring-boot

我创建了一个包含复合键的可嵌入类

@Embeddable
public class TaskCategoryPK  implements Serializable{

@Column(name = "Task_taskId")
private Long taskid;
@Column(name = "Category_categoryId")
private Long categoryid;

public TaskCategoryPK() {}

public TaskCategoryPK(Long taskid,Long categoryid) {
    this.taskid = taskid;
    this.categoryid = categoryid;
}

public void setTaskid(Long taskid) {
    this.taskid = taskid;
}

public void setCategoryid(Long categoryid) {
    this.categoryid = categoryid;
}

public Long getTaskid() {
    return taskid;
}

public Long getCategoryid() {
    return categoryid;
}


}

我创建了映射到此复合键类的实体类

@Entity
@Table(name = "TaskCategoryMapping")
public class TaskCategoryMapping {

@EmbeddedId
private TaskCategoryPK taskcategorypk;

public TaskCategoryPK getTaskCategoryPK() {
    return taskcategorypk;
}

public void setTaskCategoryPK(TaskCategoryPK taskCategoryPK) {
    this.taskcategorypk = taskCategoryPK;
}
}

在控制器中,我试图通过资源在db中保存数据,但是它会抛出空指针异常。控制器处理

TaskCategoryPK taskCategoryPK = new TaskCategoryPK(1L,2L);
        TaskCategoryMapping taskCategoryMapping = new TaskCategoryMapping();
        taskCategoryMapping.setTaskCategoryPK(taskCategoryPK);
        taskCategoryResource.save(taskCategoryMapping);

但我得到以下异常

enter image description here

0 个答案:

没有答案