-->
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.  [ 5 posts ] 
Author Message
 Post subject: Is many-to-one broken on MySQL? (SOLVED)
PostPosted: Thu Jun 09, 2005 7:57 am 
Newbie

Joined: Wed Jun 08, 2005 3:36 pm
Posts: 4
On MySQL I am trying to get objects that mapped as follows

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="hibernate.UserLesson, hibernate" table="user_lesson">


      <composite-id>
         <key-property name="user_id" column="user_id" access="field" />
         <key-property name="lesson_id" column="lesson_id" type="System.Int32" access="field" />
      </composite-id>

        <property name="Score" column="score"></property>
        <property name="Attempts" column="attempts"></property>
        <property name="ReverseAttempts" column="reverse_attempts"></property>         
        <property name="ReverseScore" column="reverse_score"></property>

      <many-to-one name="ToLesson" class="hibernate.Lesson,hibernate" />

    </class>
</hibernate-mapping>


but I am getting the following exception

Code:
[MySqlException: Unknown column 'userless0_.ToLesson' in 'field list']
   MySql.Data.MySqlClient.PacketReader.CheckForError()
   MySql.Data.MySqlClient.PacketReader.ReadHeader()
   MySql.Data.MySqlClient.PacketReader.OpenPacket()
   MySql.Data.MySqlClient.NativeDriver.ReadResult(UInt64& affectedRows, Int64& lastInsertId)
   MySql.Data.MySqlClient.CommandResult.ReadNextResult(Boolean isFirst)
   MySql.Data.MySqlClient.CommandResult..ctor(Driver d, Boolean isBinary)
   MySql.Data.MySqlClient.NativeDriver.SendQuery(Byte[] bytes, Int32 length, Boolean consume)
   MySql.Data.MySqlClient.MySqlCommand.GetNextResultSet(MySqlDataReader reader)
   MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   MySql.Data.MySqlClient.MySqlCommand.System.Data.IDbCommand.ExecuteReader()
   NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
   NHibernate.Loader.Loader.GetResultSet(IDbCommand st, RowSelection selection, ISessionImplementor session)
   NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
   NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
   NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes)
   NHibernate.Hql.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters)
   NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)


and indeed generated SQL is out of wack

Code:
2005-06-09 07:38:30,651 [964] DEBUG NHibernate.Hql.QueryTranslator [] <> - HQL: from hibernate.UserLesson as ul where ul.user_id = 2
2005-06-09 07:38:30,661 [964] DEBUG NHibernate.Hql.QueryTranslator [] <> - SQL: select userless0_.user_id as user_id, userless0_.lesson_id as lesson_id, userless0_.reverse_attempts as reverse_5_, userless0_.ToLesson as ToLesson, userless0_.reverse_score as reverse_6_, userless0_.score as score, userless0_.attempts as attempts from user_lesson userless0_ where (userless0_.user_id=2)
2005-06-09 07:38:30,661 [964] DEBUG NHibernate.Hql.QueryTranslator [] <> - HQL: from hibernate.UserLesson as ul where ul.user_id = 2
2005-06-09 07:38:30,671 [964] DEBUG NHibernate.Hql.QueryTranslator [] <> - SQL: select userless0_.user_id as user_id, userless0_.lesson_id as lesson_id, userless0_.reverse_attempts as reverse_5_, userless0_.ToLesson as ToLesson, userless0_.reverse_score as reverse_6_, userless0_.score as score, userless0_.attempts as attempts from user_lesson userless0_ where (userless0_.user_id=2)
2005-06-09 07:38:30,681 [964] INFO  NHibernate.Loader.Loader [] <> - select userless0_.user_id as user_id, userless0_.lesson_id as lesson_id, userless0_.reverse_attempts as reverse_5_, userless0_.ToLesson as ToLesson, userless0_.reverse_score as reverse_6_, userless0_.score as score, userless0_.attempts as attempts from user_lesson userless0_ where (userless0_.user_id=2)


I have couple questions:

1. Is this MySQL specific bug? I am surprised to find that I cannot fetch such simple objects.

2. Do I understand it right that loading objects with Many-to-one mapping should result in a query spanning two table and I should have my -to-one object (Lesson) loaded?


Last edited by zander on Tue Jun 14, 2005 12:39 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 4:00 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I don't quite understand what you are trying to achieve. NHibernate is doing right, <many-to-one> means that you have in your table (the many- end) a foreign key to some other table (the -to-one end).

Tell us what you are trying to do and we'll find a solution.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 11:48 am 
sergey wrote:
I don't quite understand what you are trying to achieve.


I am trying to fetch objects. It works fine when I do not define <many-to-one>, but when I add <many-to-one> to the mapping, it blows because SQL that got generated is wrong.

sergey wrote:
NHibernate is doing right, <many-to-one> means that you have in your table (the many- end) a foreign key to some other table (the -to-one end).


I have exactly this setup. I have Lesson and UserLesson, lesson_id is primary/foreign key. UserLesson is the "many end". The database is MySQL.

Could anybody confirm that many-to-one works on MySQL? (I believe it is MySQL related code that blows).

And what SQL should be generated when trying to load objects mapped as posted in the original post?

Thanks.


Top
  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:19 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, many-to-one works with MySQL just fine. But you should probably remove it and turn <key-property name="lesson_id" ...> into <key-many-to-one> (and change the lesson_id property from Int32 to Lesson).

If you can't, then leave that <many-to-one>, but add column="lesson_id" to it, and also add insert="false", update="false".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 12:42 pm 
Newbie

Joined: Wed Jun 08, 2005 3:36 pm
Posts: 4
Adding column="lesson_id" sure did the trick.

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.