-->
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.  [ 7 posts ] 
Author Message
 Post subject: <set> tag not working correctly
PostPosted: Wed May 26, 2004 5:08 pm 
Beginner
Beginner

Joined: Sun May 16, 2004 3:53 pm
Posts: 47
Location: Belo Horizonte, Brazil
Hi,
i got 2 tables wich look like the following.

table Organization (orgId (pk), orgName)

table Employee (empId, empName, fk_orgId)

This is a one (Organization) - to - many (Employee) relationship.
I'd like to have a bidirection relation here, what causes me to insert the following method in the Organization class, i'd like to have all the employees for a specific company (i know that i can get this data from a find query in Employee class, but i'd like to make it this way - straigth call).

Code:
public Set getEmployees()


Naturally, this method doesn't map to a physical field.
I'm trying to use this mapping on Organization.hbm.xml

[code]
<set
name="employees"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
order-by="firstName"
>
<key column="fk_orgId"/>
<one-to-many
class="hibernate.Employee"
/>
</set>[/code

But i get a null pointer exception when trying to access it in the following way.

[code]
Set org10Employees = org1.getEmployees();[/code]

Does anyone know what i'm doing wrong???

Thanks,
ltcmelo


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 7:45 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
See that here you can take null pointer exception only if "org1" is null.
This exception is not about the "Set"

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 9:12 am 
Beginner
Beginner

Joined: Sun May 16, 2004 3:53 pm
Posts: 47
Location: Belo Horizonte, Brazil
That's the whole point, org1 is not null.

If i run the app in debug mode and i watch the org1 object at this point, it's fields (orgId and orgName) are instanciated correctly, but the field employees are null. That's why i get the null pointer exception.

The problem is probably because i don't get the employees for the specific company loaded to the organization object, i don't know why.

Have any other ideas???

Thanks,
ltcmelo


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 9:04 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 10:32 pm
Posts: 35
Location: Toronto, Ontario
For bidirectional one-to-many relationships declare inverse="true" on the many side (set of employees). Also declare and initialize the variable as Set employees = new HashSet();
Mind you still shouldn't have gotten the Null Pointer Exception on org.getEmployees() so maybe something else is wrong. Maybe there's additional logic in the getEmployees() that's causing the problem


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 9:34 pm 
Beginner
Beginner

Joined: Sun May 16, 2004 3:53 pm
Posts: 47
Location: Belo Horizonte, Brazil
Why do i have to declare and instanciate it as HashSet ?

Where exactly, before the getEmployees() method?

Thanks,
ltcmelo


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 11:05 am 
Beginner
Beginner

Joined: Mon Sep 29, 2003 10:32 pm
Posts: 35
Location: Toronto, Ontario
Like this:
Code:
public class Organization{
private String name;
private Set employees = new HashSet();

public void setName(String name){
   this.name = name;
}

//Other setters and getters

public Set getEmployees(){
  return employees;
}
}



Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 3:40 pm 
Beginner
Beginner

Joined: Sun May 16, 2004 3:53 pm
Posts: 47
Location: Belo Horizonte, Brazil
Thanks,
actually i stoped getting the nullPointException, but i cannot get the employees of the company yet (the method doesn't return the organization employees).

I posted all my code here
http://forum.hibernate.org/viewtopic.php?t=931275

So, if you could take a look there to see what's wrong, i'd appreciate even more!

Thank you,
ltcmelo

p.s.: altough the code on the link i cited doesn't have the new HashSet(), i've already put it in my current code.


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