Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I prevent EclipseLink (JPA 2.0) from clearing my database/table?
    primarykey
    data
    text
    <p>I'm using EclipseLink (JPA 2.0) on Netbeans 7.0 with a MySQL database - which I configured according to <a href="http://netbeans.org/kb/docs/javaee/jpa-eclipselink-screencast.html" rel="nofollow">this screencast</a>. But sometimes when I persist an Object/Entity my table get's cleared before that object persists, and I get a table with only this object (like it has been cleared). I'm only simply adding new entries in (populating) the database.</p> <p>How can I prevent this?</p> <p>Here's my code:</p> <pre><code>Movie movie = new Movie( jsondata.path("Title").getTextValue(), jsondata.path("Year").getValueAsInt(), jsondata.path("Genre").getTextValue(), jsondata.path("Plot").getTextValue(), jsondata.path("Poster").getTextValue()); persist(movie); </code></pre> <p>My persist() function:</p> <pre><code>public static void persist(Object object) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("AllmoviesPU"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); try { em.persist(object); em.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); em.getTransaction().rollback(); } em.close(); } </code></pre> <p>My Movie object/entity:</p> <pre><code>@Entity public class Movie implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @Column(name="MOVIEYEAR") private int year; private String genre; private String synopsis; private String image; public Movie(String title, int year, String genre, String synopsis, String image) { this.title = title; this.year = year; this.genre = genre; this.synopsis = synopsis; this.image = image; } public Movie() { } /* ... Getters and Setters stuff ... */ } </code></pre> <p>My persistence.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt; &lt;persistence-unit name="AllmoviesPU" transaction-type="RESOURCE_LOCAL"&gt; &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt; &lt;class&gt;allmovies.data.Movie&lt;/class&gt; &lt;properties&gt; &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://123.123.123.123:3306/psanta_allmovies"/&gt; &lt;property name="javax.persistence.jdbc.password" value="XXXXXX"/&gt; &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt; &lt;property name="javax.persistence.jdbc.user" value="psanta_allmovies"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload