java - 休眠不更新子实体
问题描述
伙计们希望你们在封锁期间做得很好吗?我的工作项目中出现了一个问题,问题是当我从视图更新它时,Hibernate 不会更新我的子实体。
我的代码父类有:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "permitType", fetch = FetchType.EAGER)
private Set<BlockDate> blockDates;
.....
public Set<BlockDate> getBlockDates() {return blockDates;}
public void setBlockDates(Set<BlockDate> blockDates) {this.blockDates = blockDates;}
我的孩子班有:
@Entity
@Table(name="block_date")
public class BlockDate implements GenericModel<Long> {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "block_date")
@TableGenerator(name = "block_date", allocationSize = 30, initialValue = 10000)
private Long id;
@Column(name = "effective_date", nullable = false)
private DateTime effectiveDate;
@ManyToOne
@JoinColumn(name = "permit_type_fk")
private PermitType permitType;
@Override
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
@JsonSerialize(using = CustomDateTimeSerializer.class)
public DateTime getEffectiveDate() {return effectiveDate;}
public void setEffectiveDate(DateTime effectiveDate) {this.effectiveDate = effectiveDate;}
public PermitType getPermitType() {return permitType;}
public void setPermitType(PermitType permitType) {this.permitType = permitType;}
}
我控制子实体并从视图中提取条目的 DTO 代码是:
if (getBlockDates() != null ) {
Set<BlockDate> blockDates = new HashSet<>();
DateTime effectiveDate;
DateTimeZone timeZone = DateTimeZone.forID(permitType.getPermitArea().getOrganisation().getTimezone());
for (String blockDateString : getBlockDates()){
effectiveDate = DATEPICKER_FORMATTER.parseDateTime(blockDateString).withZone(timeZone).withTimeAtStartOfDay();
blockDates.add(getBlockDateAtt(permitType, effectiveDate));
System.out.println("test dates " + blockDateString); //this is testing how many entry from user
}
permitType.setBlockDates(blockDates);
System.out.println("test the new child entity " + permitType.getBlockDates().size()); //it sets the new record on child entity if i print to consol but it wont applies on the DB
} else{
permitType.setBlockDates(new HashSet<>());
System.out.println("test null " + permitType.getBlockDates().size()); // this show 0 but in the DB wont updated the child, it still have the old record
}
控制器只需:
permitAreaDao.update(permitArea);
解决方案
推荐阅读
- javascript - 经典 ASP:来自 Chrome 中 iframe 的 parent.parent.getelementbyID
- python - 如何在 python 3.7.0 中使用 os 模块移动具有相似名称的文件
- c# - InvokeCommandAction CommandParameter 始终为零
- amazon-web-services - 如何使用不同的 aws s3 预签名 url 使浏览器缓存相同的图像?
- angular - 键入时禁用 matSelect 上的默认搜索
- python-docx - python-docx 获取 docx 信息
- android - 当我添加查看要形成的edittext时如何获取每个edittext值?
- c# - 如何将EditText用户输入存储到c#中的字符串变量中
- mysql - MySQL:创建模式的内存副本
- c++ - openssl 内存泄漏:我还是错误?