We have following situation:
A Contract object with following (stripped) declaration: public class Contract{ private Long id; private Long claimId; private List<ContractObject> contractObjects;
... getters & setters ...
@OneToMany(fetch=FetchType.LAZY, mappedBy="contract") @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN}) @JoinColumn(name="CONTRACT_ID") public Set<ContractObject> getContractObjects() { return contractObjects; } }
A contract object is an abstract class with sub classes like ContractVehicle and ContractParty. But: there is a parent - child relation between ContractObject's.
So, the stripped version of ContractObject becomes:
public class ContractObject{ private Long id; private Set<ContractObject> childContractObjects;
private ContractObject parentContractObject;
.. getters & setters ..
@OneToMany(mappedBy="parentContractObjectRelation") @LazyCollection(LazyCollectionOption.TRUE) @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN}) @JoinColumn(name="RELATION_ID") public Set<ContractObject> getChildContractObjectRelations() { return childContractObjectRelations; }
@ManyToOne @JoinColumn(name="RELATION_ID") public ContractObject getParentContractObjectRelation() { return parentContractObjectRelation; } }
Now, when executing public List<Contract> findContractsOfClaim(Integer claimId) { return getHibernateTemplate().find("from Contract where CLAIM_ID=?", claimId); } we sometimes get contractobjects (contract.contractObjects) with duplicate child contract objects. That is at least strange (those relations are modeled as sets, so this should not be possible. The equals and hashcode methods were generated with eclipse but in the hashcode method, the references to parent and child relations were removed to prevent circular references). We don't think a 'distinct' indication in the query can help, since the contractObjects are only an attribute of contract and are (in this case) not queried directly.
The second problem is that, when querying the ContractObject class, we would expect a flat list of ContractObject's. (query eg: "from ContractObject where ContractId= ?"). Off course, there should be relations between those ContractObject objects, but we would expect a 'flat' list. Eg: if there is a parent with two children, belonging to the same contract(id), we would expect as result a list of three ContractObject instances). Which is not the case. Probably, this is a hibernate feature, but then we would like to know if this can be changed and what is exactly happening behind the scenes.
If more details are needed, feel free to ask.
Sincerely,
Dieter.
|