JPA insert statement
Asked Answered
F

5

36

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?

Futilitarian answered 21/6, 2010 at 14:52 Comment(1)
Here is a good case where the least correct answer is marked as the proper solution. Please consider marking as valid either frm or Ben Hoffstein answer instead.Sharlasharleen
F
2

The option that looks more promising so far is using Flyway. It is a more automated way of doing it and handles the upgrade process of databases basically automatically.

Futilitarian answered 29/6, 2012 at 13:7 Comment(0)
G
36

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.

Grasso answered 21/6, 2010 at 15:0 Comment(2)
I've seen native SQL being used for this purpose, but how can this been written for all database providers? Hibernate has such a feature, you can include a file called "import.sql" in the runtime classpath of Hibernate. At the time of schema export it will execute the SQL statements contained in that file after the schema has been exported. I'm looking for some more generic approach to this as I am using Eclipselink.Futilitarian
Really!, ahh, I was trying to insert a record into database using jpa-ql persistance console in Intellij idea.Babettebabeuf
A
22

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();
Andino answered 21/6, 2010 at 15:6 Comment(2)
As I mentioned I'm aware on how to do it from Java. I'm looking for a way to use standard inserts when the database is created besides hardcoding it in java.Futilitarian
Gotcha - sorry, misunderstood the question.Andino
A
5

If you want to insert data to the database outside java you need to use native SQL. Use SQL Standard to make sure most databases can execute the script. When the application runs, JPA will make the mapping of the new data and convert it into objects when needed.

How to make sure the script works in all databases? well thats the same problem any DBA has when making Store Procedures or native queries... thats why JPA exists, to avoid making it directly in SQL, but I know sometimes is needed that way.

I suggest you to make 3 main scripts. One for Oracle, one for SQL Server (there are some issues in the date datatypes from 2005 to 2008 versions so be careful) and one for MySQL. Start your script with standard SQL and when you test it in this databases you will find some fixes you will need to do for each DBMS.

One you got it you can make a file script (*.sql) file and run it with the DB manager. If it works run the server, put the app online and the data will be integrated just fine.

Aver answered 27/6, 2012 at 17:8 Comment(1)
This is the way I ended up doing it. I was looking for a more automatic way but I had to code it to do this kind of work.Futilitarian
F
2

The option that looks more promising so far is using Flyway. It is a more automated way of doing it and handles the upgrade process of databases basically automatically.

Futilitarian answered 29/6, 2012 at 13:7 Comment(0)
L
2

No need to write a separate INSERT query in JPA. JpaRepository has inbuilt saveAndFlush() method which you can use to insert into the database. Hope this works for you.

Lifeguard answered 26/5, 2020 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.