I have a problem with JPA and Hibernate where I have 2 entities that are in relation of 1 to many. On local host on the integraiton test using H2 the things are as they should (children are loaded), when deployed into the weblogic server that is using Oracle as DB, the children are not loaded. Any idea on this? The entities definitions are:
@Entity
@Table(name = "TB_MASTER")
@Data
@ToString
@NoArgsConstructor
public class Case implements Serializable {
private static final long serialVersionUID = 1919288768119684307463L;
@Id
@Column(name = "ID", nullable = false, length = 55)
private String id;
@Column(name = "CODE_ADR", nullable = false, length = 100)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "id.masterId")
@EqualsAndHashCode.Exclude
private Set<MasterReference> references = new TreeSet<>();
}
The EmbeddedId class:
@Data
@AllArgsConstructor
@EqualsAndHashCode
@NoArgsConstructor
@Embeddable
public class MasterReferenceId implements Serializable, Comparable {
@ManyToOne(optional = false)
@JoinColumn(name = "MASTER_ID", nullable = false)
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Master masterId;
@Column(name = "REF", nullable = false, length = 42)
private String reference;
@Column(name = "SHORCUT", nullable = false, length = 100)
private String shortcut;
@Column(name = "REF_DATE")
private Date refDate;
@Column(name = "REF_BACK", length = 1)
@Convert(converter = BooleanToNumberConverter.class)
private Boolean refBack;
@Column(name = "REF_DATA_ROW", length = 2000)
private String refDataRow;
@Override
public int compareTo(Object o) {
if (o instanceof MasterReferenceId && ((MasterReferenceId) o).getReference() != null && ((MasterReferenceId) o).getReference() != null) {
return ((MasterReferenceId) o).getReference().compareTo(this.getReference());
}
return 0;
}
}
The cild entity:
@Entity
@Table(name = "TB_MASTER_REFERENCE")
@Data
@ToString
@NoArgsConstructor
public class MasterReference implements Serializable, Comparable {
private static final long serialVersionUID = -517596820344348395356L;
@EmbeddedId
private MasterReferenceId id;
@Column(name = "UPDATE_DATE", nullable = false)
private Date updateDate;
@Column(name = "COMMENTS", length = 4000)
private String comments;
@Override
public int compareTo(Object o) {
if (o instanceof MasterReference && ((MasterReference) o).getId() != null) {
return ((MasterReference) o).getId().compareTo(this.getId());
}
return 0;
}
}
Try this instead:
public class MasterReference implements Serializable, Comparable {
@EmbeddedId
private MasterReferenceId id;
@ManyToOne(optional = false)
@JoinColumn(name = "MASTER_ID", nullable = false)
@MapsId("masterId")
private Master masterId;
..
}
public class MasterReferenceId implements Serializable, Comparable {
private String masterId;
..
}
Change the mappedBy in the OneToMany on references to just "masterId" so that it uses the reference mapping instead of trying to use the embeddedId.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.