2015-10-16 107 views
0

我正在使用JPA批注来映射模型中的实体。但是,我发现Hibernate Criteria易于使用,包含的代码较少,所以有没有使用Criteria而不使用hibernate xml方式进行映射的方法?我在DAO实现类试过这样:我可以在不使用Hibernate映射的情况下使用Hibernate Criteria吗?

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+映射xmls与所有属性,或了解更多关于Spring DaoSupport或其他方式。

在此先感谢。

回答

2

是的,它会工作。您可以使用JPA标注实体,而使用Hibernate标准来查询您的实体,而不是JPA标准。

我实际上已经测试过它。

我的实体类看起来是这样的:

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的标准检索:

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 
+0

我想这和它给这样的:在线程/ R/n'Exception“主” org.hibernate.HibernateException:当“hibernate.dialect”没有设置 连接不能为空\t在org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97) \t在org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67) \t at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)' – FaithReaper

+0

它正在工作,因为我使用正确的方式在Hibernate 4.3.11中加载'hibernate.cfg.xml'。谢谢。其实我发现我的问题更多地涉及到“如何阅读hibernate.cfg.xml以使Hibernate Criteria正常工作”。我在这里留下了更多相关的[链接](http://stackoverflow.com/questions/7986750/create-session-factory-in-hibernate-4),以便有人找到我的答案。 – FaithReaper

相关问题