使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是啥?

     2023-02-26     233

关键词:

【中文标题】使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是啥?【英文标题】:What are the Maven dependencies to use hibernate, hibernate annotations, and ehcache?使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是什么? 【发布时间】:2011-05-04 19:14:14 【问题描述】:

我想用最新的 hibernate、hibernate-annotations 和 ehcache 依赖项更新我的 Maven pom.xml。

我阅读了休眠下载页面和 ehcache 下载页面。我所有试图解释它的尝试似乎都失败了。有人可以写出应该进入 pom.xml 的 sn-p 吗?

神秘的休眠下载页面: http://hibernate.org/downloads

神秘的 ehcache 下载页面: http://www.terracotta.org/dl/ehcache-oss-download-catalog

这是我目前所拥有的:

   <properties>
       <hibernate.version>3.6.0.Final</hibernate.version>
   </properties>
   <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>$hibernate.version</version>
        <exclusions>
            <exclusion>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
            </exclusion>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>$hibernate.version</version>
        <exclusions>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.2.0</version>
        <type>pom</type>
    </dependency>
 </dependencies>

    <repositories>
        <repository>
            <id>codehaus-repository</id>
            <name>Codehaus</name>
            <url>https://nexus.codehaus.org/content/repositories/releases/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>java.net</id>
            <url>http://download.java.net/maven/1</url>
            <layout>legacy</layout>
        </repository>
        <repository>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>sonatype-ehcache</id>
            <url>http://oss.sonatype.org/content/repositories/sourceforge-releases</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>JBoss Repository</id>
            <url>https://repository.jboss.org/nexus/content/groups/public</url>
            <layout>default</layout>
        </repository>
    </repositories>

我知道,它看起来很乱,因为我似乎不知道所有依赖项都存储在哪里。

以上失败:

Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)

更新: 帕斯卡,你的回复很清楚,解释得很好。但是,hibernate 在启动时仍然失败。

    sl4j 抛出错误,添加:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>
    

    这个警告有很多实例:

    g in the two-phase commit!
    1797 [main] WARN  net.sf.ehcache.hibernate.AbstractEhcacheRegionFactory  - No Tr
    ansactionManagerLookup found in Hibernate config, XA Caches will be participatin
    

    另外,这个错误会让一切都失败:

    相关原因:

    org.springframework.beans.factory.BeanCreationException: Error creating bean wit
    h name 'financialDAO' defined in ServletContext resource [/WEB-INF/config/applic
    ationContext-database.xml]: Cannot resolve reference to bean 'transactionManager
    ' while setting bean property 'transactionManager'; nested exception is org.spri
    ngframework.beans.factory.BeanCreationException: Error creating bean with name '
    transactionManager' defined in ServletContext resource [/WEB-INF/config/applicat
    ionContext-database.xml]: Cannot resolve reference to bean 'sessionFactory' whil
    e setting bean property 'sessionFactory'; nested exception is org.springframewor
    k.beans.factory.BeanCreationException: Error creating bean with name 'sessionFac
    tory' defined in ServletContext resource [/WEB-INF/config/applicationContext-dat
    abase.xml]: Invocation of init method failed; nested exception is java.lang.Null
    PointerException
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
    .resolveReference(BeanDefinitionValueResolver.java:328)
    

确认一下,这是spring配置:

<!-- Enable EHCache -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>

更新: 这似乎是休眠卡住的最新异常:

Caused by: java.lang.NullPointerException
    at net.sf.ehcache.hibernate.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:48)
    at net.sf.ehcache.hibernate.EhCacheRegionFactory.start(EhCacheRegionFactory.java:79)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:236)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)

【问题讨论】:

暂时不用你的 ehcache.xml 试试(即从你的 spring 配置中删除它)。 当我删除 hibernateproperties 中的所有 ehcache 条目时,应用程序启动正常。当我只删除 configurationResourceName 条目时,它表示它正在使用故障转移配置。 【参考方案1】:

如果您真的指的是 hibernate-annotations(而不是 hibernate-entitymanager)和 ehcache 的终极版本,那么您将需要以下内容:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.0.Final</version>
    </dependency>
    <dependency>
       <groupId>net.sf.ehcache</groupId>
       <artifactId>ehcache-core</artifactId>
       <version>2.2.0</version>
   </dependency>
   ...
  </dependencies>

  <repositories>
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </repository>
  </repositories>
  ...
</project>

从 Hibernate 3.6 开始,JDK 1.4 支持已被删除,Hibernate Annotations 已合并回 Core。

我的意思是 Hibernate EntityManager,然后在上面的 sn-p 中将 hibernate-core 替换为 hibernate-entitymanager

关于 EHCache,ehcache-core 应该提供您需要的一切(包括“新”Hibernate 3.3/3.5 caching SPI 及其CacheRegionFactory 的实现)。所以,作为documented:

Setting Ehcache as the Hibernate Second Level Cache

休眠 3.3 及更高版本

注意休眠 3.2 用户

请务必注意属性名称和值的更改。

用途:

<property name="hibernate.cache.region.factory_class">
          net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

例如创建实例,或

<property name="hibernate.cache.region.factory_class">
          net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>

强制 Hibernate 使用单例 Ehcache CacheManager.


跟进:回答您的更新:

sl4j 抛出错误,通过添加 (...)

好吧,我猜不出您使用的是什么日志框架,提供您选择的绑定作为练习留给读者 :)

这个警告有很多实例(...)

在非 JTA 环境中运行时,我只得到一个 WARNING。如果这是您的情况(非 JTA 环境),我会认为 WARNING 正常。如果这不是您的情况,请查看有关 JTA 的文档。

另外,这个错误会让一切都失败

这是一个不同的问题(但看起来SessionFactory 无法正确初始化,激活日志以查看原因),我建议发布一个新的春季特定问题。

【讨论】:

评论框里的格式太可怕了,会更新上面的问题。 帕斯卡,再次感谢您的帮助。你看起来很犀利。而且,针对配置问题提出一个新问题可能是有意义的。【参考方案2】:

如果你将 Spring 3 与 hibernate 3 集成,你可以通过这个 pom.xml

<properties>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>

<dependencies>
    <!-- Core utilities used by other modules. Define this if you use Spring 
        Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Expression Language (depends on spring-core) Define this if you use 
        Spring Expression APIs (org.springframework.expression.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define 
        this if you use Spring Bean APIs (org.springframework.beans.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, 
        spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Application Context (depends on spring-core, spring-expression, spring-aop, 
        spring-beans) This is the central artifact for Spring's Dependency Injection 
        Container and is generally always defined -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Various Application Context utilities, including EhCache, JavaMail, 
        Quartz, and Freemarker integration Define this if you need any of these integrations -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Transaction Management Abstraction (depends on spring-core, spring-beans, 
        spring-aop, spring-context) Define this if you use Spring Transactions or 
        DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
        spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, 
        and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) 
        Define this if you need ORM (org.springframework.orm.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, 
        JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, 
        spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Web application development utilities applicable to both Servlet and 
        Portlet Environments (depends on spring-core, spring-beans, spring-context) 
        Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
        web framework with Spring (org.springframework.web.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, 
        spring-context, spring-web) Define this if you use Spring MVC with a Servlet 
        Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, 
        spring-context, spring-web) Define this if you use Spring MVC with a Portlet 
        Container (org.springframework.web.portlet.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
        <version>$org.springframework.version</version>
    </dependency>

    <!-- Support for testing Spring applications with tools such as JUnit and 
        TestNG This artifact is generally always defined with a 'test' scope for 
        the integration testing framework and unit testing stubs -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>$org.springframework.version</version>
        <scope>test</scope>
    </dependency>


    <!-- Hibernate resources -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.2.GA</version>
    </dependency>
    <!-- EhCache -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.7</version>
    </dependency>


    <!-- Taglibs -->

    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>20030825.184428</version>
    </dependency>
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>20030825.183949</version>
    </dependency>

    <!-- Log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <type>jar</type>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.6</version>
    </dependency>


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

如果你想要不同版本的hibernate,你可以查看here

【讨论】:

【参考方案3】:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.2.2.Final</version>
</dependency>

【讨论】:

请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。

要使用的hibernate或jpa注释

我在HibernateDomainPojoObjects的项目和基于注释的配置中使用Hibernate。对于基于注释的配置,我们有两个选项使用javax.persistence.*的基于JPA的注释使用HibernateNativeAnnotationsorg.hibernate.annotations.*目前,我们为POJO文件和Hibernate本机API(如Ses... 查看详情

java示例代码_使用Hibernate为特定条件使用Hibernate注释覆盖表数据

java示例代码_使用Hibernate为特定条件使用Hibernate注释覆盖表数据 查看详情

使用 Hibernate 注释映射 PostgreSQL 串行类型

】使用Hibernate注释映射PostgreSQL串行类型【英文标题】:MappingPostgreSQLserialtypewithHibernateannotations【发布时间】:2011-06-2604:05:12【问题描述】:我正在使用Hibernate3.3和PostgreSQL8.x,并希望使用Hibernate注释来映射不是主键的自动递增列... 查看详情

hibernate:使用注释的完全连接

】hibernate:使用注释的完全连接【英文标题】:hibernate:fulljoinusingannotations【发布时间】:2013-03-1704:11:45【问题描述】:我想使用休眠对两个表执行完全外连接。这两个表没有任何外键主键关系。表1。valcodenvarchar2(10)valuenumberdatemo... 查看详情

hibernate注释用法

注释到现在为止,你已经看到Hibernate如何使用XML映射文件来完成从POJO到数据库表的数据转换的,反之亦然。Hibernate注释是无需使用XML文件来定义映射的最新方法。你可以额外使用注释或直接代替XML映射元数据。Hibernate注释是一... 查看详情

如何将继承策略与 JPA 注释和 Hibernate 混合使用?

】如何将继承策略与JPA注释和Hibernate混合使用?【英文标题】:HowtomixinheritancestrategieswithJPAannotationsandHibernate?【发布时间】:2011-04-2407:44:00【问题描述】:根据Hibernate参考文档,在使用Hibernate的XML元数据时应该可以混合不同的继... 查看详情

如何使用 Hibernate 注释标记外键约束?

】如何使用Hibernate注释标记外键约束?【英文标题】:HowcanImarkaforeignkeyconstraintusingHibernateannotations?【发布时间】:2013-03-0319:59:21【问题描述】:我正在尝试使用Hibernate注释为我的数据库表编写模型类。我有两个表,每个表都有... 查看详情

java示例代码_正确使用Hibernate@Index注释

java示例代码_正确使用Hibernate@Index注释 查看详情

Hibernate 默认模式和表注释

...tation【发布时间】:2016-11-0301:40:20【问题描述】:我正在使用SpringBoot1.3.5和Hibernate5.1。告诉Hibernate使用什么模式的属性是spring.jpa.properties.hibernate.default_schema值(模式名称,可以说是development 查看详情

java示例代码_从Hibernate透视图使用带注释的对象而不是hbms执行Hibernate查询

java示例代码_从Hibernate透视图使用带注释的对象而不是hbms执行Hibernate查询 查看详情

如何使用 Hibernate 注释创建允许 NULL 值的唯一约束?

】如何使用Hibernate注释创建允许NULL值的唯一约束?【英文标题】:HowcanIcreateaUniqueConstraintthatallowsNULLvalueswithHibernateannotations?【发布时间】:2014-04-0500:02:32【问题描述】:我正在使用:Spring4、Hibernate4、SQLServer2008我从这个问题回... 查看详情

java示例代码_使用hibernate注释创建外键约束

java示例代码_使用hibernate注释创建外键约束 查看详情

java示例代码_使用java hibernate注释添加二级索引

java示例代码_使用java hibernate注释添加二级索引 查看详情

是否可以使用 Hibernate 验证注释对 DTO 类和实体进行注释?

】是否可以使用Hibernate验证注释对DTO类和实体进行注释?【英文标题】:IsitoktoannotatewithHibernatevalidationannotationsbothDTOclassesandEntities?【发布时间】:2021-08-0313:30:31【问题描述】:为了减少出现错误的机会。还是我应该只注释DTO?... 查看详情

Hibernate 注释或 XML 配置

...目。Hibernate注解是更好的选择还是HibernateXML映射更好?我使用过带有XML配置的Hibernate,但我对注解一无所知。在进行基于注释的实现时是否有任何问题?基于注释的应用程序维护更容易还是更复杂 查看详情

如何使用 Hibernate 正确注释 List<Interface>?

】如何使用Hibernate正确注释List<Interface>?【英文标题】:HowtoproperlyannotateaList<Interface>withHibernate?【发布时间】:2018-03-1802:16:33【问题描述】:我刚刚开始学习如何使用Hibernate,但我无法弄清楚在引用接口时如何正确注释... 查看详情

使用 H2、JPA 注释和 Hibernate 问题的一对多关联

】使用H2、JPA注释和Hibernate问题的一对多关联【英文标题】:UsingOne-to-ManyassociationswithH2,JPAannotationsandHibernateProblem【发布时间】:2011-09-2615:02:00【问题描述】:我们使用H2、JPA注释、Spring和Hibernate的组合来开发我们的web应用程序。... 查看详情

在 Hibernate 中使用注释定义默认列值

】在Hibernate中使用注释定义默认列值【英文标题】:DefinedefaultcolumnvaluewithannotationsinHibernate【发布时间】:2015-03-2208:28:53【问题描述】:我知道在SO和网络上有很多这样的问题,但所有答案都建议使用columnDefinition,这是特定于数... 查看详情