Joe
Joe

Reputation: 7919

Where is it saving persistance data with Netbeans if I created a default persistance unit?

Environment:

I'm doing some test examples with javaee and it come up with something that I'd like to clear it up. I just created the Entity ToDo with a persistance unit and it works, but I don't really know where it is actually saving the data.

persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="prod" transaction-type="JTA">
        <properties>
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        </properties>
    </persistence-unit>
</persistence>

ToDo.java

...
@Entity
@NamedQuery(name=ToDo.findAll, query= "SELECT t FROM ToDo t")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ToDo {

    @Id
    @GeneratedValue
    private long id;

    public static final String PREFIX = "reminders.entity.ToDo";
    public static final String findAll = PREFIX + "findAll";

    private String caption;
    private String description;
    private int priority;

    public ToDo(String caption, String description, int priority) {
        this.caption = caption;
        this.description = description;
        this.priority = priority;
    }

    public ToDo() {
    }

    public long getId() {
        return id;
    }

    public static String getFindAll() {
        return findAll;
    }

    public String getCaption() {
        return caption;
    }

    public String getDescription() {
        return description;
    }

}

Upvotes: 0

Views: 193

Answers (2)

John Manko
John Manko

Reputation: 1908

First, install MySQL:

sudo apt-get install mysql-server mysql-client

Download the MySQL JDBC driver (ie, Connector/J). If you're writing a desktop app, just use Maven to add it to your maven project. If you're writing a web app, add the driver to your server's class path (differs for each server, so do some research on Stack Overflow).

Assuming you're writing a desktop app and eclipselink, make your persistence.xml look something like the following:

<?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="PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.myPackage.ToDo</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/TEST_DB"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="user"/>
      <property name="javax.persistence.jdbc.password" value="pass"/>
    </properties>
  </persistence-unit>
</persistence>

If your running on the server you'll need to configure a JNDI datasource in your server's admin console, then something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="PU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/MyDBConnection</jta-data-source>
        <class>com.myPackage.ToDo</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.logging.level" value="SEVERE"/>
        </properties>
    </persistence-unit>
</persistence>

Upvotes: 1

Alex S. Diaz
Alex S. Diaz

Reputation: 2667

You are not saving any data because you are not connecting to any db. The requirements you need in order to allow this are:

<property name="javax.persistence.jdbc.driver" value="some.driver"/>
<property name="javax.persistence.jdbc.url" value="location"/>
<property name="javax.persistence.jdbc.user" value="youruser"/>
<property name="javax.persistence.jdbc.password" value="yourpassword"/>

Check how to Configure the persistence xml file or the Persistence Wiki.

As you have right now, your objects just live in the context of your app.

Upvotes: 1

Related Questions