• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused about interfaces

 
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using JPA to persist entities into a database. I know how to use JPA but I don't understand how its interfaces work. Take the following example:



This is a very simple JSF backing bean. The PersonEntityJpaController class persists the PersonEntity entity to the db. My question is about the injected EntityManagerFactory and UserTransaction. It's my understanding that when you use these your application server (glassfish in my case) "injects" instances of these interfaces into your class. But how can you instantiate an interface? I understand that you can use an interface as a reference type but I am not sure how this works here. A segment of the PersonEntityJpaController class is below:



The UserTransaction and EntityManagerFactory are passed to the constructor and the EntityManagerFactory creates an EntityManager using its createEntityManager method. You can also see that the UserTransaction begin, commit, etc. method being called. My problem is understanding how these methods work when all these are interfaces?? Interfaces only outline methods, they don't write how they are perform (for want of a better word). So what class(s) have implemented these interfaces and their methods, and where are they?

Sorry for the long winded question!

Thanks,
Alan
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the factory returns "an instance of an interface", what it's actually doing is returning an instance of a specific class that implements the interface.

If you try logging the value of emf.getClass().getName() (for example) you'll be able to find out exactly which class is being used. But the point of interfaces is that you don't need to know this. All you need to know is that the object you've got implements that interface - your code is completely decoupled from the specific type being used. This makes your code more flexible - if the factory is changed and now returns a different type, your code continues to work.
 
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

Alan Smith wrote:Sorry for the long winded question!


No probs, but please don't put excessively long lines inside code tags. I broke up yours (line 25 in the second set) this time, but please re-read the UseCodeTags page thoroughly for details.

Thanks.

Winston
 
Alan Smith
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:When the factory returns "an instance of an interface", what it's actually doing is returning an instance of a specific class that implements the interface.

If you try logging the value of emf.getClass().getName() (for example) you'll be able to find out exactly which class is being used. But the point of interfaces is that you don't need to know this. All you need to know is that the object you've got implements that interface - your code is completely decoupled from the specific type being used. This makes your code more flexible - if the factory is changed and now returns a different type, your code continues to work.



Ok, thanks for that. I used the emf.getClass().getName() and it printed an EntityManagerFactoryWrapper class. Good to know.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic