Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA ManyToOne Mapping Question

 
Dhriti lanka
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Experts,

I have a situation where i have two entities namely Event and Employee.

The Event entity has a ManyToOne relation with the Employee.

For a given Date, there is a single record of event for an employee.

Now, when i use employee.getEvents() , the list will be enormous .

Say after 5 year will have 5*365 records for an employee.

Is there anyway, i can establish a bounded relationship between employee and event.

I need to load only the records starting from today until the next one month .

Thanks
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
You will need to use JPQL to limit the results.

Also your name is not as per coderanch standards.


Regards,
Amit
 
Dhriti lanka
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Amit.

Can you please be more specific.

Do you mean i should make the getEvents() method return a limited result set?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would probably need to select based on some criteria and in your case it will be the date.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they mean to create another getEvents method with two dates as parameters.
you have getEvents();
you should also have getEvents(start,end)

Do you understand how to add parameters to your HQL?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fundamental problem is that you modelled you Java entities one-to-one as the ER looks like - a common mistake.

While n:m relations are always bidirectional in the relational model they don't have to be in Java - i.e if the getEvents() method should never be called it should not be modelled. If you are using tools to create entities from tables they typically make all relations bidirectional.

Remove the events collection. Write a service class that returns events for a user and a given time period (or a getEvents(start, end) method in the entity itself, but this means allowing it to access the persistence layer).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic