org.hibernate.cfg.AnnotationConfiguration报错

在使用MyEclipse开发过程中遇到无法加载特定Jar文件导致的Hibernate配置错误,通过删除项目中多余的hibernate-annotations.jar和hibernate-commons-annotations.jar文件,并保留hibernate3.jar,然后在MyEclipse中执行Redeploy操作来解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Caused by: org.springframework.beans.BeanInstantiationException:


Could not instantiate bean class [org.hibernate.cfg.AnnotationConfiguration]: Constructor threw exception; nested exception is java.lang.NoSuchFieldError: INSTANCE


网上查找了原因,原来要到tomcat里的webapps里删掉项目的hibernate-annotations.jar;hibernate-commons-annotations.jar;保留hibernate3.jar就行了。


不过在myeclipse的文件夹只有hibernate3.jar。

在myeclipse里tomcatserver。重新Redeploy一次,webapp文件夹就要重复生成一次,得不停的删



`org.hibernate.cfg.Environment.URL`是一个Hibernate环境变量,用于指定Hibernate配置文件(通常是`.cfg.xml`或`.properties`格式)所在的URL路径。这个设置在`Hibernate Configuration`对象的初始化过程中非常重要,因为它告诉Hibernate去哪里查找配置信息,以便进行数据库连接和其他配置设置。 例如,在`.properties`文件中,你可能会看到类似这样的设置: ``` hibernate.cfg.xml=file:/path/to/your/hibernate.cfg.xml ``` 而在`Hibernate.cfg.xml`中,你可以直接引用这个属性,不需要显式设置: ```xml <property name="hibernate.cfg.location">file:/path/to/your/hibernate.cfg.xml</property> ``` 如果你在Java代码中使用Spring,你可以在Spring配置中设置Hibernate的`SessionFactory`,这样Spring会自动处理`Environment.URL`的值,避免手动设置: ```java @Configuration @EnableTransactionManagement public class AppConfig { @Value("${hibernate.cfg.location}") private String hibernateConfigLocation; @Bean public LocalSessionFactoryBean sessionFactory() { // 使用hibernateConfigLocation设置配置文件路径 LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); factory.setConfigLocation(new ClassPathResource(hibernateConfigLocation)); return factory; } } ``` 如果你遇到`InitialContext`没有实现`EventContext`的问题,很可能是在试图手动通过`InitialContext`访问`Environment.URL`,而不是在Spring的上下文中操作,这时需要确保上述提到的最佳实践已经被遵循。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值