Hibernate不会更新子实体。

huangapple 未分类评论43阅读模式
英文:

Hibernate not Updating the child entity

问题



大家好,希望在封锁期间一切顺利?

我在工作项目中遇到了一个问题,问题是在我从视图更新子实体时,Hibernate不会对其进行更新。

我的父类代码如下:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "permitType", fetch = FetchType.EAGER)
private Set blockDates;
.....
public Set getBlockDates() {return blockDates;}
public void setBlockDates(Set blockDates) {this.blockDates = blockDates;}


我的子类代码如下:

@Entity
@Table(name="block_date")
public class BlockDate implements GenericModel {

@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 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);

英文:

Guys hope you doing well under the lockdown?
I have an issue occur in my work project, the problem is Hibernate won't update my child entity when I update it from the View.

my code parent class has:

@OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;permitType&quot;, fetch = FetchType.EAGER)
private Set&lt;BlockDate&gt; blockDates;
.....
public Set&lt;BlockDate&gt; getBlockDates() {return blockDates;}
public void setBlockDates(Set&lt;BlockDate&gt; blockDates) {this.blockDates = blockDates;}

My child class has :

@Entity
@Table(name=&quot;block_date&quot;)
public class BlockDate implements GenericModel&lt;Long&gt; {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = &quot;block_date&quot;)
    @TableGenerator(name = &quot;block_date&quot;, allocationSize = 30, initialValue = 10000)
    private Long id;

    @Column(name = &quot;effective_date&quot;, nullable = false)
    private DateTime effectiveDate;

    @ManyToOne
    @JoinColumn(name = &quot;permit_type_fk&quot;)
    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;}
}

My DTO code to control the child entity and pull the entry from the view is:


if (getBlockDates() != null ) {
    Set&lt;BlockDate&gt; blockDates = new HashSet&lt;&gt;();
    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(&quot;test dates  &quot; + blockDateString); //this is testing how many entry from user
    }
    permitType.setBlockDates(blockDates);
    System.out.println(&quot;test the new child entity &quot; + 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&lt;&gt;());
          System.out.println(&quot;test null &quot; + permitType.getBlockDates().size()); // this show 0 but in the DB wont updated the child, it still have the old record
    }

and the controller simply have:

permitAreaDao.update(permitArea);

huangapple
  • 本文由 发表于 2020年4月8日 13:40:47
  • 转载请务必保留本文链接:https://java.coder-hub.com/61093992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定