Hibernate:对象内的Date属性未保存到数据库

时间:2018-04-25 21:29:17

标签: hibernate jpa spring-data-jpa

所以我有一个schedule类,其中包含一个ScheduleDateActiveItem列表,它有一个“date”属性。我在Schedule对象中设置列表并保存schedule对象,使用crudrepository保存对象,但里面有“date”属性检索保存的对象时,ScheduleDateActiveItem为null。 使用调试我已经确认我要保存的对象包含date属性,所以当我保存它时会出错。在数据库中,为DateActiveScheduleItem创建一个带有Id的条目,但是date和schedule_id为null < / p>

    for key,value in data:
        print(key, value)

DateActiveScheduleItem

 @Entity
 public class Schedule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@ManyToOne
@JoinColumn(name = "usergroup_id")
private UserGroup userGroup;

private String description;


private boolean master;//is this a schedule for all supervalus(true) or one supervalu(false)

@OneToMany(mappedBy = "schedule",cascade = CascadeType.PERSIST)
private List<DateActiveScheduleItem> dateActiveScheduleItems = new ArrayList<>();

@OneToMany(mappedBy = "schedule",cascade = CascadeType.PERSIST)
private List<MusicScheduleItem> musicScheduleItems = new ArrayList<>();

@OneToMany(mappedBy = "schedule",cascade = CascadeType.PERSIST)
private List<AdvertisementScheduleItem> advertisementScheduleItems = new ArrayList<>();

@Basic
@Temporal(TemporalType.DATE)
private java.util.Date dateAdded;
//getters setters}

1 个答案:

答案 0 :(得分:0)

在日程安排的dateActiveScheduleItems上将级联类型更改为CascadeType.MERGE修复它。