-->
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.  [ 6 posts ] 
Author Message
 Post subject: many-to-one relationship
PostPosted: Tue Mar 24, 2009 3:56 am 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
Iam an absolute beginner when it comes to Hibernate. Iam attempting to create my first one-to-many relationship and am receiving the following output when I attempt to reference the value

[java] Welcome nikki massamanute
[java] This staff member manages a department!
[java] This staff member manages id: 3
[java] Hibernate: select dept0_.dept_id as dept1_2_0_, dept0_.dept_name as dept2_2_0_ from dept dept0_ where dept0_.dept_id=?
[java] This staff member manages:
[java] 24/03/2009 17:42:57 org.hibernate.impl.SessionFactoryImpl close
[java] INFO: closing
[java] 24/03/2009 17:42:57 org.hibernate.connection.DriverManagerConnectionProvider close
[java] INFO: cleaning up connection pool: jdbc:mysql://localhost:3306/timekeeper

So it's basically saying that they do manage a department but that it can't retrieve the dept_name when I throw

System.out.println("This staff member manages: " + thisStaff.getI_manage().getDept_name());

The following is my code:

<class name="com.timekeeper.model.Staff_Member" table="staff_member">
<id name="staff_member_id" column="staff_member_id">
<generator class="native"/>
</id>
<property name="manages_dept" insert="false" update="false" />
<many-to-one name="i_manage" column="manages_dept" class="com.timekeeper.model.Dept" />
<property name="first_name"/>
<property name="last_name"/>
<property name="pass_word"/>
<property name="staff_type_id"/>
<property name="email" />
</class>

i_manage is a Dept object which (as far as I can tell) is correctly mapped.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 6:46 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
How are you getting thisStaff?

In ur o/p I see only select for dept. Why select for staff_member is not thr?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Iam loading the object previously
PostPosted: Wed Mar 25, 2009 4:57 am 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
thisStaff = (Staff_Member) session.createQuery("from Staff_Member where email='" + UName + "' and pass_word='" + PWord + "'").uniqueResult();

which gives the output:

Hibernate: select staff_memb0_.staff_member_id as staff1_0_, staff_memb0_.manages_dept as manages2_0_, staff_memb0_.first_name as first3_0_, staff_memb0_.last_name as last4_0_, staff_memb0_.pass_word as pass5_0_, staff_memb0_.staff_type_id as staff6_0_, staff_memb0_.email as email0_ from staff_member staff_memb0_ where staff_memb0_.email='flowers@gmail.com' and staff_memb0_.pass_word='flowers'

this is then followed by the call to fetch the dept_name as stated in the original post.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 8:38 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Well then... ur code and mapping looks fine... Double check if the data is thr in the DB ;-)

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Wed Mar 25, 2009 5:13 pm 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
Because Iam new to Hibernate I was sure that the problem must be with my Hibernate script. As it turns out you were right the problem was with my database. Stupidly enough my sql was inserting the dept_name into the dept_id and Iam surprised that mysql didn't report an error due to mismatched data types. In checking my table I saw that the dept_name was blank for each of the entries.

Fixed it and now it works perfectly thanks.

Cheers

[java] Welcome nikki massamanute
[java] This staff member manages a department!
[java] This staff member manages id: 3
[java] Hibernate: select dept0_.dept_id as dept1_2_0_, dept0_.dept_name as dept2_2_0_ from dept dept0_ where dept0_.dept_id=?
[java] This staff member manages: information technology


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 12:44 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Rating is welcome... :-)

_________________
Regards,
Litty Preeth


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.