[hibernate-issues] [Hibernate-JIRA] Created: (HHH-6963) Criteria generates incorrect sql: ManyToMany w additional columns
Marc Schipperheyn (JIRA)
noreply at atlassian.com
Thu Jan 12 10:41:21 EST 2012
Criteria generates incorrect sql: ManyToMany w additional columns
-----------------------------------------------------------------
Key: HHH-6963
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6963
Project: Hibernate ORM
Issue Type: Bug
Components: query-criteria
Affects Versions: 3.6.9, 3.6.8
Reporter: Marc Schipperheyn
Priority: Minor
Referencing forum https://forum.hibernate.org/viewtopic.php?f=1&t=1014117
Criteria generates the wrong sql with a ManyToMany with an intermediary class with extra columns using AssociatonOverride for an embeddedId.
Using hql, the correct sql is generated
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
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=?
Class configuration:
@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;
}
}
Criteria:
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();
HQL:
String hql = "from NetworkCandidateLink ncl inner join ncl.pk.network network inner join ncl.pk.candidate candidate where candidate.id=:candidateId and network.id=:networkId";
return (NetworkCandidateLink) this.getSessionFactory().getCurrentSession().createQuery(hql)
.setParameter("networkId", networkId)
.setParameter("candidateId", candidateId)
.uniqueResult();
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list