-->
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: hibernate composite-id generating unknown columns
PostPosted: Mon Aug 14, 2006 12:08 pm 
Newbie

Joined: Fri Aug 11, 2006 1:38 pm
Posts: 3
I have a very simple class with a composite id. The class has two Integer properties which are used as composite id.

However tyring to access or save the class throws up exception. The reason is there are unknown colums in the SQL query.

Hibernate version: Hibernate 3.1.2 & Hibernate Annotations 3.1 Beta 8

Mapping documents:

Hibernate.cfg.xml
Code:
<hibernate-configuration>
  <session-factory>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.username">xx</property>
    <property name="connection.password">xx</property>
    <property name="connection.url">jdbc:mysql://localhost/teacher_student_db</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="show_sql">true</property>
    <property name="generate_statistics">false</property>
    <property name="c3p0.acquire_increment">1</property>
    <property name="c3p0.acquire_increment">100</property>
    <property name="c3p0.idle_test_period">100</property>
    <property name="c3p0.max_size">10</property>
    <property name="c3p0.max_statements">0</property>
    <property name="c3p0.min_size">1</property>
    <property name="c3p0.timeout">100</property>

    <mapping class="com.test.teacherstudent.entity.teacherstudent.TeacherStudentBean" />
  </session-factory>
</hibernate-configuration>


Here is the class:
Code:
@Entity
@Table(name = "teacher_student")
public class TeacherStudentBean implements Serializable, com.test.teacherstudent.entity.teacherstudent.TeacherStudentIf {


    /**
     * the composite primary key.
     */
   private com.test.teacherstudent.entity.teacherstudent.TeacherStudentPK primaryKey;

   public TeacherStudentBean() {
   }

   /**
    * Value object constructor.
    */
   public TeacherStudentBean(com.test.teacherstudent.entity.teacherstudent.TeacherStudentIf value) {
      if (value != null) {
         setTeacherId(value.getTeacherId());
      }
      if (value != null) {
         setStudentId(value.getStudentId());
      }
   }

   public com.finalist.teacherstudent.entity.teacherstudent.TeacherStudentPK create(com.finalist.teacherstudent.entity.teacherstudent.TeacherStudentIf value) {
      if (value != null) {
        setTeacherId(value.getTeacherId());
      }
      if (value != null) {
        setStudentId(value.getStudentId());
      }
      if (value != null) {
         setTeacherId(value.getTeacherId());
      }
      if (value != null) {
         setStudentId(value.getStudentId());
      }
      return value.getPrimaryKey();
   }


   @Id
   public com.test.teacherstudent.entity.teacherstudent.TeacherStudentPK getPrimaryKey() {
      return primaryKey;
   }


   public void setPrimaryKey(com.test.teacherstudent.entity.teacherstudent.TeacherStudentPK primaryKey) {
        this.primaryKey = primaryKey;
    }

   /**
    * Returns the value of the <code>teacherId</code> property.
    *
    */
   @javax.persistence.Transient 
   public java.lang.Integer getTeacherId() {
      if (primaryKey == null) {
        return null;
      }   
      return primaryKey.getTeacherId();
   }


   @javax.persistence.Transient
   public void setTeacherId(java.lang.Integer teacherId) {
      if (primaryKey == null) {
         primaryKey = new com.finalist.teacherstudent.entity.teacherstudent.TeacherStudentPK();
      }
      primaryKey.setTeacherId(teacherId);
   }

   @javax.persistence.Transient 
   public java.lang.Integer getStudentId() {
      if (primaryKey == null) {
        return null;
      }   
      return primaryKey.getStudentId();
   }


   @javax.persistence.Transient
   public void setStudentId(java.lang.Integer studentId) {
      if (primaryKey == null) {
         primaryKey = new com.finalist.teacherstudent.entity.teacherstudent.TeacherStudentPK();
      }
      primaryKey.setStudentId(studentId);
   }


}


The primary key is as follows:
Code:
@Embeddable
public class TeacherStudentPK implements java.io.Serializable {

   @javax.persistence.Column(name="teacher_id")
   public java.lang.Integer teacherId;
   @javax.persistence.Column(name="student_id")
   public java.lang.Integer studentId;

   public TeacherStudentPK()
   {
   }

   public TeacherStudentPK(java.lang.Integer teacherId, java.lang.Integer studentId) {
      this.teacherId = teacherId;
      this.studentId = studentId;
   }

   public java.lang.Integer getTeacherId()
   {
      return teacherId;
   }

   public void setTeacherId(java.lang.Integer teacherId)
   {
      this.teacherId = teacherId;
   }
   public java.lang.Integer getStudentId()
   {
      return studentId;
   }

   public void setStudentId(java.lang.Integer studentId)
   {
      this.studentId = studentId;
   }
...



Code between sessionFactory.openSession() and session.close():
Nothing unusual

Full stack trace of any exception that occurs:
Code:
org.hibernate.exception.SQLGrammarException: could not execute query
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.j
ava:65)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp
er.java:43)
        at org.hibernate.loader.Loader.doList(Loader.java:2148)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
        at org.hibernate.loader.Loader.list(Loader.java:2024)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.ja
va:308)
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java
:153)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1129)
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
        at com.ttpcom.teacherstudent.HibernateQueryHelper.list(HibernateQueryHel
per.java:98)
        at com.finalist.teacherstudent.session.TeacherStudentFacade.getTeacherSt
udentList(TeacherStudentFacade.java:436)
        at com.ttpcom.teacherstudent.actions.InitTeacherStudentSearchAction.doPe
rform(InitTeacherStudentSearchAction.java:48)
        at com.finalist.util.genelv.struts.GenericAction.execute(GenericAction.j
ava:69)
        at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
tProcessor.java:421)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:226)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:116
4)
        at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilte
r.java:169)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at com.ttpcom.teacherstudent.SessionFilter.doFilter(SessionFilter.java:3
9)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrid
eFilter.java:125)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
        at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcesso
r.java:833)
        at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.pr
ocess(Http11AprProtocol.java:639)
        at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:12
85)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Unknown column 'teacherstu0_.teacherId' in 'fi
eld list'
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.ja
va:936)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:
1030)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewPr
oxyPreparedStatement.java:76)
        at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:
139)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
        at org.hibernate.loader.Loader.doQuery(Loader.java:662)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Lo
ader.java:224)
        at org.hibernate.loader.Loader.doList(Loader.java:2145)
        ... 38 more


The relevant part is
Code:
Caused by: java.sql.SQLException: Unknown column 'teacherstu0_.teacherId' in 'fi
eld list'
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.ja
va:936)


Name and version of the database you are using: MySQL 5.0

The generated SQL (show_sql=true):
Code:
Hibernate: select teacherstu0_.teacherId as teacherId2_, teacherstu0_.studentId
as studentId2_ from teacher_student teacherstu0_


The interesting part is that Hibernate is generating "teacherId" and "studentId" columns which do not exist. The columns available are "teacher_id" and "student_id"

Any ideas please?

Thanks.


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.