【问题标题】:Can I use Hibernate Criteria without mapping with Hibernate?我可以在不映射 Hibernate 的情况下使用 Hibernate Criteria 吗?
【发布时间】:2016-01-15 02:20:41
【问题描述】:

我正在使用 JPA 注释来映射模型中的实体。但是,我发现 Hibernate Criteria 易于使用并且包含较少的代码编写,那么有什么方法可以使用 Criteria 而无需映射到 hibernate xml 方式?我在我的 DAO 实现类中试过这个:

private SessionFactory sFactory; // of type org.hibernate.SessionFactory
....

Session session = sFactory.getCurrentSession();
Criteria criteria = session.createCriteria(BTerminal.class);

但是,如果没有hibernate.cfg.xml,它会给出nullpointerexception。当然因为它没有注入。但是要填写这个cfg.xml,我必须添加映射xml文件,这不是我喜欢的方式。那么,我可以在使用 Hibernate Criteria 的同时使用 JPA 映射吗?

我没有使用 Spring。仍然怀疑哪个更容易:编写 10 多个具有所有属性的映射 xml,或者了解更多关于 Spring DaoSupport 的信息,或任何其他方式。

提前致谢。

【问题讨论】:

    标签: spring hibernate annotations mapping


    【解决方案1】:

    是的,它会起作用的。您可以使用 JPA 注释的实体,同时使用 Hibernate Criteria 而不是 JPA Criteria 来查询您的实体。

    我已经测试过了。

    我的实体类如下所示:

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class TestEntity {
    
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer id;
        @Version
        private Long version;
    ...
    }
    

    然后,我有 Hibernate 配置文件:hibernate.cfg.xml

    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/test</property>
            <property name="connection.username">root</property>
            <property name="connection.password">root</property>
            <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
            <property name="hbm2ddl.auto">create</property>
            <property name="show_sql">true</property>
            <mapping class="com.test.model.TestEntity" />
        </session-factory>
    </hibernate-configuration>
    

    注意,我仍然需要列出实体类,但我没有使用 Hibernate 映射文件 (hbm.xml)。我不认为 Hibernate 支持自动检测实体类,就像 JPA 那样(所以即使它们被注释,你仍然必须将它们列出来)。

    然后我将这段代码作为测试,持久化实体,然后使用 Hibernate Criteria 检索:

        Session session = sessionFactory.getCurrentSession();
    
        session.beginTransaction();
    
        TestEntity testEntity = new TestEntity();
        testEntity.setName("test");
        session.save(testEntity);
    
        List<TestEntity> tests = (List<TestEntity>) session.createCriteria(TestEntity.class).list();
        for (TestEntity test : tests) {
            System.out.println(test.getName());
        }
    
        session.getTransaction().commit();
    

    我有 ff。在我的控制台中输出:

    Hibernate: insert into TestEntity (name, version) values (?, ?)
    Hibernate: select this_.id as id1_0_0_, this_.name as name2_0_0_, this_.version as version3_0_0_ from TestEntity this_
    test
    

    【讨论】:

    • 我正在尝试这个,它给出了这个:/r/nException in thread "main" org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97) at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)
    • 它工作正常,因为我使用正确的方式在 Hibernate 4.3.11 中加载 hibernate.cfg.xml。谢谢。实际上,我发现我的问题与“如何阅读 hibernate.cfg.xml 以使 Hibernate Criteria 工作”更相关。我在这里留下一个更相关的link 供可能找到我答案的人使用。
    猜你喜欢
    • 2012-07-01
    • 2014-08-14
    • 2018-02-08
    • 1970-01-01
    • 2020-04-29
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多