If I enable "em.flush()" after "and now committed!" statement, then I
get the following error:
-- cut here --
javax.persistence.TransactionRequiredException:
Exception Description: Error marking externally managed transaction for
rollback
-- cut here --
I think something more basic is missing because I'm not even seeing the
tables getting created in the database. Any clues ?
-Arun
Mitesh Meswani wrote:
> em.persist() does not actually write object to database. The object will
> be written to database when transaction commits. If you want to force
> writing the object to database than call em.flush() before executing the
> query.
> You can get more information from the spec or perssitence tutorial
> (http://java.sun.com/javaee/5/docs/tutorial/doc/PersistenceIntro.html)
>
> -Mitesh
>
> Arun Gupta wrote:
>> Also wanted to mention that the following code:
>>
>> -- cut here --
>> 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();
>> System.out.println("Query returned: " + c2);
>> -- cut here --
>>
>> gives the following error:
>>
>> -- cut here --
>> javax.persistence.NoResultException: getSingleResult() did not
>> retrieve any entities.
>> -- cut here --
>> I wonder why no error was thrown during persist() ?
>>
>> -Arun
>>
>> gives the following error:
>>
>> Marina Vatkina wrote:
>>> Sorry, I means if you want to use EntityTransaction, you need to have
>>> EMF injected/lookedup.
>>>
>>> Do you mean you don't see the data in the table or the table being
>>> created?
>>>
>>> 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? If yes, and you use CLI to deploy
>>> your app, you can just specify --dropandcreatetables=true on the
>>> asadmin 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
>
--
Technology Evangelist
Web Technologies and Standards
Sun Microsystems, Inc.
Blog: http://blogs.sun.com/arungupta