0

I am using JPA annotations to map entities in my model. However, I found Hibernate Criteria is easy to use and contains less codes to write, so is there some way to use Criteria without mapping with hibernate xml ways? I tried this in my DAO implementation class:

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

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

But, without hibernate.cfg.xml it's giving nullpointerexception. Of course because it is not injected. But to fill this cfg.xml I have to add mapping xml files, which is not the way I like. So, can I use JPA mapping while using Hibernate Criteria?

I am not using Spring. Still doubt which is easier: write 10+ mapping xmls with all atributes, or to learn more about Spring DaoSupport, or any other ways.

Thanks in advance.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
WesternGun
  • 11,303
  • 6
  • 88
  • 157

1 Answers1

2

Yes, it will work. You can have JPA annotated entities, while you use Hibernate Criteria to query your entities, instead of JPA Criteria.

I have actually have tested it.

My entity class looks like this:

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;
...
}

Then, I have Hibernate config file: 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>

Notice, that I still have to list down the entity classes, but I'm not using Hibernate mapping files (hbm.xml). I don't think that Hibernate has support for auto-detection of entity classes, like JPA does (so you still have to list them down even if they are annotated).

Then I have this code as a test, persist entity then retrieve using 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();

I have the ff. output in my console:

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
Ish
  • 3,992
  • 1
  • 17
  • 23
  • I am trying this and it's giving this: /r/n`Exception 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)` – WesternGun Oct 16 '15 at 12:44
  • It is working since I use correct way to load `hibernate.cfg.xml` in Hibernate 4.3.11. Thanks. Actually I find my question is more related to "how to read hibernate.cfg.xml to get Hibernate Criteria working". I leave a more related [link](http://stackoverflow.com/questions/7986750/create-session-factory-in-hibernate-4) here for someone who may find my answer. – WesternGun Oct 19 '15 at 10:00