I am getting this error in calculating a value before saving the entity.
“IllegalStateException: Cannot get unfetched attribute [targetType] from detached object com.company.sheeloffice.entity.AAQMSParameter-2dbbd626-c2e7-4db1-c785-12c0dd691340 [detached].”
Entity AAQMSData has association to AQMSParameter. I am calculating ‘compliace’ attribute in AAQMSData.edit which is based on AAQMSParameer attibutes.
The error is being faced only when creating AAQMSData and not when editing AAQMSData.
The entity definition for AAQMSData is
package com.company.sheeloffice.entity; import com.haulmont.cuba.core.entity.StandardEntity; import com.haulmont.cuba.core.entity.annotation.Lookup; import com.haulmont.cuba.core.entity.annotation.LookupType; import javax.persistence.*; import javax.validation.constraints.NotNull; import java.util.Date; @Table(name = "SHEELOFFICE_AAQMS_DATA") @Entity(name = "sheeloffice_AAQMSData") public class AAQMSData extends StandardEntity { @Temporal(TemporalType.DATE) @NotNull @Column(name = "DATE_", nullable = false) protected Date date; @Lookup(type = LookupType.DROPDOWN, actions = {"lookup", "clear"}) @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "AAQMS_ID") protected AAQMS aaqms; @Lookup(type = LookupType.DROPDOWN, actions = {"lookup", "clear"}) @NotNull @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "PARAMETER_ID") protected AAQMSParameter parameter; @NotNull @Column(name = "VALUE_", nullable = false) protected Double value; @Column(name = "COMPLIANCE", length = 5) protected String compliance; public AAQMS getAaqms() { return aaqms; } public void setAaqms(AAQMS aaqms) { this.aaqms = aaqms; } public void setValue(Double value) { this.value = value; } public Double getValue() { return value; } public String getCompliance() { return compliance; } public void setCompliance(String compliance) { this.compliance = compliance; } public AAQMSParameter getParameter() { return parameter; } public void setParameter(AAQMSParameter parameter) { this.parameter = parameter; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
and entity for AAQMSParamter is
package com.company.sheeloffice.entity;
import com.haulmont.chile.core.annotations.NamePattern;
import com.haulmont.cuba.core.entity.StandardEntity;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@NamePattern("%s, %s|parameter,aAQMS")
@Table(name = "SHEELOFFICE_AAQMS_PARAMETER")
@Entity(name = "sheeloffice_AAQMSParameter")
public class AAQMSParameter extends StandardEntity {
@NotNull
@Column(name = "PARAMETER_", nullable = false, length = 50)
protected String parameter;
@Column(name = "TARGET_TYPE")
protected String targetType;
@Column(name = "LOW_STANDARD")
protected Double lowStandard;
@Column(name = "HIGH_STANDARD")
protected Double highStandard;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "A_AQMS_ID")
protected AAQMS aAQMS;
@NotNull
@Column(name = "UNIT", nullable = false, length = 20)
protected String unit;
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public void setLowStandard(Double lowStandard) {
this.lowStandard = lowStandard;
}
public Double getLowStandard() {
return lowStandard;
}
public void setHighStandard(Double highStandard) {
this.highStandard = highStandard;
}
public Double getHighStandard() {
return highStandard;
}
public EnumTargetType getTargetType() {
return targetType == null ? null : EnumTargetType.fromId(targetType);
}
public void setTargetType(EnumTargetType targetType) {
this.targetType = targetType == null ? null : targetType.getId();
}
public AAQMS getAAQMS() {
return aAQMS;
}
public void setAAQMS(AAQMS aAQMS) {
this.aAQMS = aAQMS;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
I am using this code in AAQMSdata.edit
@Subscribe(target = Target.DATA_CONTEXT)
private void onPreCommit(DataContext.PreCommitEvent event) {
String comp = calculateCompliance();
getEditedEntity().setCompliance(comp);
}
public String calculateCompliance()
{
AAQMSData AAQMSData = getEditedEntity();
String MyCompliance = "No";
if(AAQMSData.getParameter().getTargetType().getId().equals("10")) {
if (AAQMSData.getValue() < AAQMSData.getParameter().getLowStandard()) {
MyCompliance = "No";
return MyCompliance;
}
else {
MyCompliance = "Yes";
return MyCompliance;
}
}
else if(AAQMSData.getParameter().getTargetType().getId().equals("30")) {
if (AAQMSData.getValue() > AAQMSData.getParameter().getHighStandard()) {
MyCompliance = "No";
return MyCompliance;
}
else {
MyCompliance = "Yes";
return MyCompliance;
}
}
else {
if (AAQMSData.getValue() < AAQMSData.getParameter().getLowStandard() || AAQMSData.getValue() > AAQMSData.getParameter().getHighStandard()) {
MyCompliance = "No";
return MyCompliance;
}
else {
MyCompliance = "Yes";
return MyCompliance;
}
}
}
As per the earlier support tickets this is due to views. but i have defined the views (include referenced entity and enum) in my application
Thanks