-->
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: Simple JoinTable query fails with mysql but works with pgsql
PostPosted: Thu Oct 09, 2008 4:36 pm 
Newbie

Joined: Thu Oct 09, 2008 4:10 pm
Posts: 1
Hi all,

We are trying to migrate our db from pgsql to mysql. Everything are ok expect queries contains subquery to a many-to-many explicit join table. Mysql always complains "Operand should contains 1 column(s)". Hereunder are simple test code

Code:
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table( name = "student")
public class Student {
 
  @Id
  @Column( name = "student_id", length = 32)
  @GeneratedValue(generator="hibernate-uuid.hex")
  @GenericGenerator( name="hibernate-uuid.hex", strategy="org.hibernate.id.UUIDHexGenerator")
  private String id;
 
  @Column( name = "name")
  private String name;

/* getter / setter */
  ...
}

@Entity
@Table( name = "course")
public class Course {
 
  @Id
  @Column( name = "course_id", length = 32)
  @GeneratedValue(generator="hibernate-uuid.hex")
  @GenericGenerator( name="hibernate-uuid.hex", strategy="org.hibernate.id.UUIDHexGenerator")
  private String id;
 
  @Column( name = "name")
  private String name;

/* getter / setter */
  ...
}

@Entity
@Table( name = "asso_student_course")
public class StudentCourse implements java.io.Serializable {
 
  @Embeddable
  public static class Id implements java.io.Serializable {
   
    @Column( name = "student_id", length = 32)
    private String studentId;
   
    @Column( name = "course_id", length = 32)
    private String courseId;
   
    public Id() {};
   
    public Id( String studentId, String courseId) {
      this.studentId = studentId;
      this.courseId = courseId;
    }
   
    public boolean equals( Object o) {
      if( o != null && o instanceof Id) {
        Id that = (Id) o;
        return this.studentId.equals( that.studentId) && this.courseId.equals( that.courseId);
      } else {
        return false;
      }
    }
   
    public int hashCode() {
      return this.studentId.hashCode() + this.courseId.hashCode();
    }
   
    public String getStudentId() {
      return studentId;
    }

    public void setStudentId(String studentId) {
      this.studentId = studentId;
    }

    public String getCourseId() {
      return courseId;
    }

    public void setCourseId(String courseId) {
      this.courseId = courseId;
    }
  }
 
  @EmbeddedId
  private Id id = new Id();
 
   
  /**
   * student relation
   */
  @ManyToOne
  @JoinColumn( name = "student_id", insertable = false, updatable = false)
  private Student student;
 
 
  /**
   * baseItem relation
   */
  @ManyToOne
  @JoinColumn( name = "course_id", insertable = false, updatable = false)
  private Course course;

/* getter / setter */
  ...

}



I tried to execute underlying query
Code:
select stu from Student stu where exists ( select sc from StudentCourse sc where sc.student = stu)


The generated SQL is

Code:
select
  student0_.student_id as student1_34_,
  student0_.name as name34_
from
  student student0_
where
  exists (
   select
    (studentcou1_.course_id,
    studentcou1_.student_id)
   from
    asso_student_course studentcou1_
   where
    studentcou1_.student_id=student0_.student_id
  )


The exception I got when the backend database is mysql
Code:
java.sql.SQLException: Operand should contain 1 column(s)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
   at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)


If the backend database is postgresql, the generated SQL is exactly the same and works perfectly.

I tried to remove the parenthesis in the subquery of generated SQL and it works even on MySQL.
Code:
select
  student0_.student_id as student1_34_,
  student0_.name as name34_
from
  student student0_
where
  exists (
   select
    studentcou1_.course_id,
    studentcou1_.student_id
   from
    asso_student_course studentcou1_
   where
    studentcou1_.student_id=student0_.student_id
  )


The execution environments are:
MySQL 5.0.67
j-connector 5.0.8
hibernate-core 3.3.1.GA
hibernate-entitymanager 3.4.0.GA
hibernate-annotations-3.4.0.GA

I am wondering if I missed something or this is a bug in Dialect? Any comment will be very appreciated.

//Austin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 4:58 pm 
Newbie

Joined: Tue Oct 28, 2008 4:47 pm
Posts: 1
I am also seeing this error. I have a similar query -- below is a snippet from the generated SQL

Does not work - same error as above post:
Code:
... select (supportedc6_.urn, supportedc6_.id, supportedc6_.idValue) from ...


Works with the parenthesis removed:
Code:
... select supportedc6_.urn, supportedc6_.id, supportedc6_.idValue from ...


I am using:
hibernate-core 3.3.1.GA
MySQL 4.1

After reading the above post, it seems these errors is related. I suspected a dialog bug as well -- just wondering if there has been any resolution on this?

Thanks for any info,
-Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 5:38 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
MySQL has some limitations. You cannot put more then one value into exists clause.
Try this:
Code:
select stu from Student stu where exists ( select 1 from StudentCourse sc where sc.student = stu)


If it helps don't forget torate.


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.