Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JPA ManyToOne Mapping Question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would probably need to select based on some criteria and in your case it will be the date.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic