-->
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.  [ 12 posts ] 
Author Message
 Post subject: many-to-one association problem
PostPosted: Tue Aug 10, 2010 2:33 am 
Newbie

Joined: Mon Aug 09, 2010 3:39 pm
Posts: 7
I have a class StaffSchedule which has mant to one relationship with another class Staff. See below:

Code:
<class name="StaffSchedule" table="TC_StaffSchedule">
.......      
      <many-to-one name="staffId" column="staffId"
      class="test.Staff" not-null="true"/>
........
/class>


The class Staff has a PK called id. In my code I am trying to find all the schedules for a given staffId. See below:
Code:
List<StaffSchedule> staffScheduleList = session.createCriteria(StaffSchedule.class)
                     .add(Restrictions.eq("staffId", staffId)).list();


This is throwing an exception:
Code:
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Staff.id
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class


However if I change my many to one association as follows I get a different error.
Code:
<many-to-one name="staffId" column="staffId"
      class="test.Staff" property-ref="id" not-null="true"/>


The default value of property-ref should be the PK of the Staff (id), so the property-ref should NOT have made any difference, but see below:

Code:
Service method 'public abstract java.util.List test.StaffSchedulerService.getAllSchedules(long)' threw an unexpected exception: java.lang.NullPointerException
....
Caused by: java.lang.NullPointerException
   at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:524)
   at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:3844)
   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
   at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:141)
...


I am confused! I am using Java 1.6 with Hibernate 3.5.3.
Any pointers?


Last edited by suomi99 on Tue Aug 10, 2010 3:56 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 3:33 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Hi Saoumi99,

You can also use HQL query.

Code:
List staffScheduleList  = session.createQuery("Example"."from StaffSchedule ss where ss.staffId.identifier=:staffIdentifier").setLong("staffIdentifier",value).list();


Try this and let me know.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 4:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There is probably something wrong with your entity classes. Check that your StaffSchedule.getStaffId() really returns a Staff object.


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 6:52 am 
Newbie

Joined: Mon Aug 09, 2010 3:39 pm
Posts: 7
Thanks for your response shynate26. The query works when I use HQL, though I cannot see any difference in the SQL that Hibernate generates in both cases, see below.

SQL generated by query using Criteria:
Code:
List<StaffSchedule> staffScheduleList = session.createQuery(
            "from StaffSchedule ss where ss.staffId=:staffIdentifier").setLong("staffIdentifier",staffId).list();
select
        staffsched0_.id as id6_,
        staffsched0_.staffId as staffId6_,
        staffsched0_.startDate as startDate6_,
        staffsched0_.endDate as endDate6_
    from
        TC_StaffSchedule staffsched0_
    where
        staffsched0_.staffId=?


And SQL generated by HQL:

Code:
List<StaffSchedule> staffScheduleList = session.createCriteria(StaffSchedule.class)
                     .add(Restrictions.eq("staffId", staffId)).list();
select
        this_.id as id6_0_,
        this_.staffId as staffId6_0_,
        this_.startDate as startDate6_0_,
        this_.endDate as endDate6_0_
    from
        TC_StaffSchedule this_
    where
        this_.staffId=?


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 7:00 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Hi Suomi99,

Whether the problem resolved or still you get property violation error?

By default if many-to-one is specified hibernate looks at the identifier column , which is unique in the table. Only when it is referenced other than primary key property-ref can be used. Else we can ignore that attribute.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 9:17 am 
Beginner
Beginner

Joined: Sat Nov 19, 2005 11:54 am
Posts: 22
you can Also solve problem by this code if you don`t want to use HQL:
Code:
Criteria c=session.createCriteria(StaffSchedule.class);
c.createAlias("staffId","staffId");
List<Map> mapList= c.setResultTransformer(Transformers.aliasToBean(Staff.class)).list();

now you can Iterate in mapList and access to Staff by "staffId" key
but i suggest you HQL


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 10:17 am 
Newbie

Joined: Mon Aug 09, 2010 3:39 pm
Posts: 7
Nordborg, thanks for your response. I understand the problem now.

The class definition for StaffSchedule returns the PK of Staff and not the Staff object itself. And I cannot change this because we are trying to replace Hibernate with another home grown solution
Code:
public class StaffSchedule implements Serializable {
   // the PK of the schedule
   protected long id;
   // the FK to the staff owning this schedule
   protected long staffId;
   // the date from when the schedule starts. null if it starts from now.
   protected Date startDate;   
   // the date till when the schedule is applicable.
   protected Date endDate;
}


So, I think I need to correct my many-to-one association. My current many-to-one association is shown below. How can I model it to reflect that the StaffSchedule.getStaffId() returns the the PK for Staff and not the Staff object itself?
Code:
<class name="StaffSchedule" table="TC_StaffSchedule">
.......     
      <many-to-one name="staffId" column="staffId"
      class="test.Staff" not-null="true"/>
........
/class>


Last edited by suomi99 on Tue Aug 10, 2010 3:57 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 12:34 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You can model it as a regular <property>. The drawback with this is that, as far as Hibernate is concerned, there is no longer a many-to-one association between the StaffSchedule and Staff objects. This means that you, for example, can't do joins in queries/criterias, can't use cascading for save/update/delete/etc.., and possible some other limitations as well. Since you are moving away Hibernate, this may not be of any importance to you anyway.


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 12:51 pm 
Newbie

Joined: Mon Aug 09, 2010 3:39 pm
Posts: 7
Thanks nordborg.

I meant we are moving to Hibernate from a home-grown solution, which is why I the data-model exists and cannot be changed. Thanks for your tip mate!


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Tue Aug 10, 2010 5:23 pm 
Newbie

Joined: Mon Aug 09, 2010 3:39 pm
Posts: 7
I have now changed my model so that StaffSchedule.getStaff returns a Staff obect and not a PK of Staff. However I am still getting the same old error.

Code:
public class StaffSchedule implements Serializable {
   // the PK of the schedule
   protected long id;
   // the FK to the staff owning this schedule
   protected Staff staff;   
   // the date from when the schedule starts. null if it starts from now.
   protected Date startDate;   
   // the date till when the schedule is applicable.
   protected Date endDate;
}

Code:
<many-to-one name="staff" column="staff"
      class="test.Staff" not-null="true"/>


The following lines throw an error:
Code:
List<StaffSchedule> staffScheduleList = session.createCriteria(StaffSchedule.class)
                     .add(Restrictions.eq("staff", staffId)).list();


The error is:
Code:
Service method 'public abstract java.util.List test.StaffSchedulerService.getAllSchedules(long)' threw an unexpected exception: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Staff.id
.....
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Staff.id
.....
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:172)
   ... 46 more


Could anything else be the matter?


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Wed Aug 11, 2010 12:10 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
You have earlier mapped staffId to colun staffId. But after changing the property to staff, column mapping also changed to staff .

Which is the correct column name staffId or staff ? Check with your schema.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: many-to-one association problem
PostPosted: Wed Aug 11, 2010 2:35 am 
Newbie

Joined: Mon Aug 09, 2010 3:39 pm
Posts: 7
Thanks for your response shynate26.

I had dropped the database table and recreated it, so, that is not the problem. My Hibernate mapping is as follows:
Code:
<many-to-one name="staff" column="staff"
      class="test.Staff" not-null="true"/>


My table definition.
Code:
ID   NUMBER(19,0)   No      1   
STAFF   NUMBER(19,0)   No      2   
STARTDATE   DATE   No      3   
ENDDATE   DATE   No      4   


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