java.lang.NoSuchFieldError : name at run time in hibernate project 错误

标签 java hibernate maven jpa slf4j

尝试我的第一个 hibernate (JPA 注释)控制台应用程序。而且我一直在兜圈子,试图理清依赖关系,感觉就像我在飓风中试图 build 一座纸牌屋。另外,我正在使用 maven,我的印象是 maven 应该可以防止这种类型的事情发生。无论如何,这是我得到的错误:

Exception in thread "main" java.lang.NoSuchFieldError: name
    at org.slf4j.impl.Log4jLoggerAdapter.<init>(Log4jLoggerAdapter.java:75)
    at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:75)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:103)
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:197)
    at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:107)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:124)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.deangrobler.hibernatetest.App.main(App.java:14)

这是我的 pom 文件:

<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.deangrobler</groupId>
  <artifactId>HibernateTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>HibernateTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.4.0.GA</version>
      <exclusions>
        <exclusion>
          <artifactId>ejb3-persistence</artifactId>
          <groupId>org.hibernate</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.10.Final</version>
      <exclusions>
        <exclusion>
          <artifactId>antlr</artifactId>
          <groupId>antlr</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.6.10.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-commons-annotations</artifactId>
      <version>3.2.0.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.0-api</artifactId>
      <version>1.0.1.Final</version>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-jpamodelgen</artifactId>
      <version>1.1.1.Final</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.5</version>
    </dependency>
  </dependencies>
</project>

还有我带注释的 bean 类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.deangrobler.hibernatetest;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * 
 * @author Dean Grobler
 */
@Entity
@Table(name = "user")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
    @NamedQuery(name = "User.findByName", query = "SELECT u FROM User u WHERE u.name = :name")})
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;

    public User() {
    }

    public User(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.deangrobler.hibernatetest.User[ id=" + id + " ]";
    }

}

如果我能在这方面得到一些帮助,我将永远感激不尽!

最佳答案

hibernate 实体管理器有两个不同的版本依赖关系。这是行不通的。删除其中一个

<dependency>
      <groupId>hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.4.0.GA</version>
      <exclusions>
        <exclusion>
          <artifactId>ejb3-persistence</artifactId>
          <groupId>org.hibernate</groupId>
        </exclusion>
      </exclusions>
    </dependency>
........................

<dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.6.10.Final</version>
    </dependency>

关于java.lang.NoSuchFieldError : name at run time in hibernate project 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22381239/

相关文章:

java - 破解编码面试 : Why does the recursive subset algorithm increase the index rather than decreasing it?

java - 通用 JDBCException : could not load an entity

java - 更改 Maven 中的默认结构以进行 war

java.lang.IllegalArgumentException : The given string value: { "In last 7 days" : 19 } cannot be transformed to Json object

maven - 使用 Intellij + Maven 导入库 : Cannot resolve symbol

java - Jenkins Build on Sonar Analysis 的错误 projectKey

java - extdirectspring 方法不起作用

java - 多维数组(网格显示) IF 条件

java - selenium 格式化程序修改(导出到 webdriver 支持的 java)

java - 没有可联合的枚举的 Hibernate 映射列表