环境:
我正在使用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;
}
}
答案 0 :(得分:1)
您没有保存任何数据,因为您没有连接到任何数据库。您需要的要求是:
<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或Persistence Wiki。
的方法正如您现在所做的那样,您的对象只是存在于应用的上下文中。
答案 1 :(得分:1)
首先,安装MySQL:
sudo apt-get install mysql-server mysql-client
下载MySQL JDBC driver(即Connector / J)。如果您正在编写桌面应用程序,只需使用Maven将其添加到您的maven项目中。如果您正在编写Web应用程序,请将驱动程序添加到服务器的类路径中(每个服务器都有所不同,因此请对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>