gpt4 book ai didi

java - 在 JodaTime 中使用 Hibernate 注释的问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:57:07 27 4
gpt4 key购买 nike

我回来了,因为我在使用 JodaTime 时仍然遇到问题。在前面的评论之后,我修改了我的 pom 并且 @Type 注释 是固定的。这是我的新 pom:

<properties>
<org.springframework.version>3.0.3.RELEASE</org.springframework.version>
<hibernate.version>3.6.0.Beta1</hibernate.version>
</properties>
<dependencies>
...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<!-- Spring Dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</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>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.3.1</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.5</version>
</dependency>

<!-- Joda Time dependencies -->
<dependency>
<artifactId>joda-time</artifactId>
<groupId>joda-time</groupId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time-jsptags</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time-hibernate</artifactId>
<version>1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>

我实际上正在开发一个我使用 Spring 和 Hibernate 的个人项目。我还使用 JodaTime 来保存日期字段。当我使用 Junit4 进行单元测试时,我发现了这个异常:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring/spring-dao.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: Implementing class
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
....
fr.cc2i.intervention.dao.test.WebInterventionTest.oneTimedSetUp(WebInterventionTest.java:33)
.....
Caused by: java.lang.IncompatibleClassChangeError: Implementing class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
at
.....

根据该网站:http://www.mail-archive.com/joda-interest@lists.sourceforge.net/msg00609.html,jodatime 可能会导致错误。

谁能给我解释一下什么是:java.lang.incompatibleChangeError : Implementing Class?以及如何解决这个问题?

我的 Spring 配置:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"> <ref local="dataSource" /></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>

<!-- Pour les requetes SQL -->
<prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>

<!-- manipuler avec attention -->
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>

</props>
</property>
<property name="annotatedClasses">
<list>
<value>fr.cc2i.intervention.dao.beans.Client</value>
<value>fr.cc2i.intervention.dao.beans.Intervention</value>
<value>fr.cc2i.intervention.dao.beans.Technicien</value>
<value>fr.cc2i.intervention.dao.beans.Contrat</value>
</list>
</property>
</bean>

我的实体类:

    package fr.cc2i.intervention.dao.beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Type;
import org.joda.time.Period;

/**
* Classe définissant un contrat
* My code is written in French ;)
* @author lindows
*
*/
@Entity
@Table(name="T_Contrat")
public class Contrat {

private long id;

private String reference;

private Period heures_totales;

private Period heures_restantes;


public Contrat(){}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}

private void setId(long id) {
this.id = id;
}


public String getReference() {
return reference;
}

public void setReference(String reference) {
this.reference = reference;
}

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistantPeriod")
public Period getHeures_totales() {
return heures_totales;
}

public void setHeures_totales(Period heuresTotales) {
heures_totales = heuresTotales;
}

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistantPeriod")
public Period getHeures_restantes() {
return heures_restantes;
}

public void setHeures_restantes(Period heuresRestantes) {
heures_restantes = heuresRestantes;
}

public String toString(){
return this.reference + " - \n Heures totales :" + this.heures_totales.toString() +
" Heures Restantes : " + this.heures_restantes.toString();
}

}

谢谢你的帮助

最佳答案

正如我在评论中所写,您的 pom.xml 很乱,这在某种程度上是问题的根本原因。这是完整的解释。

根据公告Simultaneous Hibernate 3.5.4 and 3.6.0.Beta1 Releases下面引用的是,hibernate-annotations 模块已被合并到 Hibernate 3.6.x 的核心中(这成为可能,因为 Hibernate 3.6.x 正在放弃 JDK 1.4 支持):

3.6.0.Beta1

3.6 introduces some new features as well as incorporating most of the fixes from 3.5. Changes of particular interest include

  • Dropping support for JDK 1.4
  • merging some modules into core; specifically hibernate-jmx and hibernate-annotations
  • repackaging of classes in hibernate-testing
  • HHH-2277 : fixes a long known limitation in key-many-to-one support.
  • HHH-5138, HHH-5182, HHH-5262 : collectively address a number of improvements to the "Hibernate type system". See the newly separated and expanded chapter on Types in the reference manual for details.
  • HHH-5268 : java.util.UUID support

For more details about 3.6.0.Beta1, including the full list of changes, see the release page.

事实上,hibernate-annotations-3.6-SNAPSHOT.jar 的最新版本实际上是临时构建的空 jar (因为合并),直到完全删除 maven 模块本身。但是因为您没有使用 hibernate-core-3.6-SNAPSHOT.jar,所以您根本就没有 @Type 注释。

现在,这是我的建议/评论的摘要:

  • 使用 Maven 传递依赖解析机制,如果您想使用 JPA,请声明所有依赖,只声明“顶级”依赖,即 hibernate-entitymanager。<
  • 从 3.5 版开始,Hibernate Core、Annotations、EntityManager 一起发布,它们的版本同步
  • 如果你想使用 Hibernate 3.6.x(现在有 Beta2),@Typehibernate-core 中(如果你使用 transitive,这并不重要依赖项)。
  • 除非您确切地知道自己在做什么,否则我不建议您依赖 SNAPSHOT。

所以你的 pom.xml 应该为 Hibernate 声明这样的东西:

<project>
...
<properties>
<!--hibernate.version>3.4.O.GA</hibernate.version--> <!-- for JPA 1.0 -->
<hibernate.version>3.5.4-Final</hibernate.version> <!-- for JPA 2.0 -->
<!--hibernate.version>3.6.0.Beta2</hibernate.version--> <!-- experimental -->
<properties>
...
<dependencies>
<!-- Hibernate dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
...
</dependencies>
...
</project>

这就是您所需要的。

关于java - 在 JodaTime 中使用 Hibernate 注释的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3472851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com