Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a Persistence Unit in web-app  RSS feed

 
Csaba Birtalan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'd like to ask for your help in creating a Persistence Unit in a web application. I tried to do this, but with my approach I didn't get anywhere. I created a web app and defined a Persistence Unit( tried this in an EJB module - it went all good). I used Netbeans 6.1 and JBoss AS 4.0.5. When I deploy the web-app the AS doesn't register the persistence unit defined in the persistence.xml file. I think it's a "placing" issue. I have the persistence.xml file in WEB-INF\classes\META-INF dir.

Where should I put this for the AS to register it?

Thanks in advance for your help.

Csaba Birtalan
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by persistence unit? Often in 3-tiered EJB-based applications, web-apps have limited access to the persistence layer; they contact an EJB service to make all changes for it.
 
Csaba Birtalan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your quick answer.

I wanted to use directly the persistence unit in my web application without any other service(like an ejb module or anything else).
Since there is an option in Netbeans to create a persistence unit in a web application I thought that there's nothing wrong with this approach. I have to mention that I don't really have experience in j2ee.

If you have suggestions on how should I approach this problem please let me know.

Have a nice day

Csaba Birtalan
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally you need EJBs or some other middleware to access a persistence layer. Accessing via a Web application is usually done via JDBC, rather than a true persistence mechanism. In short, you need a middle tier to define and maintain the persistence layer the web app will use, else its all just direct JDBC access.
 
Csaba Birtalan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for clearing that out for me
I'll create an ejb module then for the persistence unit.

Thank you Scott for taking the time to help.

All the best,

Csaba Birtalan
 
Mohnish Chaudhary
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not necessarily need to create the middleware EJB solution as explained to you above.
The point is that the "persistence.xml" should be placed inside the META-INF folder of the root folder containing all of your source files. I think you would have placed the "persistence.xml" under WEB-INF/META-INF. Place it under src/META-INF/ and it should work fine.

Cheers
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Csaba,

We describe the pitfalls of using the JPA entity manager directly in the web tier in the client tiers chapter of EJB 3 in Action. Although technically possible, it is not good practice. Here is a blog describing some of the pitfalls: http://weblogs.java.net/blog/ss141213/archive/2005/12/dont_use_persis_1.html if you absolutely want to do this.

I do strongly agree with the advise that you should use the entity manager only through EJB 3 or some other middleware tier technology like Spring. Using EJB 3 through a few annotations is particularly easy.

Best regards,
Reza
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!