我想设置一个基本的JPA应用程序,它在mySQL中创建一个简单的表。EclipseLink/MySQL/Glassfish - 即使连接工作正常,表格也不会创建
我使用glassfish 4.1,我创建了连接池/资源并成功从管理面板中ping数据库。
我创建了一个web应用程序项目,其中包含一个简单的实体和src/META-INF文件夹下的persistence.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="DataPersistencePU" transaction-type="JTA">
<jta-data-source>mysqlresource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
与实体文件,
@Entity
public class DataProvider implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof DataProvider)) {
return false;
}
DataProvider other = (DataProvider) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Entity.DataProvider[ id=" + id + " ]";
}
}
我也试过在/ WEB-INF移动persistence.xml中,仍无法治愈。
Glassfish日志中没有错误。 我只是想让eclipseLink工作并自动生成表格。
好的问这里明显。你是否在应用程序的某个地方创建了“EntityManager”? – Ranjeet