实际上我已经创建了用于在mysql数据库中创建表的Entity Beans,使用entityfactory的事务类一切正常。
现在我需要这个功能......当我在应用程序服务器JBOSS中部署EAR文件时,必须检查表中是否存在表...如果它们不存在......必须创建表格自动..当然我已经在persistence.xml中指定了这个函数hibernate.hbm2ddl.auto = update。但仍然没有创建数据库中不存在的任何表,并且在运行应用程序时也没有抛出任何错误服务器它只创建mysql默认表..但不是开发的那些,在这里我给我的代码:
package com.ernst.persistenceImpl.beanImpl;
import com.ernst.persistenceAPI.beanAPI.NrKreiseBeanAPI;
import com.ernst.persistenceAPI.localBeanAPI.NrKreiseLocalBeanAPI;
import com.ernst.persistenceAPI.remoteBeanAPI.NrKreiseRemoteBeanAPI;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Basic;
import java.io.Serializable;
@Entity
@Table(name = "NR_KREISE")
public class NrKreiseBean implements NrKreiseBeanAPI, NrKreiseLocalBeanAPI,
NrKreiseRemoteBeanAPI, Serializable {
@Id
private int nr_kreise_id;
@Basic
private int fk_nr_typ;
@Basic
private int data_ref_von;
@Basic
private int data_ref_bis;
@Basic
private int data_ref;
@Basic
private int msg_ref_von;
@Basic
private int msg_ref_bis;
@Basic
private int msg_ref;
@Basic
private String desc;
public int getNr_kreise_id() {
return nr_kreise_id;
}
public void setNr_kreise_id(int nr_kreise_id) {
this.nr_kreise_id = nr_kreise_id;
}
public int getFk_nr_typ() {
return fk_nr_typ;
}
public void setFk_nr_typ(int fk_nr_typ) {
this.fk_nr_typ = fk_nr_typ;
}
public int getData_ref_von() {
return data_ref_von;
}
public void setData_ref_von(int data_ref_von) {
this.data_ref_von = data_ref_von;
}
public int getData_ref_bis() {
return data_ref_bis;
}
public void setData_ref_bis(int data_ref_bis) {
this.data_ref_bis = data_ref_bis;
}
public int getData_ref() {
return data_ref;
}
public void setData_ref(int data_ref) {
this.data_ref = data_ref;
}
public int getMsg_ref_von() {
return msg_ref_von;
}
public void setMsg_ref_von(int msg_ref_von) {
this.msg_ref_von = msg_ref_von;
}
public int getMsg_ref_bis() {
return msg_ref_bis;
}
public void setMsg_ref_bis(int msg_ref_bis) {
this.msg_ref_bis = msg_ref_bis;
}
public int getMsg_ref() {
return msg_ref;
}
public void setMsg_ref(int msg_ref) {
this.msg_ref = msg_ref;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Persistence.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="DefaultDS" transaction-type="JTA">
<jta-data-source>java:/DefaultDS</jta-data-source>
<class>com.ernst.persistenceImpl.beanImpl.NrKreiseBean</class>
<properties>
<property name="jboss.entity.manager.factory.jndi.name"
value="persistence-units/DefaultDS" />
<property name="hibernate.ejb.cfgfile"
value="WEB-INF.classes.META-INF.hibernate.cfg.xml" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/lagerstandnew" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="sekhar" />
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection"
value="class, hbm" />
<property name="hibernate.hbm2ddl.auto"
value="create" />
<!-- SQL stdout logging -->
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
任何人都可以告诉我如何做到这一点,或者请告诉我,如果我在上述文件中做错了什么。
先谢谢。
最诚挚的问候, 拉加。
答案 0 :(得分:1)
也许您的表实际上是在DefaultDS指向的数据库中创建的?在标准的JBoss环境中,这是一个HypersonicSQL数据库。我将从您的配置中删除所有DefaultDS引用。
答案 1 :(得分:0)
(...)当我运行应用程序服务器时,它只创建mysql默认表而不是开发的默认表
除非您覆盖hsqldb-ds.xml
文件,否则您无法连接到您认为自己的数据库。
在JBoss上部署应用程序时,持久性提供程序正在使用JTA数据源,该数据源的全局JNDI名称由以下元素指定:
<jta-data-source>java:/DefaultDS</jta-data-source>
而不是使用特定于hibernate properties
配置的内置连接池。
如果要连接到MySQL数据库,
persistence.xml
中使用此数据源。 对于步骤#1,请参阅Chapter 8. Using other Databases(它们提供基于MySQL的示例)。
对于步骤#2,典型的persistence.xml
将是(假设MySQL数据源为MySqlDS
):
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="DefaultDS" transaction-type="JTA">
<jta-data-source>java:/MySqlDS</jta-data-source>
<class>com.ernst.persistenceImpl.beanImpl.NrKreiseBean</class>
<properties>
<property name="hibernate.ejb.cfgfile"
value="WEB-INF.classes.META-INF.hibernate.cfg.xml" />
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection"
value="class, hbm" />
<property name="hibernate.hbm2ddl.auto"
value="create" />
<!-- SQL stdout logging -->
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
答案 2 :(得分:0)
以下persistence.xml完美运行
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="lagerstand_local" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>dataSource</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="com.ernst.ernstWeb.mySqlDialectExtended.MySqlDialectExtended"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/lagerstand?autoReconnect=true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="sekhar"/>
<property name="hibernate.archive.autodetection" value="hbm, class"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.cache.use_structured_entries" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="false"/>
<property name="use_sql_comments" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>