NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

How to insert data in database.

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
pvs22



Joined: 17 Mar 2011
Posts: 3

PostPosted: Mon Mar 21, 2011 10:17 pm    Post subject: How to insert data in database. Reply with quote

I'm designing an application which using jsf and jpa. I want to insert the data in the database. I tried using named query but I'm getting an error:

Exception Description: Syntax error parsing the query
line 1, column 0: unexpected token [INSERT].

When I try to do it this way:

em.getTransaction().begin();
em.persist(objDefectInfo);
em.getTransaction().commit();

I get the following error:
Cannot use an EntityTransaction while using JTA.

I'm really confused about the way to insert data in the database.
Back to top
gusto2



Joined: 22 Mar 2011
Posts: 12

PostPosted: Tue Mar 22, 2011 1:49 pm    Post subject: Re: How to insert data in database. Reply with quote

pvs22 wrote:
I'm designing an application which using jsf and jpa. I want to insert the data in the database. I tried using named query but I'm getting an error:

Exception Description: Syntax error parsing the query
line 1, column 0: unexpected token [INSERT].

When I try to do it this way:

em.getTransaction().begin();
em.persist(objDefectInfo);
em.getTransaction().commit();

I get the following error:
Cannot use an EntityTransaction while using JTA.

I'm really confused about the way to insert data in the database.


I assume (!) you use a JSF bean to insert the data and an application server to run your application. If the bean is marked as an ejb (@EJB, @Stateless, @Statefull) and the data source is a JTA or XA datasource, the default transaction management is container management, so the J2EE server starts and commits the transaction, so you don't worry where your transaction starts / ends and leave it in good hands of you app server (omit the begin() and commit() methods).

Or you can mark the bean or method as bean managed (@TransactionManagement(TransactionManagementType.BEAN)
) or use a non-jta datasource, then you have to take care when to start / commit your transaction.

carpe diem
Gabriel
Back to top
Naum Dicovski
Posted via mailing list.





PostPosted: Wed Mar 23, 2011 9:12 pm    Post subject: Re: How to insert data in database. Reply with quote

Hi,

Basically, the named queries are used for gathering information from the
database and not for performing DML queries.

The other exception that you get is about the transaction itself. If you
are using EJB to back the JSF, the bean has transaction integrated
transparently (unless you explicitly specify that you don't want to use
JTA). In fact JTA stands for Java Transaction API.
So, when you are using EJBs, you don't need (and obviously can not use)
explicitly declared transaction. The code should work if you remove the
"em.getTransaction().begin();" and "em.getTransaction().commit();" from
the code.
Each EJB call is executed as one transaction which is handled by the EJB
container implicitly. So if you have multiple JPA operations in one EJB
method, then they will all be either commited, or rolled back.


On 03/21/2011 11:17 PM, pvs22 wrote:
Quote:
I'm designing an application which using jsf and jpa. I want to insert the data in the database. I tried using named query but I'm getting an error:

Exception Description: Syntax error parsing the query
line 1, column 0: unexpected token [INSERT].

When I try to do it this way:

em.getTransaction().begin();
em.persist(objDefectInfo);
em.getTransaction().commit();

I get the following error:
Cannot use an EntityTransaction while using JTA.

I'm really confused about the way to insert data in the database.



Back to top
pvs22



Joined: 17 Mar 2011
Posts: 3

PostPosted: Wed Mar 30, 2011 6:36 am    Post subject: Re: How to insert data in database. Reply with quote

Thanks gusto2 and Naum.

I'm really new to JavaEE, Netbeans and JPA development. I resolved the error just by using em.persist().
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo