Wildfly上的Hibernate OGM不存储实体

时间:2017-11-26 20:22:00

标签: java mongodb wildfly-10 hibernate-ogm

我正在尝试使用MongoDb在Wildfly 10.1上使用Hibernate OGM 5.1存储实体。什么都没有存储。

当持久性代码运行时,它没有异常。 Wildfly日志中没有警告或例外。日志表明已成功连接到mongodb服务器,持久性代码中的日志语句报告会话已保存。 mongodb日志报告没有错误。但是,身份验证数据库中不会保存任何内容。 Sessions集合不存在。

我的persistence.xml:

<?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="mongo-ogm-auth">
    <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
    <class>org.secomm.authenticator.persistence.ServerVault</class>
    <class>org.secomm.authenticator.persistence.Sessions</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.ogm.datastore.database" value="authentication"/>
      <property name="hibernate.ogm.datastore.host" value="xxxx"/>
      <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
      <property name="hibernate.ogm.datastore.username" value="xxxx" />
      <property name="hibernate.ogm.datastore.password" value="xxxx"/>
      <property name="hibernate.ogm.mongodb.authentication_database" value="authentication"/>
    </properties>
  </persistence-unit>
</persistence>

我的实体:

@Entity
public class Sessions {

    /**
     * 
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private ObjectId id;

    /**
     * 
     */
    private int sessionId;

    /**
     * 
     */
    private long authToken;

    /**
     * 
     */
    private long timestamp;

    /* Getters and setters */

}

我的持久性代码:

/**
 * 
 */
@PersistenceUnit(name="mongo-ogm-auth")
private EntityManagerFactory entityManagerFactory;

private void storeSession(SessionContext context) {

    try {
        logger.trace("Storing the session info");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        Sessions session = new Sessions();
        session.setSessionId(context.getSessionId());
        session.setAuthToken(0);
        session.setTimestamp(System.currentTimeMillis() / 1000);

        entityManager.persist(session);
        entityManager.close();
        logger.trace("Session info stored");
    }
    catch (Exception e) {
        logger.warn("Unknown exception - " + e.getClass().getName() + " - " + e.getLocalizedMessage());
    }

}

0 个答案:

没有答案