-->
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.  [ 1 post ] 
Author Message
 Post subject: many-to-may relation with extra column criteria problems
PostPosted: Fri Oct 19, 2012 5:46 am 
Newbie

Joined: Fri Oct 19, 2012 5:42 am
Posts: 1
I followed this tutorial http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/ to implement in my domain model a many-to-many relationship with an extra column. It works great but I'm unable to create a criteria to query a field within the left side of my relation.

Taking this code

Code:
            @Entity
            @Table(   name = "projects")
            public class Project implements Cloneable, Serializable{
             
               private Long id;
               private String name;
               private Set<ProjectOrganization> projectOrganizations = new HashSet<ProjectOrganization>(0);
           
               @Id
               @GeneratedValue(strategy = GenerationType.AUTO)
               @Column(nullable = false)
               public Long getId() {
                  return this.id;
               }
           
   
               public void setId(Long id) {
                  this.id = id;
               }
           
   
               @Column(name = "name", length = 255, nullable = false)
               public String getName() {
                  return this.name;
               }
           
   
               public void setName(String name) {
                  this.name = name;
               }
           
   
               @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.project")
               @Cascade(value = { CascadeType.ALL })
               public Set<ProjectOrganization> getProjectOrganizations() {
                  return this.projectOrganizations;
               }
           
   
               public void setProjectOrganizations(Set<ProjectOrganization> organizationProjects) {
                  this.projectOrganizations = organizationProjects;
               }
           
            }
       
       
   
   
   
        @Entity
        @Table(name = "projects_has_organizations")
        @AssociationOverrides({ @AssociationOverride(name = "pk.project", joinColumns = @JoinColumn(name = "projects_id")),
                          @AssociationOverride(name = "pk.organization", joinColumns = @JoinColumn(name = "organizations_id"))
                          })
        public class ProjectOrganization implements Cloneable, Serializable {
       
       
           private ProjectOrganizationPK pk = new ProjectOrganizationPK();
           private OrganizationRolesEnum role;
       
           public ProjectOrganization() {
           }
       
           @Transient
           public Organization getOrganization() {
              return getPk().getOrganization();
           }
       
           public void setOrganization(Organization organization) {
              getPk().setOrganization(organization);
           }
       
           @EmbeddedId
           public ProjectOrganizationPK getPk() {
              return pk;
           }
       
           public void setPk(ProjectOrganizationPK pk) {
              this.pk = pk;
           }
       
           @Transient
           public Project getProject() {
              return getPk().getProject();
           }
       
           public void setProject(Project project) {
              getPk().setProject(project);
           }
       
           @Enumerated(EnumType.STRING)
           @Column(nullable = false, length = 50)
           public OrganizationRolesEnum getRole() {
              return role;
           }
       
           public void setRole(OrganizationRolesEnum role) {
              this.role = role;
           }
       
        }
       
        @Embeddable
        public class ProjectOrganizationPK implements Cloneable, Serializable {
       
           /** Generated serial version UID */
           private static final long serialVersionUID = -4534322563105003365L;
       
           private Organization organization;
           private Project project;
       
           @ManyToOne
           public Organization getOrganization() {
              return organization;
           }
       
           public void setOrganization(Organization organization) {
              this.organization = organization;
           }
       
           @ManyToOne
           public Project getProject() {
              return project;
           }
       
           public void setProject(Project project) {
              this.project = project;
           }
        }
       
        @Entity
        @Table(name = "organizations")
        public class Organization implements Cloneable, Serializable {
       
           private Long id;
           private String name;
           private Set<ProjectOrganization> projectOrganizations = new HashSet<ProjectOrganization>(0);
       
           public Organization() {
           }
       
       
           @Id
           @GeneratedValue(strategy = GenerationType.AUTO)
           @Column(nullable = false)
           @Override
           public Long getId() {
              return this.id;
           }
       
           public void setId(Long id) {
              this.id = id;
           }
       
           @Column(name = "name", nullable = false, length = 255)
           @NotNull(message = "A name is required!")
           public String getName() {
              return this.name;
           }
       
           public void setName(String name) {
              this.name = name;
           }
       
       
           @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.organization")
           public Set<ProjectOrganization> getProjectOrganization() {
              return this.projectOrganizations;
           }
       
       
           public void setProjectOrganization(Set<ProjectOrganization> projectOrganizations) {
              this.projectOrganizations = projectOrganizations;
           }
    }


I want is to create a criteria to select a `Project` which has an `organization` with a requested name.
Code:
    final Criteria crit = getSession().createCriteria(Project.class);
    crit.createCriteria("projectOrganizations", "projectOrganization").
     createAlias("pk.organization", "organization").
      add( Restrictions.like("organization.name", "TEST"));




But when i run this code i have this error
Code:
2012-10-19 10:38:43,095 ERROR  [org.hibernate.util.JDBCExceptionReporter] Unknown column 'organizati2_.name' in 'where clause'


and the sql query generated by hibernate is incomplete, doesn't join projects_has_organizations.organization with organization.id.. So it can't find column organization.name

Code:
    SELECT
       ....
    FROM
       projects this_
    INNER JOIN projects_has_organizations projectorg1_ ON this_.id = projectorg1_.projects_id
    WHERE
       projectorg1_.role =?
    AND organizati2_. NAME LIKE ?
    ORDER BY
       this_.publish_date DESC



What's wrong with this code? How can i build query using criteria ?


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

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.