让我们从我的实际代码开始:
包信息.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
。```
我发现最简单的解决方案是自己生成它,但在这种情况下,我不希望这样做。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。