【问题标题】:EJB/Hibernate does not access MySQL DatabaseEJB/Hibernate 不访问 MySQL 数据库
【发布时间】:2017-10-23 13:50:02
【问题描述】:

我正在尝试学习如何使用 JavaEE/EJB 和数据库持久性,我有一个基本示例,我想通过输入字段将字符串保存到数据库并读取已保存项目的列表。

我在 localhost(V5.7 社区版)上安装了 MySQL 服务器,我的测试服务器是 WildFly 10.1.0(通过 Eclipse)。整个项目是一个包含 Web 和 EJB 子项目的 EAR 容器。

我正在使用容器管理的事务,据我了解,事务是在调用方法时自动创建的,并在方法退出后立即刷新/提交。

问题是,没有数据被写入实际的数据库。但也不会抛出任何错误。我假设,实体管理器缓存所有假定的保存并在选择时直接读取它们,甚至不检查数据库。因此,当我重新启动服务器时,什么都没有。此外,当我直接查看 mysql db 时,也没有任何内容(即使在 wildfly 服务器运行时,直接在假定的插入之后)。我也尝试直接向 db 表添加一些行,但 select 也没有“看到”它们。

因此,在我看来,即使在persistence.xml 中配置了数据库,也无法访问该数据库。我试图在那里删除连接 url/用户名/密码,它实际上没有任何区别,实体管理器仍然没有抛出任何错误,一切似乎都正常。那么它究竟在哪里保存数据,我必须更改什么才能访问提供的 mysql 数据库呢?

处理程序(在 Web 项目中):

@ManagedBean(name = "handlerBean")
@SessionScoped
public class HandlerBean {

@EJB
private TodoWorkerBeanRemote worker;

private String input;

public void add() {
    if (input.compareTo("") != 0) {

        TodoBean item = new TodoBean();
        item.setText(input);
        worker.saveItem(item);

        input = "";
    }
}
...

会话 Bean/事务容器(在 EJB 项目中)

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class TodoWorkerBean implements TodoWorkerBeanRemote {

@PersistenceContext(unitName = "EnterpriseTestEJB")
private EntityManager entityManager;

public TodoWorkerBean() {
}

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // does not help
public void saveItem(TodoBean item) {

    // entityManager.joinTransaction(); <- does not help
    entityManager.persist(item); // tried .merge() as well
    // entityManager.flush(); <-does not help
}
...

实体 Bean(在 EJB 项目中)

@Entity
@Table(name = "todobean")
@NamedNativeQueries({
    @NamedNativeQuery(name = "TodoBean.getItems", query = "select * from todobean", resultClass = TodoBean.class),
    @NamedNativeQuery(name = "TodoBean.clearItems", query = "delete from todobean") })
public class TodoBean implements Serializable {

private Integer id;
private String text;
...

persistence.xml(也尝试过 hibernate.cfg.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="EnterpriseTestEJB">
    <class>de.dianasalsa.ejb.TodoBean</class>
    <properties>

        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/feedback" /> // not used
        <property name="hibernate.connection.username" value="root" /> // not used
        <property name="hibernate.connection.password" value="root" /> // not used

        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.use_sql_comments" value="true" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" /> // tried "create" as well

    </properties>

</persistence-unit>
</persistence>

日志:

....
15:42:59,602 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 66) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'EnterpriseTest.ear/EnterpriseTestEJB.jar#EnterpriseTestEJB'
15:42:59,952 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 65) WFLYCLINF0002: Started client-mappings cache from ejb container
15:42:59,973 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 66) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
15:43:00,102 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 66) Envers integration enabled? : true
15:43:00,101 INFO  [org.jboss.as.protocol] (management I/O-1) WFLYPRT0057:  cancelled task by interrupting thread Thread[management-handler-thread - 3,5,management-handler-thread]
15:43:00,604 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 66) HHH000228: Running hbm2ddl schema update
15:43:00,617 INFO  [org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl] (ServerService Thread Pool -- 66) HHH000262: Table not found: todobean
15:43:00,619 INFO  [org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl] (ServerService Thread Pool -- 66) HHH000262: Table not found: todobean
15:43:01,499 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 83) Mojarra 2.2.13.SP1 20160303-1204 für Kontext '/EnterpriseTestWeb' wird initialisiert.
15:43:02,379 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 83) WFLYUT0021: Registered web context: /EnterpriseTestWeb
15:43:02,414 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "EnterpriseTest.ear" (runtime-name : "EnterpriseTest.ear")
15:43:02,515 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
15:43:02,519 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
15:43:02,519 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 9581ms - Started 879 of 1127 services (422 services are lazy, passive or on-demand)
15:43:18,799 INFO  [org.jboss.ejb.client] (default task-2) JBoss EJB Client version 2.1.4.Final
15:43:32,763 INFO  [stdout] (default task-3) Hibernate: 
15:43:32,763 INFO  [stdout] (default task-3)     /* insert de.dianasalsa.ejb.TodoBean
15:43:32,763 INFO  [stdout] (default task-3)         */ insert 
15:43:32,763 INFO  [stdout] (default task-3)         into
15:43:32,763 INFO  [stdout] (default task-3)             todobean
15:43:32,763 INFO  [stdout] (default task-3)             (text) 
15:43:32,763 INFO  [stdout] (default task-3)         values
15:43:32,764 INFO  [stdout] (default task-3)             (?)

15:43:32,786 INFO  [stdout] (default task-3) Hibernate: 
15:43:32,786 INFO  [stdout] (default task-3)     /* TodoBean.getItems */ select
15:43:32,786 INFO  [stdout] (default task-3)         * 
15:43:32,787 INFO  [stdout] (default task-3)     from
15:43:32,787 INFO  [stdout] (default task-3)         todobean

【问题讨论】:

    标签: mysql hibernate jakarta-ee persistence.xml


    【解决方案1】:

    对不起我的英语......

    你需要配置一个 JNDI 名称“jta-data-source”,并说用“hibernate.hbm2ddl.auto”创建新的关系,比如这个例子:

    <?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="EnterpriseTestEJB"
            transaction-type="JTA">
            <jta-data-source>java:jboss/datasources/some-name</jta-data-source>
            <properties>
                <property name="hibernate.hbm2ddl.auto" value="create" />
                <property name="hibernate.show_sql" value="true" />
            </properties>
        </persistence-unit>
    </persistence>
    

    wildfly 有一个“名称服务器”,您可以使用 JNDI 名称从您的注释(如“@PersistenceContext”)中访问它。

    在 Wildfly at selfe 中,您必须使用该 JNDI 名称配置新的 MySql 连接。

    对于MySql 连接下载jdbc 驱动 并上传到WildFly。 https://dev.mysql.com/downloads/connector/j/5.1.html

    Inser MySqgl jdbc Driver

    连接将使用 JNDI 名称 (java:jboss/datasources/some-name) 命名。

    Add new MySql Connection (1)

    Add new MySql Connection (2)

    当您在 Wildfly 中运行程序时,该程序将在 Wildfly 中查找您的 MySql 连接的 JNDI 名称。

    如果您想看一个示例代码,我在这里有一个来自我的研究的示例代码: https://gitlab.com/Java_Project/JavaEE2.0

    希望它能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 2010-12-14
      • 2010-10-11
      • 2020-06-12
      • 1970-01-01
      相关资源
      最近更新 更多