• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about interfaces

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic