Thursday, September 27, 2012

Re: Using JPA Hibernate persist()... Where is the Data?

Nothing related to GWT, use JPA/Hibernate forum. You are create and drop the database each time that EMF is created:

            <property name="" value="create" />
            <property name="" value="create-drop" />

Use update instead create-drop.

2012/9/27 Manuel <>
Hi everyone,

Actually I try to use GWT, Request Factory + JPA with Hibernate.
It seems to be working, but there is no data in my local database.

I can create a EntityMangerFactory  
private static final EntityManagerFactory emfInstance = Persistence.createEntityManagerFactory("kunde");[/code]

a EntityManger

and persists a Object

I can also read all my persisted Objects.

But I dont know where the data is saved. I thought, it should be saved in the postgresDB I configured in my persistence.xml?

Any help is much appreciated.
Thanks in advanced.


Here is my Code, please let me know if you need any further information.

My Entity Class
package de.mash.project.server;

import java.util.List;

import javax.jdo.annotations.Transactional;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Table(name = "Worker")
public class Worker {

    public static Worker findWorker(Long id) {
        // if (id == 0) {
        return null;
        // }
        // EntityManager em = entityManager();
        // try {
        // Worker worker = em.find(Worker.class, id);
        // return worker;
        // } finally {
        // em.close();
        // }

    protected Object[] jdoDetachedState;

    // @Id
    // @GeneratedValue(generator = "auto_increment")
    // @GenericGenerator(name = "auto_increment", strategy = "increment")
    // @Column(name = "id")
    // private int id;

    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "salary")
    private int salary;

    public Worker() {

    public Worker(String fname, String lname, int salary) {
        this.firstName = fname;
        this.lastName = lname;
        this.salary = salary;

    public Long getId() {
        return id;

    public void setId(long id) { = id;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String first_name) {
        this.firstName = first_name;

    public String getLastName() {
        return lastName;

    public void setLastName(String last_name) {
        this.lastName = last_name;

    public int getSalary() {
        return salary;

    public void setSalary(int salary) {
        this.salary = salary;

    public static Long countWorkers() {
        return 2l;

    public Integer getVersion() {
        return 1;

    public void persist() {
        EntityManager em = entityManager();
        try {
            // em.getTransaction().begin();
            // em.flush();
            // em.getTransaction().commit();
        } catch (Exception e) {
            int i = 1;
        } finally {

        em = entityManager();

        final List<Worker> list = em.createQuery("select p from Worker p").getResultList();

        for (Worker current : list) {
            System.out.println(current.getFirstName() + " " + current.getLastName() + " " + current.getSalary());



    public static final EntityManager entityManager() {
        return EMF.get().createEntityManager();


My persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns=""
    xsi:schemaLocation="" version="1.0">
    <persistence-unit name="kunde" transaction-type="RESOURCE_LOCAL">

            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/dev_mash" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="m4nu3l" />
            <property name="hibernate.default_schema" value="public" />
            <property name="" value="create" />
            <property name="" value="create-drop" />

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment