Hibernate with Glassfish 4.1

时间:2015-05-26 00:16:17

标签: java hibernate glassfish hibernate-mapping jpa-2.1

我已经在Glassfish 4.1上设置了Hibernate,但我遇到了持久性问题。 我能够读取数据,但无法写入BD (更改似乎未被提交)。

我当前的persistent.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://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="myPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/myDataSource</jta-data-source>
    <properties>
      <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
      <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
      <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
    </properties>
  </persistence-unit>
</persistence>

Glassfish上的我的连接池配置是:

<jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" steady-pool-size="2" name="myPool" res-type="javax.sql.DataSource">
  <property name="TrustServerCertificate" value="false"></property>
  <property name="User" value="sa"></property>
  <property name="LastUpdateCount" value="true"></property>
  <property name="ResponseBuffering" value="adaptive"></property>
  <property name="URL" value="jdbc:sqlserver://server\bd"></property>
  <property name="XopenStates" value="false"></property>
  <property name="PacketSize" value="8000"></property>
  <property name="Password" value="mypass"></property>
  <property name="ApplicationName" value="Microsoft JDBC Driver for SQL Server"></property>
  <property name="DatabaseName" value="MyDB"></property>
  <property name="Encrypt" value="false"></property>
  <property name="LockTimeout" value="-1"></property>
  <property name="SendStringParametersAsUnicode" value="true"></property>
  <property name="MultiSubnetFailover" value="false"></property>
  <property name="ApplicationIntent" value="readwrite"></property>
  <property name="LoginTimeout" value="15"></property>
  <property name="WorkstationID" value="My-MacBook-Pro.local"></property>
  <property name="ServerName" value="xpto"></property>
  <property name="PortNumber" value="1433"></property>
  <property name="SelectMethod" value="direct"></property>
  <property name="SendTimeAsDatetime" value="true"></property>
</jdbc-connection-pool>

数据源配置:

<jdbc-resource pool-name="myPool" jndi-name="jdbc/myDataSource"></jdbc-resource>

我的EJB看起来像这样:

@PersistenceContext
private EntityManager em;

public void updateUser(User u) {
        em.merge(u);
}

知道如何解决这个问题吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

请您尝试以下配置:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    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_2_0.xsd">

    <persistence-unit name="YOUR_PERSISTANCE_NAME" transaction-type="RESOURCE_LOCAL">
        <provider>YOUR_PROVIDER</provider>

        <!-- ENTITIES -->
        <class>com.company.project....EntityA</class>
        <class>com.company.project....EntityB</class>
        <class>com.company.project....EntityC</class>

        <properties>
            <property name="javax.persistence.jdbc.url" value="YOUR_URL_TO_DB" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="USER" />
            <property name="javax.persistence.jdbc.password" value="PASS" />
            <property name="eclipselink.logging.level" value="INFO" />
            <property name="eclipselink.target-database" value="PostgreSQL" />
        </properties>
    </persistence-unit>

答案 1 :(得分:1)

在我的情况下,我使用tomcat运行Hibernate 5并在更改为glassfish 4.1时停止工作

原因是最古老的jboss-logging.jar:&#34; YOUR_GLASSFISH_FOLDER / glassfish / modules&#34;

为什么呢? hibernate 5依赖于最新版本的jboss-logging,即使你在POM文件中声明了最新版本,glassfish也会使用最旧的版本。实际上我正在使用:

{{1}}

然后我下载并替换模块路径中的旧.jar并重新开始工作,我花了2天时间尝试解决这个问题,希望它有助于解决未来的问题= D

我使用此链接来帮助我:https://medium.com/@mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz