Arun.
Persistence.createEntityManagerFactory always creates a resource-local EMF. You
MUST use EntityTransaction with such EMs to commit the changes (or may be even
make them visible to the query). UserTransaction has no effect on such EMs.
Regards,
-marina
Arun Gupta wrote:
> Attached PU and inlined the code.
>
> -- cut here --
> @Resource
> private javax.transaction.UserTransaction utx;
>
> /** Processes requests for both HTTP <code>GET</code> and
> <code>POST</code> methods.
> * @param request servlet request
> * @param response servlet response
> */
> protected void processRequest(HttpServletRequest request,
> HttpServletResponse response)
> throws ServletException, IOException {
> response.setContentType("text/html;charset=UTF-8");
> PrintWriter out = response.getWriter();
> beginHTML(out);
> try {
>
> utx.begin();
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("JavaLibrary1PU");
> out.println("<h1>JPA Hello World!</h1>");
>
> EntityManager em = emf.createEntityManager();
> Company c = new Company("1", "AAA Co", (float)10.0,
> (float)2.0, (float)10.0, "Jun 15, 2007");
> em.persist(c);
> out.println("Persisting to the source ...");
> out.println("and now committed!");
>
> Company c2 = (Company) em.createQuery(
> "select c from Company c where c.id = :id")
> .setParameter("id", "1").getSingleResult();
> utx.commit();
> System.out.println("Query returned: " + c2);
> // for begin
> } catch (NotSupportedException ex) {
> ex.printStackTrace();
> } catch (SystemException ex) {
> ex.printStackTrace();
> // now for commit
> } catch (RollbackException ex) {
> ex.printStackTrace();
> } catch (SecurityException ex) {
> ex.printStackTrace();
> } catch (IllegalStateException ex) {
> ex.printStackTrace();
> } catch (HeuristicMixedException ex) {
> ex.printStackTrace();
> } catch (HeuristicRollbackException ex) {
> ex.printStackTrace();
> }
>
> endHTML(out);
> }
>
> void beginHTML(PrintWriter out) {
> out.println("<html>");
> out.println("<head>");
> out.println("<title>JPA Hello World</title>");
> out.println("</head>");
> out.println("<body>");
> }
>
> void endHTML(PrintWriter out) {
> out.println("</body>");
> out.println("</html>");
> out.close();
> }
> -- cut here --
>
> One more thing I'd like to point out is that 8 catch clauses to begin
> and commit a TX seems very clumsy to me.
>
> -Arun
>
> Lance J. Andersen wrote:
>
>> Arun,
>>
>> could u please forward your code and i will go through it, it will be
>> easier that way and i will be happy to help u out.
>>
>> -lance
>>
>> Arun Gupta wrote:
>>
>>> Lance,
>>>
>>> cil
>>>
>>>> For simple examples using a Java SE application with Java
>>>> Persistence (the JSP part are not covered but I can help you if needed)
>>>>
>>>> http://weblogs.java.net/blog/lancea/archive/2007/06/using_java_web.html
>>>
>>> This is a recent entry and has way too much details for HW.
>>>
>>>> https://glassfish.dev.java.net/javaee5/persistence/persistence-example.html
>>>>
>>>> http://www.netbeans.org/kb/55/persistence-j2se.html
>>>>
>>>> This is a pretty basic example using a simple web app
>>>> http://www.netbeans.org/kb/55/persistence.html
>>>
>>>
>>> Bullet#5 says "default data source jdbc/sample". This is not valid
>>> for GlassFish V2 b50/NB 5.5.1. My code is now:
>>>
>>> -- cut here --
>>> utx.begin();
>>> EntityManagerFactory emf =
>>> Persistence.createEntityManagerFactory("JavaLibrary1PU");
>>> out.println("<h1>JPA Hello World!</h1>");
>>>
>>> EntityManager em = emf.createEntityManager();
>>> Company c = new Company("1", "AAA Co", (float)10.0,
>>> (float)2.0, (float)10.0, "Jun 15, 2007");
>>> em.persist(c);
>>> out.println("Persisting to the source ...");
>>> out.println("and now committed!");
>>>
>>> Company c2 = (Company) em.createQuery(
>>> "select c from Company c where c.id = :id")
>>> .setParameter("id", "1").getSingleResult();
>>> utx.commit();
>>> -- cut here --
>>>
>>> and giving the following exception:
>>>
>>> -- cut here --
>>> Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0 (Build
>>> b50-beta3 (06/06/2007))):
>>> oracle.toplink.essentials.exceptions.DatabaseException
>>> Internal Exception: java.sql.SQLException: Table/View 'COMPANY' does
>>> not exist.
>>> Error Code: -1
>>> Call: SELECT ID, CHANGE, PERCENTCHANGE, PRICE, LASTUPDATED,
>>> COMPANYNAME FROM COMPANY WHERE (ID = CAST (? AS VARCHAR(32672) ))
>>> bind => [1]
>>> Query: ReportQuery(sample.Company)
>>> at
>>> oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:319)
>>>
>>> at
>>> oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:566)
>>>
>>> at
>>> oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:452)
>>>
>>> -- cut here --
>>>
>>> I'm getting this inspite of persistence.xml containing
>>> drop-and-create-tables.
>>>
>>>>
>>>>
>>>> I think Mitesh and Marina answered your other questions. If not,
>>>> perhaps post your sample app and we can help you clean it up.
>>>
>>> Yes, I'd still like my app to work and understand the whys/wheres of
>>> getting JPA to work :)
>>>
>>> -Arun
>>>
>>>>
>>>> Regards
>>>> lancde
>>>>
>>>> Arun Gupta wrote:
>>>>
>>>>> On java.net, are you talking about [1] ? That only got me where I'm
>>>>> right now but still missing why my application is not able to
>>>>> persist the data in database. I've not tried the app as is because
>>>>> I'd like something really simple.
>>>>>
>>>>> I search "JPA" on netbeans.org and get pointers to (in order):
>>>>>
>>>>> 1). http://wiki.netbeans.org/wiki/view/UsingJSFAndFaceletsInNetbeans
>>>>> Certainly does not qualify as HW!
>>>>>
>>>>> 2). Second link is:
>>>>> http://wiki.netbeans.org/wiki/view/UsingHibernateWithJPA
>>>>>
>>>>> This requires an additional software (Hibernate) and does not work
>>>>> out-of-the-box (using TopLink). I don't qualify this as HW!
>>>>>
>>>>> 3rd & 4th link are FishEye.
>>>>>
>>>>> 5th link is: http://www.netbeans.org/kb/55/persistence.html This
>>>>> uses JSF and I don't qualify this as HW.
>>>>>
>>>>> Rest of the links are FishEye or not directly relevant.
>>>>>
>>>>> I'm looking for a plain-vanilla example of JPA where no other
>>>>> "frills" are required or used. That would be using JPA in an
>>>>> index.jsp (without any frameworks) using TopLink on GlassFish
>>>>> promoted build.
>>>>>
>>>>> Googling web or netbeans.org does not seem to point me to such a
>>>>> resource. JavaEE5 tutorial also requires to understand the Duke's
>>>>> Bookstore scenario.
>>>>>
>>>>> In order to understand the technology, I'd like to keep the
>>>>> additional requirements to zero or minimal.
>>>>>
>>>>> Can you point me to such a blog or article ?
>>>>>
>>>>> Either way I'd like to understand why my application is not able to
>>>>> create database tables even though I'm getting the error:
>>>>>
>>>>> -- cut here --
>>>>> JDO76609: Got SQLException executing statement "CREATE TABLE
>>>>> COMPANY (ID VARCHAR(255) NOT NULL, CHANGE FLOAT, PERCENTCHANGE
>>>>> FLOAT, PRICE FLOAT, LASTUPDATED VARCHAR(255), COMPANYNAME
>>>>> VARCHAR(255), PRIMARY KEY (ID))": java.sql.SQLException: Table/View
>>>>> 'COMPANY' already exists in Schema 'APP'.
>>>>> -- cut here --
>>>>>
>>>>> persistence.xml has
>>>>> "jdbc:derby://localhost:1527/sample;create=true" and when I connect
>>>>> using NetBeans to this URL, the table is not shown. And of course,
>>>>> then there is no data there as well :) What am I missing ?
>>>>>
>>>>> [1]
>>>>> https://glassfish.dev.java.net/javaee5/persistence/persistence-example.html#Using_in_Java_EE
>>>>>
>>>>>
>>>>> -Arun
>>>>>
>>>>> Lance J. Andersen wrote:
>>>>>
>>>>>> Hi Arun,
>>>>>>
>>>>>> There are a bunch of blogs similar to what u are suggesting
>>>>>> already and a few good articles on netbeans.org.
>>>>>>
>>>>>> download the app from the blog on java.net i just put together on
>>>>>> JPA, netbeans and webstart, perhaps that will help answer some of
>>>>>> your questions
>>>>>>
>>>>>> Regards
>>>>>> Lance
>>>>>>
>>>>>> Arun Gupta wrote:
>>>>>>
>>>>>>> cil
>>>>>>>
>>>>>>> Marina Vatkina wrote:
>>>>>>>
>>>>>>>> Sorry, I means if you want to use EntityTransaction, you need to
>>>>>>>> have EMF injected/lookedup.
>>>>>>>
>>>>>>> I'm just trying to create a JPA Hello World using NetBeans IDE
>>>>>>> and then blog about it and I'd like to do it the easiest way :)
>>>>>>> Injection, to me, is better than hand-creating a TX!
>>>>>>>
>>>>>>>>
>>>>>>>> Do you mean you don't see the data in the table or the table
>>>>>>>> being created?
>>>>>>>
>>>>>>> Yep, neither the table and of course no data.
>>>>>>>
>>>>>>>>
>>>>>>>> To get the former, you need transactions to commit the changes,
>>>>>>>> to get the latter, you need to use provider-specific tools. Do
>>>>>>>> you use GlassFish with Toplink Essentials?
>>>>>>>
>>>>>>> Yes
>>>>>>>
>>>>>>>> If yes, and you use CLI to deploy your app, you can just specify
>>>>>>>> --dropandcreatetables=true on the asadmin
>>>>>>>
>>>>>>> persistence.xml has:
>>>>>>>
>>>>>>> <property name="toplink.ddl-generation" value="create-tables"/>
>>>>>>>
>>>>>>> And I'd like to the deployment from NetBeans, not CLI.
>>>>>>>
>>>>>>> What else ?
>>>>>>>
>>>>>>> -Arun
>>>>>>>
>>>>>>>> deploy. Otherwise, you'd need to set the provider-specific
>>>>>>>> properties in the persistence.xml file.
>>>>>>>>
>>>>>>>> HTH,
>>>>>>>> -marina
>>>>>>>>
>>>>>>>> Arun Gupta wrote:
>>>>>>>>
>>>>>>>>> Cool! Changing my code to:
>>>>>>>>>
>>>>>>>>> -- cut here --
>>>>>>>>> @PersistenceUnit(unitName="JavaLibrary1PU")
>>>>>>>>> private EntityManagerFactory emf;
>>>>>>>>>
>>>>>>>>> EntityManager em = emf.createEntityManager();
>>>>>>>>> // em.getTransaction().begin();
>>>>>>>>> Company c = new Company("1", "AAA Co", (float)10.0,
>>>>>>>>> (float)2.0, (float)10.0, "Jun 15, 2007");
>>>>>>>>> em.persist(c);
>>>>>>>>> out.println("Persisting to the source ...");
>>>>>>>>> // em.getTransaction().commit();
>>>>>>>>> out.println("and now committed!");
>>>>>>>>> -- cut here --
>>>>>>>>>
>>>>>>>>> got away with that error and my web page now shows
>>>>>>>>>
>>>>>>>>> "Persisting to the source ... and now committed!"
>>>>>>>>>
>>>>>>>>> But I do not see the Table in the database URL identified in
>>>>>>>>> persistence.xml. What could be wrong ?
>>>>>>>>>
>>>>>>>>> -Arun
>>>>>>>>>
>>>>>>>>> Marina Vatkina wrote:
>>>>>>>>>
>>>>>>>>>> Arun,
>>>>>>>>>>
>>>>>>>>>> The exceptions are right - you can't mix and match the access
>>>>>>>>>> types. You need to either inject an EMF via @PersistenceUnit,
>>>>>>>>>> and then create an EM or use UserTransaction for the
>>>>>>>>>> transaction boundaries.
>>>>>>>>>>
>>>>>>>>>> regards,
>>>>>>>>>> -marina
>>>>>>>>>>
>>>>>>>>>> Arun Gupta wrote:
>>>>>>>>>>
>>>>>>>>>>> Ok, I figured out how to set the transaction type (manually
>>>>>>>>>>> edit persistence.xml). But now I'm getting:
>>>>>>>>>>>
>>>>>>>>>>> -- cut here --
>>>>>>>>>>> Exception Description: Cannot use an EntityTransaction while
>>>>>>>>>>> using JTA.
>>>>>>>>>>> at
>>>>>>>>>>> oracle.toplink.essentials.internal.ejb.cmp3.transaction.JTATransactionWrapper.getTransaction(JTATransactionWrapper.java:66)
>>>>>>>>>>>
>>>>>>>>>>> at
>>>>>>>>>>> oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.getTransaction(EntityManagerImpl.java:340)
>>>>>>>>>>>
>>>>>>>>>>> at
>>>>>>>>>>> com.sun.enterprise.util.EntityManagerWrapper.getTransaction(EntityManagerWrapper.java:1094)
>>>>>>>>>>>
>>>>>>>>>>> -- cut here --
>>>>>>>>>>>
>>>>>>>>>>> Here is my code for a WebApplication:
>>>>>>>>>>> -- cut here --
>>>>>>>>>>> @PersistenceContext(unitName="JavaLibrary1PU")
>>>>>>>>>>> EntityManager em;
>>>>>>>>>>> em.getTransaction();
>>>>>>>>>>> Company c = new Company("1", "AAA Co", (float)10.0,
>>>>>>>>>>> (float)2.0, (float)10.0, "Jun 15, 2007");
>>>>>>>>>>> em.persist(c);
>>>>>>>>>>> out.println("Persisting to the source ...");
>>>>>>>>>>> em.getTransaction().commit();
>>>>>>>>>>> out.println("and now committed!");
>>>>>>>>>>> -- cut here --
>>>>>>>>>>>
>>>>>>>>>>> What is missing ?
>>>>>>>>>>>
>>>>>>>>>>> -Arun
>>>>>>>>>>>
>>>>>>>>>>> Arun Gupta wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Here is a great error message I see when deploying an error:
>>>>>>>>>>>>
>>>>>>>>>>>> -- cut here --
>>>>>>>>>>>> Deploying application in domain failed; Deployment Error --
>>>>>>>>>>>> The persistence-context-ref-name [sample.NewServlet/em] in
>>>>>>>>>>>> module [C:\Users\Arun Gupta\WebApplication5\build\web]
>>>>>>>>>>>> resolves to a persistence unit called [JavaLibrary1PU] which
>>>>>>>>>>>> is of type RESOURCE_LOCAL. Only persistence units with
>>>>>>>>>>>> transaction type JTA can be used as a container managed
>>>>>>>>>>>> entity manager. Please verify your application.
>>>>>>>>>>>> -- cut here --
>>>>>>>>>>>>
>>>>>>>>>>>> Little detailed but very clearly conveys what is possibly
>>>>>>>>>>>> wrong in the application. Now how do I fix it when
>>>>>>>>>>>> generating this PU in NB ?
>>>>>>>>>>>>
>>>>>>>>>>>> -Arun
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>
>>>>>>>>>> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>>>>>>>>>> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>
>>>>>>>> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>>>>>>>> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>>>>>> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>>>> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net