Koray Tugay
Koray Tugay

Reputation: 23844

How to use Hibernate for JPA implementation?

this is my pom.xml:

  <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.5.1-Final</version>
  </dependency>

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

When I have these 2 dependencies, I can successfully run my Hello World example. ( Which uses a persistence.xml and a class which is mapped to a table in my DB with @Entity annotation. However when I change my hibernate-core to:

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

I get :

Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.hibernate.cfg.Configuration.(Lorg/hibernate/cfg/SettingsFactory;)V from class org.hibernate.ejb.Ejb3Configuration

So how can I use hibernate core 4.2.1 final as a JPA implementation? I guess there is no version 4 for hibernate-entitymanager ?

Upvotes: 1

Views: 1030

Answers (2)

Julius
Julius

Reputation: 2864

This setup works for me. You need the same or similar release/version numbers for both components, because they are too different if you use 4.x.x and 3.x.x together.

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.1.Final</version>
    </dependency>

A common way to deal with (Hibernate or other) version numbers is to specify the version once in a property, like this

<properties>
    <hibernate.version>4.1.1.Final</hibernate.version>
</properties>

And then refer to that property in the dependency declaration..

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

Upvotes: 2

jmuok
jmuok

Reputation: 340

just change hibernate-entitymanager to the same version

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.2.1.Final</version>
</dependency>

Upvotes: 2

Related Questions