File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception while firing the join queries

 
krishna bulusu
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the following two Entities.
Manager:

Employee:

Now I want to retrieve a manager entity and also want to fectch the employee list eagerly.
My query is like this:

But I am getting the following exception while firing the above query:

Exception in thread "main" javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: Employees of: bala.persistent.Manager [select m from bala.persistent.Manager m LEFT JOIN FETCH m.Employees where m.id=:mngid]

Can anybody explain why this exception and try to correct it.
And one more doubt is that, how can I retrieve a list of employees for a given manager.
I think of the following query but it is wrong.

But I am getting the following exception for the above query:
Exception in thread "main" javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [select e from bala.persistent.Employee e JOIN Manager m where m.id=:mngid]

Please explain the above two doubts.
[ August 07, 2008: Message edited by: Christophe Verre ]
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You'd better follow the bean naming convention and rename it "private List employees;"
 
krishna bulusu
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe,
Thanks!!! First problem was solved.
Could explain about my second doubt.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need a JOIN ?
select e from bala.persistent.Employee e WHERE e.manager.id = :mngid
 
krishna bulusu
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that.
But, look at the following sql query. I want to write in EJB QL.

And I have another problem. The Employee table is the database doesn't contain the manager id!!!
following is my client code:

I know if I set emp.setManager(m) and emp1.setManager(m) would solve the problem. I don't want to do that. I want the JPA would automatically do that. What should I do in the entity class in order to achieve this.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Employee table is the database doesn't contain the manager id!!!

Why ? The owning side of the relation is Employee, isn't it ? If MANAGER_ID is not in the employee table, where is it ?
 
krishna bulusu
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean to say that the JPA is not inserting the manager id in the employee table. The employee table contains the column 'Manager_id' but it doesn't contain any value.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can. Concerning entity relationships, it is your responsability to set the necessary info in each entity. So you'll have to call setManager for each employee too.

2.1.7 Entity Relationships
Note that it is the application that bears responsibility for maintaining the consistency of runtime relationships�for example, for insuring that the "one" and the "many" sides of a bidirectional relationship are consistent with one another when the application updates the relationship at runtime.
 
krishna bulusu
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess, we can do it in Hibernate. I forgot how to do it.
A wild guess is setting inverse=true on the Entity may be or else someting other than that.
Anyway, If I find I would definitely post it. Thanks for you help!!!
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably this, but that's Hibernate, not JPA
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic