我希望我的JavaEE容器自动创建或更新表。我有一个EJB项目和Dbms我使用MySQL。我的应用服务器是Glassfish。我使用Hibernate作为提供者。但表没有创建。如果我手动创建表一切正常(持久,合并,删除)。这是我的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="AVS-WEB" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/__avs</jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/avs" />
<property name="javax.persistence.jdbc.user" value="*****" />
<property name="javax.persistence.jdbc.password" value="******" />
<!--Hibernate properties -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
如果没有可用的表格,这是我的例外。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表&#39; avs.kunde&#39;不存在
这是我的班级昆德
package de.abs.ejb.bean;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="kunde")
public class Kunde implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(
name="KUNDE_ID_GENERATOR",
sequenceName="AVS_KUNDE",
schema="AVS",
allocationSize=1,
initialValue=1)
@GeneratedValue(
strategy=GenerationType.SEQUENCE,
generator="KUNDE_ID_GENERATOR")
private Long id;
private String email;
private Long kundennummer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getKundennummer() {
return kundennummer;
}
public void setKundennummer(Long kundennummer) {
this.kundennummer = kundennummer;
}
}
答案 0 :(得分:0)
首先要尝试将update
修改为create
中此行中的persistence.xml
:
<property name="hibernate.hbm2ddl.auto" value="create" />
与在数据库中创建表格时相比,您可以将其更改为更新。