What jar should I include to use javax.persistence package in a hibernate based application?

后端 未结 7 1321
生来不讨喜
生来不讨喜 2020-12-29 01:20

Is it ok to take it from Glassfish project ( glassfish-persistence-api) or may be there is a Hibernate jar?

相关标签:
7条回答
  • 2020-12-29 01:52

    In general, i agree with above answers that recommend to add maven dependency, but i prefer following solution.

    Add a dependency with API classes for full JavaEE profile:

    <properties>
        <javaee-api.version>7.0</javaee-api.version>
        <hibernate-entitymanager.version>5.1.3.Final</hibernate-entitymanager.version>
    </properties>
    
    <depencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee-api.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    

    Also add dependency with particular JPA provider like antonycc suggested:

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

    Note <scope>provided</scope> in API dependency section: this means that corresponding jar will not be exported into artifact's lib/, but will be provided by application server. Make sure your application server implements specified version of JavaEE API.

    0 讨论(0)
  • 2020-12-29 01:59

    For JPA 2.1 the javax.persistence package can be found in here:

    <dependency>
       <groupId>org.hibernate.javax.persistence</groupId>
       <artifactId>hibernate-jpa-2.1-api</artifactId>
       <version>1.0.0.Final</version>
    </dependency>
    

    See: hibernate-jpa-2.1-api on Maven Central The pattern seems to be to change the artefact name as the JPA version changes. If this continues new versions can be expected to arrive in Maven Central here: Hibernate JPA versions

    The above JPA 2.1 APi can be used in conjunction with Hibernate 4.3.7, specifically:

    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-entitymanager</artifactId>
       <version>4.3.7.Final</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-29 02:04

    You can use the ejb3-persistence.jar that's bundled with hibernate. This jar only includes the javax.persistence package.

    0 讨论(0)
  • 2020-12-29 02:05

    If you are developing an OSGi system I would recommend you to download the "bundlefied" version from Springsource Enterprise Bundle Repository.

    Otherwise its ok to use a regular jar-file containing the javax.persistence package

    0 讨论(0)
  • 2020-12-29 02:08

    hibernate.jar and hibernate-entitymanager.jar contains only the packages org.hibernate.*. So you should take it from the Glassfish project.

    0 讨论(0)
  • 2020-12-29 02:15

    If you are using maven, adding below dependency should work

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题