Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA insert statement

Tags:

jpa

What's the correct syntax of a JPA insert statement? This might sound like an easy question but I haven't been able to find an answer.

I know how to do it from Java code but I'm looking for a way to insert objects into the database if the database was created.

Any ideas?

like image 633
javydreamercsw Avatar asked Jun 21 '10 14:06

javydreamercsw


People also ask

What are the steps to insert an entity?

You can use the Architecture Repository to insert an entity. Choose the menu item 'Insert Archifact' or 'Insert Entity' on the menubar, and click the class of entity you need in the dialog. Next, you fill in the entities details in the Add New dialog, click OK and you have your entity inserted.

What is the entity manager method to insert data?

In JPA, we can easily insert data into database through entities. The EntityManager provides persist() method to insert records.


2 Answers

There is no INSERT statement in JPA. You have to insert new entities using an EntityManager. The only statements allowed in JPA are SELECT, UPDATE and DELETE.

like image 125
frm Avatar answered Sep 20 '22 13:09

frm


Here is a good reference on persisting JPA objects using an EntityManager. As an example, this is how to insert objects using the persist method:

EntityManager em = getEntityManager(); em.getTransaction().begin();  Employee employee = new Employee(); employee.setFirstName("Bob"); Address address = new Address(); address.setCity("Ottawa"); employee.setAddress(address);  em.persist(employee);  em.getTransaction().commit(); 
like image 24
Ben Hoffstein Avatar answered Sep 21 '22 13:09

Ben Hoffstein