gpt4 book ai didi

java - 数据未插入数据库 JPA+Hibernate

转载 作者:行者123 更新时间:2023-11-30 11:56:52 24 4
gpt4 key购买 nike

实际上我已经创建了用于在 mysql 数据库中创建表的实体 Bean,使用 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>

任何人都可以告诉我如何执行此操作,或者如果我在上述文件中做错了什么,请告诉我。

提前致谢。

最好的问候,王公。

最佳答案

也许您的表实际上是在 DefaultDS 指向的数据库中创建的?在 HypersonicSQL 数据库的标准 JBoss 环境中。我将从您的配置中删除所有 DefaultDS 引用。

关于java - 数据未插入数据库 JPA+Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4145178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com