I have a very strange issue where Hibernate isn't hydrating a specific many-to-one relationship. I am using 3.5.5 with annotations. Here are my domain classes (watered down for relevance - e.g., getters/setters not displayed):
Code:
@Entity
@Table(name = "PHYSICIAN")
public class Physician extends VersionedPersistentEntity
{
@OneToMany(mappedBy = "physician")
private Set<PhysicianInsurance> acceptedInsurance;
}
@Entity
@Table(name = "PHYSICIAN_INSURANCE")
public class PhysicianInsurance extends VersionedPersistentEntity
{
@Column(name = "PHYS_INS_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PhysicianInsuranceIdGenerator")
@Id
@SequenceGenerator(name = "PhysicianInsuranceIdGenerator", sequenceName = "PHYS_INS_ID_SEQ")
private Long id;
@JoinColumn(name = "INS_ID")
@ManyToOne(fetch = FetchType.LAZY)
private InsuranceProduct insuranceProduct;
@JoinColumn(name = "PHYS_ID")
@ManyToOne(fetch = FetchType.LAZY)
private Physician physician;
}
@Entity
@Table(name = "INSURANCE")
public class InsuranceProduct extends ActivatableEntity implements SourceableEntity
{
@Column(name = "INS_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "InsuranceProductIdGenerator")
@Id
@SequenceGenerator(name = "InsuranceProductIdGenerator", sequenceName = "INS_ID_SEQ")
private Long id;
@Basic
@Column(name = "INS_NAME", nullable = false)
private String name;
@Basic
@Column(name = "INS_SYS_CD")
private String systemCode;
}
In my code, I'm trying to retrieve a list of PhysicianInsurance object with the insuranceProduct and physician relationships prefetched. What I'm seeing is the physician property is being hydrated, but the insuranceProduct still remains a proxy. Here is my relevant DAO code:
Code:
StringBuffer hql = new StringBuffer();
hql.append(StringUtils.join(new String[]
{
"select physicianInsurance from " + PhysicianInsurance.class.getCanonicalName() + " physicianInsurance",
"join fetch physicianInsurance.physician physician",
"join fetch physicianInsurance.insuranceProduct insurance",
"where physician.id = :physicianId",
"order by physicianInsurance.id"
}, " "));
// Build query.
Query query = session.createQuery(hql.toString());
query.setReadOnly(true);
query.setFirstResult(1);
query.setMaxResults(100);
query.setParameter("physicianId", somePhysicianId);
// Execute query.
List<PhysicianInsurance> insurances = query.list();
I am finding I have to do an ugly workaround of retrieving the InsuranceProducts in a separate query, and then associating them with the PhysicianInsurance objects manually in memory. Doesn't seem like I should need to do this. Any ideas out there? Thanks in advance.
Michael