-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Incorrect sql generated: ManyToMany w additional columns
PostPosted: Wed Jan 11, 2012 3:09 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
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


Top
 Profile  
 
 Post subject: Re: Incorrect sql generated: ManyToMany w additional columns
PostPosted: Thu Jan 12, 2012 8:33 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Anyone?


Top
 Profile  
 
 Post subject: Re: Incorrect sql generated: ManyToMany w additional columns
PostPosted: Fri Jan 13, 2012 7:36 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Using hql the correct sql gets generated. I posted a Jira for this
https://hibernate.onjira.com/browse/HHH-6963


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.