• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Implementing a Entity Manager that realizes the Singleton pattern

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I want to create an Entity Manager that realizes the Singleton pattern. The entities are Apartments.
The program adds properties to a list and they can be read out of the list.
Here is my implementation so far:
Enum


Entity Interface


Estate Manager Class


Entity not found exception


Estate Class


EstateUnit Class


My problem is that I dont exactly understand how Im supposed to integrate Singleton with the Entity Manager.
Any helpful pointer would be appreciated!
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have now solved the Singleton part as follows:


Now the question remains:
Is it possible to do an Entity Manager without the JPA?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Von Valkenburgh wrote:I have now solved the Singleton part as follows:


OK, but I'm still not sure what this "entity manager" is (a) supposed to be, or (b) why you think that it needs to be a singleton.

And sorry, but I particularly dislike the use of the "initalize-on-demand-holder" idiom. It's simply "noise code" and, in your case, NOT needed.

That idiom is for use when creating the default instance is prohibitively (and I mean CPU seconds or minutes) expensive; not when you're simply calling
  new EntityManager() (which does nothing).
For any other case (ie, 99.9999% of the time), just define an eagerly initialized private field.

This class would also appear to be mutable, which I also suggest is NOT a good idea.

Perhaps you could answer my two questions at the start, or (maybe even better) explain to us, in English, what you're trying to do.

Winston
 
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use the phrase "repository" rather than "entity manager".

A repository can be implemented using a database connection, but if you want your repository to be in memory, an often (mis-)used solution is to make the repository a singleton.

Instead of calling EstateRepository.getInstance() in a method when you need one, you should inject the repository into the class when you construct it:
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic