1

我在尝试配置会话工厂时遇到此错误,根据谷歌的结果,将 javaee-api 放在提供的范围内应该可以解决问题,但它没有:/

完整的 pom.xml [代码]

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.me.app</groupId>
  <artifactId>PersonnesServices</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>
  <name>PersonnesServices</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <!-- Junit -->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <!-- HIBERNATE -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.1.2</version>
   </dependency>

   <!--  HSQLDB -->
   <dependency>
      <groupId>org.hsqldb</groupId>
      <artifactId>hsqldb</artifactId>
      <version>2.2.8</version>
   </dependency> 
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>

</project>

[/代码]

我的 hibernate.cfg.xml

[代码]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="connection.password"></property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="hibernate.current_session_context_class">thread</property>
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <property name="javax.persistence.validation.mode">none</property>
         <!-- Mapping files -->
        <mapping class="com.me.dto.Personne"/>
        <mapping class="com.me.dto.Ecole"/>
    </session-factory>
</hibernate-configuration>

[/代码]

和我的错误:

[代码]

Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
    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:296)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
    at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:115)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:304)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
    at com.csc.util.HibernateUtil.configureSessionFactory(HibernateUtil.java:15)
    at com.csc.services.test.main(test.java:20)

[/代码]

你们中有人遇到过这样的问题吗?谢谢。

4

3 回答 3

1

我在运行 junit 测试时使用 javaee-web-api 6 和休眠时遇到了这个问题。

我修复它替换:

<dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>

为了:

<dependency>
   <groupId>org.jboss.spec</groupId>
   <artifactId>jboss-javaee-6.0</artifactId>
   <version>2.0.0.Final</version>
   <type>pom</type>
   <scope>provided</scope>
</dependency>
于 2014-10-22T20:08:51.280 回答
0

迟到总比没有好:你可以使用 jboss-javaee-6.0 而不是 javaee-api-6.0。

看看这里Maven:hibernate-entitymanager 和 javaee-api 一起破坏了我的单元测试

于 2012-09-28T15:40:18.453 回答
0

我在运行 JUnits 时遇到了同样的问题,并通过以下步骤解决了: 1. 在 hibernate 依赖项之上添加以下依赖项

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.2.2</version>
        <scope>test</scope>
    </dependency>
  1. 我搞砸了我的构建路径,所以删除了“.classpath”文件(eclipse>navigator)并做了 maven>update project 来重置我的构建路径

然后它起作用了

于 2015-05-12T14:05:29.480 回答