开发者社区> 问答> 正文

如何在子实体中使用复合ID配置ManyToAny关系

让我们从我的实际代码开始:

包信息.java

@AnyMetaDef(name = "EmployeeDowntimeEntityDef",
            idType = "binary",
            metaType = "string",
            metaValues = {@MetaValue(targetEntity = AbsenceEntity.class, value = "absence")})
package com.example.webservice.modules.absence.dal.entity;

import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.MetaValue;

EmployeeDowntimeEntity.java

@Entity
@Table(indexes = {@Index(name = "employeeDowntime_happensOn_idx", columnList = "happens_on")})
@IdClass(EmployeeDowntimeEntityId.class)
public class EmployeeDowntimeEntity {

  @Id
  @Column(name = "reference_id")
  private UUID referenceId;

  @Id
  @Column(name = "happens_on")
  private LocalDate happensOn;

  @NotNull
  @Min(1/100)
  @Max(24)
  private float amountOfHours;

  private EmployeeDowntimeEntity() {}

  EmployeeDowntimeEntity(LocalDate happensOn, Float value, UUID referenceId) {
    this.happensOn = happensOn;
    this.amountOfHours = value;
    this.referenceId = referenceId;
  }

  public LocalDate getHappensOn() {
    return happensOn;
  }

  public float getAmountOfHours() {
    return amountOfHours;
  }

  public UUID getReferenceId() {
    return referenceId;
  }

  public static class EmployeeDowntimeEntityId implements Serializable {
    private LocalDate happensOn;
    private UUID referenceId;

    private EmployeeDowntimeEntityId() {}
  }
}

AbsenceEntity.java

@Entity
@Table
public class AbsenceEntity {

  @Id
//  @GeneratedValue(generator = "UUID")
//  @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
  @Column(name = "absence_id")
  private UUID id;

  @ManyToOne
  @JoinColumn(name = "reason_id", nullable = false)
  private ReasonEntity reason;

  @Size(min = 1)
  @ManyToAny(fetch = FetchType.EAGER,
             metaColumn = @Column(name = "employee_downtime_type"),
             metaDef = "EmployeeDowntimeEntityDef")
  @Cascade({CascadeType.ALL})
//  @JoinColumns({@JoinColumn(name = "reference_id"), @JoinColumn(name = "happens_on")})
  @JoinTable(name = "absence_downtimes",
             joinColumns = @JoinColumn(name = "absence_id"),
             inverseJoinColumns = {@JoinColumn(name = "reference_id"), @JoinColumn(name = "happens_on")})
  private Set<EmployeeDowntimeEntity> downtimes;

  // Required by Hibernate
  private AbsenceEntity() {
  }

  public AbsenceEntity(Absence absence) {
    this.id = absence.getId().equals(IdentifiableObject.createTemporaryId()) ? UUID.randomUUID(): absence.getId();
      this.downtimes = absence.getHours().entrySet().stream().map((entry) -> new EmployeeDowntimeEntity(entry.getKey(),
                                                                                           entry.getValue(),
                                                                                           this.id)).collect(toSet());
  }

  public UUID getId() {
    return id;
  }

  public Set<EmployeeDowntimeEntity> getDowntimes() {
    return downtimes;
  }
}

保存新的AbsenceEntities可以按预期工作,但是在访问停机时间属性时将其读回会抛出LazyInitializationException。

AbsenceEntity absence = new AbsenceEntityBuilder(TEMPORARY_ID,
                                                 Map.of(LocalDate.parse("1998-02-01"),
                                                        10f,
                                                        LocalDate.parse("1998-02-02"),
                                                        8f)).build();
absenceEntityDao.save(absence);
int i = absenceEntityDao.findAll().stream().findAny().get().getDowntimes().size();

随着全栈是: ```js org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.webservice.modules.absence.dal.entity.AbsenceEntity.downtimes, could not initialize proxy - no Session


我的目标是必须```js
EmployeeDowntimeEntity.referenceId
等```  
于我的```js
AbsenceEntity
身```  
份证。将```js
ManyToAny
被```  
要求作为```js
EmployeeDowntimeEntity
将```  
引用其它类型的比```js
AbsenceEntity
。```  


当我已经在```js
ManyToAny
声```  
明中将获取类型设置为EAGER时,我想问题出在我的```js
AnyMetaDef
声```  
明中,可能带有```js
idType
我```  
可以弄清楚该放在哪里的值。

我尝试使用a ```js
JoinTable
或```  
a进行操作,```js
JoinColumns
但```  
在此简单示例中,两者的行为相同。

我想念什么?

奖励:您可能已经注意到,我```js
AbsenceEntity.id
在```  
创建新的缺勤时评论了有关生成新的UUID ID的注释,因为我无法按预期使用生成的值```js
EmployeeDowntimeEntity.referenceId
。```  
我发现最简单的解决方案是自己生成它,但在这种情况下,我不希望这样做。

展开
收起
几许相思几点泪 2019-12-05 15:47:40 799 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
继承与功能组合 立即下载
重新定义计算的边界 立即下载
低代码开发师(初级)实战教程 立即下载