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

525 views
Skip to first unread message

Manuel

unread,
Sep 27, 2012, 8:26:44 PM9/27/12
to google-we...@googlegroups.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>

Juan Pablo Gardella

unread,
Sep 27, 2012, 8:39:36 PM9/27/12
to google-we...@googlegroups.com
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 <develo...@gmail.com>

--
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-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Manuel

unread,
Sep 27, 2012, 9:42:16 PM9/27/12
to google-we...@googlegroups.com
Hi,

thanks for your reply. I see...

Btw changeing it to "update" doesnt have any effect.

The data I persists isnt saved to my local database.
It seems like it doesnt really matter what configuration I got in the persistence.xml.
Even when I set an invalid password, I still can persists data / read data.
2012/9/27 Manuel <develo...@gmail.com>
To unsubscribe from this group, send email to google-web-toolkit+unsub...@googlegroups.com.

Juan Pablo Gardella

unread,
Sep 27, 2012, 10:44:23 PM9/27/12
to google-we...@googlegroups.com
Hi,

Try next in Hibernate forum. Perhaps you are not working inside a transaction. Try work inside of a transaction and commit it.

Cheers

2012/9/27 Manuel <develo...@gmail.com>

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Manuel

unread,
Sep 27, 2012, 11:06:14 PM9/27/12
to google-we...@googlegroups.com
Hey,

thats some code from my entity.persist() method:


            // em.getTransaction().begin();
            em.persist(this);
            // em.flush();
            // em.getTransaction().commit();

I already uncommented the code and tried that, if thats what you meant by using a transaction.

Regards
2012/9/27 Manuel <develo...@gmail.com>
2012/9/27 Manuel <develo...@gmail.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.

Manuel

unread,
Sep 28, 2012, 4:26:54 AM9/28/12
to google-we...@googlegroups.com
Hey again,

I still havent found a solution...

Heres one more Info:
I get warning when creating my EntityManagerFactory:
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.StaffView was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.AppController was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.MashService was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.server.MashServiceImpl was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.MainPage was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.WorkerProxy was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.WorkerRequestFactoryDeobfuscatorBuilder was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.presenter.Presenter was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,507 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.ExpensesRequestFactoryDeobfuscatorBuilder was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,633 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.StockPrice was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,633 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.ExpensesRequestFactory was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,633 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.WorkerRequestFactory was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,633 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.MashServiceAsync was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,633 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.WorkerRequest was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,633 WARN  [DataNucleus.MetaData] - Class de.mash.project.client.TabContent was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,633 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.EmployeeProxy was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,649 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.FieldVerifier was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,649 WARN  [DataNucleus.MetaData] - Class de.mash.project.server.EMF was specified in persistence-unit kunde but not annotated, so ignoring
10:20:03,649 WARN  [DataNucleus.MetaData] - Class de.mash.project.shared.EmployeeRequest was specified in persistence-unit kunde but not annotated, so ignoring


So it looks like datanucleus is used instead of my db... tough i dont exactly know what this nucleus does.
I tried to disable it via project properties, but after that i had some errors inside my project because he deleted jars etc...

Manuel

unread,
Sep 28, 2012, 4:44:45 AM9/28/12
to google-we...@googlegroups.com
Sorry for posting several times.
But heres the error message from EntityManagerFactory emfInstance = Persistence.createEntityManagerFactory("kunde");

Explicit persistence provider error(s) occurred for "kunde"
after trying the following discovered implementations:
org.datanucleus.api.jpa.PersistenceProviderImpl,
org.hibernate.ejb.HibernatePersistence from provider:
org.hibernate.ejb.HibernatePersistence

Manuel

unread,
Sep 28, 2012, 11:48:45 PM9/28/12
to google-we...@googlegroups.com
Hey,

here an update on my topic.

I could solve the problem.


I created a new Project without App Engine. Now the persistence.xml gets used like expected.
App Engine caused, that datanucleus instead of hibernate were used.

Regards
Reply all
Reply to author
Forward
0 new messages