如果我创建了一个默认的持久性单元,它将在哪里保存 Netbeans 的持久性数据?

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

环境:

我正在用 javaee 做一些测试示例,它提出了一些我想清理它的东西。 我刚刚创建了带有持久性单元的 Entity ToDo,它可以工作,但我真的不知道它实际保存数据的位置。

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;
    }

}

您没有保存任何数据,因为您没有连接到任何数据库。允许这样做所需的要求是:

<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"/>

查看如何Configure the persistence xml file or the Persistence Wiki

正如您现在所拥有的,您的对象仅存在于您的应用程序上下文中。

首先,安装MySQL:

sudo apt-get install mysql-server mysql-client

下载MySQL JDBC driver(即Connector/J)。如果您正在编写桌面应用程序,只需使用 Maven 将其添加到您的 Maven 项目中。如果您正在编写 Web 应用程序,请将驱动程序添加到服务器的 class 路径(每个服务器都不同,因此请对 Stack Overflow 进行一些研究)。

假设您正在编写桌面应用程序和 eclipselink,使您的 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="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>

如果您的 运行 在服务器上,您需要在服务器的管理控制台中配置 JNDI 数据源,然后如下所示:

<?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>