You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JBoss EAP 6.4.x的JEE应用中引入Hibernate特定注解依赖?

在JBoss EAP 6.4.x中引入最小化Hibernate注解依赖(用于@CreationTimestamp/@UpdateTimestamp)

首先得理清两个关键前提:

  • 你提到的旧hibernate-annotations构件早在Hibernate 3.6版本就被合并到hibernate-core里了,后续再也没有单独维护,完全没必要再考虑它。
  • JBoss EAP 6.4.x本身内置了Hibernate 4.3.x(对应JPA 2.1规范),所以我们的依赖必须和这个版本匹配,避免容器内出现版本冲突。

最小化依赖配置

你只需要在pom.xml中添加hibernate-core的依赖,并将其scope设为provided——因为JBoss已经为你提供了这个库,不需要打包到war/ear里,仅用于编译阶段识别Hibernate的特定注解:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.11.Final</version> <!-- 这个版本和JBoss EAP 6.4.x内置版本完全匹配,彻底避免冲突 -->
    <scope>provided</scope>
</dependency>

选这个版本的原因很简单:JBoss EAP 6.4.x官方对应的Hibernate核心版本就是4.3.11.Final,用它能保证和容器内置的Hibernate实现100%兼容,不会出现类加载异常或者版本不兼容的问题。

注解使用示例

配置完依赖后,你就可以在实体类里正常使用@CreationTimestamp和@UpdateTimestamp了,举个典型例子:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.util.Date;

@Entity
public class YourEntity {
    @Id
    @GeneratedValue
    private Long id;

    @CreationTimestamp
    private Date createTime;

    @UpdateTimestamp
    private Date updateTime;

    // 省略getter/setter方法
}

额外注意事项

  • 绝对不要引入hibernate-annotations依赖,它不仅过时,还会和容器内置的Hibernate库产生冲突。
  • 一定要保持依赖版本和JBoss EAP内置的Hibernate版本一致,否则可能出现ClassNotFoundException或者类版本不兼容的奇怪问题。
  • 因为设置了scope=provided,这个依赖不会被打包到你的war/ear中,完全符合纯JEE应用轻量化部署的需求。

内容的提问来源于stack exchange,提问作者maxxyme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:04:06