0

我正在尝试使用 Hibernate OGM 为带有 java 1.6 和 OGM 4.1.3.Final 的 mongoDB 配置我的 Spring Web 应用程序。

对于 Session,我编写了 HibernateUtil.java,它在 JUnit 测试中运行良好,但它会抛出异常。

休眠.java

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.ogm.cfg.OgmConfiguration;

public class HibernateUtil {

    private static final Logger log = Logger.getLogger(HibernateUtil.class.getName());
    private static final SessionFactory sessionFactory;
    private static final StandardServiceRegistry serviceRegistry;

    static {
        try {
            // create a new instance of OmgConfiguration
            OgmConfiguration cfgogm = new OgmConfiguration();
            // process configuration and mapping files
            cfgogm.configure("hibernate_mongoDB.cfg.xml");
            // create the SessionFactory
            //cfgogm.getProperties();
            serviceRegistry = (StandardServiceRegistry) new StandardServiceRegistryBuilder()
                .applySettings(cfgogm.getProperties()).build();
            sessionFactory = cfgogm.buildSessionFactory(serviceRegistry);
        } catch (Exception ex) {
            log.log(Level.SEVERE, "Initial SessionFactory creation failed !",
                ex);
            throw new EdumissException(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

TestMongoDb.java

package mkcl.os.test;

import java.util.Date;
import java.util.List;

import mkcl.os.apps.edumiss.utilities.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;

public class TestMongoDB {@
    Test
    public void testMongoDB() {
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {
            Players player = new Players();
            player.setId("2");
            player.setAge((short) 24);
            player.setBirth(new Date());
            player.setName("Pankaj");
            player.setSurname("Saboo");


            session.beginTransaction();
            session.save(player);
            session.getTransaction().commit();

            String hql = "FROM Players";
            Query query = session.createQuery(hql);
            List results = query.list();
            //List results = cr.list();
            for (Object object: results) {
                System.out.println("Data : " + object);
            }


            //session.flush(); // flush happens automatically anyway

        } catch (RuntimeException re) {
            session.getTransaction().rollback();
            throw re;
        } finally {
            session.close();
        }
    }

}

玩家.java

package mkcl.os.test;

import java.util.Date;

public class Players {

    private String id;
    @Override
    public String toString() {
        return "Players [id=" + id + ", name=" + name + ", surname=" + surname
                + ", age=" + age + ", birth=" + birth + ", getId()=" + getId()
                + ", getName()=" + getName() + ", getSurname()=" + getSurname()
                + ", getAge()=" + getAge() + ", getBirth()=" + getBirth()
                + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
                + ", toString()=" + super.toString() + "]";
    }

    private String name;
    private String surname;
    private short age;
    private Date birth;

    public Players() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Players(String id, String name, String surname, short age, Date birth) {
        super();
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.birth = birth;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public short getAge() {
        return age;
    }

    public void setAge(short age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

}

玩家.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 5, 2014 4:06:20 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>

    <class name="mkcl.os.test.Players" table="PLAYERS">
        <id name="id" type="java.lang.String">
            <column name="ID" />
            <generator class="assigned" />
        </id>

        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>

        <property name="surname" type="java.lang.String">
            <column name="SURNAME" />
        </property>

        <property name="age" type="short">
            <column name="AGE" />
        </property>

        <property name="birth" type="java.util.Date">
            <column name="BIRTH" />
        </property>

    </class>

</hibernate-mapping>

hibernate_mongoDB.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> -->

<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >



<hibernate-configuration>
    <session-factory>

    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <property name="hibernate.ogm.datastore.provider">mongodb</property>
    <!-- <property name="hibernate.ogm.datastore.grid_dialect">org.hibernate.ogm.datastore.mongodb.MongoDBDialect</property> -->
    <property name="hibernate.ogm.datastore.database">yourdb</property>
    <property name="hibernate.ogm.datastore.host">localhost</property>
    <property name="hibernate.ogm.datastore.port">27017</property>
    <property name="hibernate.ogm.datastore.create_database">true</property>
    <mapping resource="mkcl/os/test/Players.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

pom.xml

用于 OGM 的依赖项是

<dependency>
    <groupId>org.hibernate.ogm</groupId>
    <artifactId>hibernate-ogm-mongodb</artifactId>
    <version>4.1.3.Final</version>
</dependency>

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>2.12.4</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search-orm</artifactId>
    <version>4.2.0.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate.ogm</groupId>
    <artifactId>hibernate-ogm-core</artifactId>
    <version>4.1.3.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.1.0.Final</version>
</dependency>

<dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm</artifactId>
    <version>4.1</version>
</dependency>


<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>

<dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm-tree</artifactId>
    <version>4.1</version>
</dependency>

<dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm-util</artifactId>
    <version>4.1</version>
</dependency>

在Tomcat上部署后,我正在通过另一个jsp,当时HibernateUtil.java在以下行抛出异常

OgmConfiguration cfgogm = new OgmConfiguration();

例外线是

java.lang.IncompatibleClassChangeError:类 org.parboiled.transform.ClassNodeInitializer 具有接口 org.objectweb.asm.ClassVisitor 作为超类

请帮我解决这个问题。先感谢您

4

2 回答 2

0

更新你的 ASM jar,你可以从这里下载,

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.ow2.asm/asm-all/4.0

还要检查 parboiled-core 和 parboiled-java jar 的兼容版本。

在您的库中包含所有必需的 jar 之后,针对新库重新编译代码并尝试运行。

于 2016-01-04T06:35:22.813 回答
0

问题解决了。

实际上我的应用程序连接到 mysql 和 mongodb。所以我一起使用休眠和休眠OGM。以前对于 mysql,我在 Web 应用程序的 .hbm.xml 中编写了一些命名查询。因此,在从 HibernateUtil.java 创建会话时切换到带有 hibernate-OGM 的 mongoDB 时,hibernate 会扫描 .hbm.xml 文件,该文件包含 Hibernate OGM 未解析的命名查询,因为这些查询具有 sql 语法。所以它抛出一个异常。注释 sql 查询后,它工作正常。

@Zia:谢谢。

于 2016-01-04T07:26:27.123 回答