Hi,
I'm running into a problem with a ManyToMany relation with an intermediary class with extra columns.
The entity reference in the sql is wrong. This is the sql I'm getting:
Code:
select this_.candidateId as candidat2_28_0_, this_.networkId as networkId28_0_, this_.remove as remove28_0_ from NetworkCandidates this_ where network2_.networkId=? and candidate3_.candidateId=?
it should be
Code:
select this_.candidateId as candidat2_28_0_, this_.networkId as networkId28_0_, this_.remove as remove28_0_ from NetworkCandidates this_ where this_.networkId=? and this_.candidateId=?
This is the class configuration I'm using.
Code:
@Entity
@Table
@AssociationOverrides({
@AssociationOverride(name = "pk.network",
joinColumns = @JoinColumn(name = "networkId")),
@AssociationOverride(name = "pk.candidate",
joinColumns = @JoinColumn(name = "candidateId"))
})
public class NetworkCandidateLink implements Serializable{
private NetworkCandidateLinkId pk = new NetworkCandidateLinkId();
private boolean remove=false;
public NetworkCandidateLink(){}
public NetworkCandidateLink(Network network, NetworkCandidate candidate){
pk.setCandidate(candidate);
pk.setNetwork(network);
}
@EmbeddedId
public NetworkCandidateLinkId getPk() {
return pk;
}
public void setPk(NetworkCandidateLinkId pk) {
this.pk = pk;
}
}
@Embeddable
public class NetworkCandidateLinkId implements Serializable{
private Network network;
private NetworkCandidate candidate;
@ManyToOne(optional=false)
public NetworkCandidate getCandidate() {
return candidate;
}
public void setCandidate(NetworkCandidate candidate) {
this.candidate = candidate;
}
@ManyToOne(cascade=CascadeType.MERGE,optional=false)
public Network getNetwork() {
return network;
}
public void setNetwork(Network network) {
this.network = network;
}
}
@Entity
@Table
public class Network implements Serializable{
private Long id;
private Set<NetworkCandidateLink> candidates;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="pk.network")
@Fetch(FetchMode.SELECT)
public Set<NetworkCandidateLink> getCandidates() {
return candidates;
}
}
@Entity
@Table
public class NetworkCandidate implements Serializable {
private Long id;
private List<NetworkCandidateLink> networks;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="pk.candidate")
public List<NetworkCandidateLink> getNetworks() {
return networks;
}
}
And this is the query I'm running.
Code:
public NetworkCandidateLink getCandidate(Long networkId, Long candidateId){
return (NetworkCandidateLink) this.getSessionFactory().getCurrentSession().createCriteria(NetworkCandidateLink.class)
.createAlias("pk", "pk")
.createAlias("pk.network", "network")
.createAlias("pk.candidate", "candidate")
.add(Restrictions.eq("network.id", networkId))
.add(Restrictions.eq("candidate.id", candidateId))
.uniqueResult();
}
Any ideas?
Kind regards,
Marc