我在Netbeans 7.0上使用EclipseLink(JPA 2.0)和MySQL數據庫 - 我根據this screencast配置了該數據庫。但有時當我堅持一個對象/實體時,在該對象持久存在之前,我的表被清除,並且我得到一個僅包含此對象的表(例如它已被清除)。我只是簡單地在(填充)數據庫中添加新條目。如何防止EclipseLink(JPA 2.0)清除我的數據庫/表?
我該如何預防?
這裏是我的代碼:
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);
我的堅持()函數:
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();
}
我的電影對象/實體:
@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 ... */
}
我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="AllmoviesPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>allmovies.data.Movie</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://123.123.123.123:3306/psanta_allmovies"/>
<property name="javax.persistence.jdbc.password" value="XXXXXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="psanta_allmovies"/>
</properties>
</persistence-unit>
</persistence>
你在別的地方發佈過這個問題嗎?完成(工作!!)數據庫登錄設置*公共*?難道這不就是一些不時縮短桌面的小丑嗎?嘗試更改您的密碼,不要在萬維網中發佈數據庫連接詳細信息... – BalusC 2011-05-19 02:59:49