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="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />

Use update instead create-drop.

2012/9/27 Manuel <develop.mash@gmail.com>
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
EMF.createEntityManager()

and persists a Object
em.persist(Worker);

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.

Regards,
Manuel



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;

@Entity
@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();
        // }
    }

    @Transient
    protected Object[] jdoDetachedState;

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

    @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) {
        this.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.persist(this);
            // em.flush();
            // em.getTransaction().commit();
        } catch (Exception e) {
            int i = 1;
        } finally {
            em.close();
        }

        em = entityManager();

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

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

        em.close();

    }

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

}


My persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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_1_0.xsd" version="1.0">
   
    <persistence-unit name="kunde" transaction-type="RESOURCE_LOCAL">
        <class>de.mash.project.server.Worker</class>

        <properties>
            <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="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>
   
</persistence>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/-tgVT993FtQJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment