【问题标题】:Cannot Resolve the Error in Hibernate无法解决休眠中的错误
【发布时间】:2018-01-02 07:42:49
【问题描述】:

我正在尝试通过使用控制台提供行的 ID 号从数据库中检索特定行。但是我遇到了某种错误,我不知道代码有什么问题。我已经尝试了很多来解决它,但没有任何帮助请帮助我解决这个错误。这是我的整个项目代码。

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="show_sql">true</property>
    <!-- <property name="hbm2ddl.auto">create</property> -->
    <!--Below are other values for hbm2ddl.auto validate: validate the schema, 
        makes no changes to the database. update: update the schema. create: creates 
        the schema, destroying previous data. create-drop: drop the schema at the 
        end of the session. -->
    <!--property name="hibernate.cache.use_query_cache">true</property -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3307/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">mujju</property>
    <mapping resource="hibernate.hbm.xml"/>

</session-factory>
</hibernate-configuration>

我的 hibernate.hbm.xml 文件

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

<hibernate-mapping>  
 <class name="com.Beans.Employee" table="employee">  
<id name="Id">  
 <generator class="increment"></generator>  
</id>  

<property name="F_Name"></property>  
<property name="L_Name"></property>  
 <many-to-one name="com.Beans.Employee"></many-to-one>
</class>  

</hibernate-mapping>

pojo 类

package com.Beans;

public class Employee {
public int id;
public String F_Name;
public String L_Name;

public int getId() {
    return id;
}

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

public String getF_Name() {
    return F_Name;
}

public void setF_Name(String f_Name) {
    F_Name = f_Name;
}

public String getL_Name() {
    return L_Name;
}

public void setL_Name(String l_Name) {
    L_Name = l_Name;
}

@Override
public String toString() {
    return "Employee [id=" + id + ", F_Name=" + F_Name + ", L_Name=" + L_Name + ", getClass()=" + getClass()
            + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}}

这是我的数据插入代码

 package com.Beans;
 import java.util.Scanner;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 import org.hibernate.cfg.Configuration;
 public class Client {
 public static void main(String[] args) {
    Configuration cfg= new Configuration(); 
    cfg.configure("Hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();
    Transaction tx = session.beginTransaction();
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Employee First Name:");
    String fname = sc.nextLine();
    Scanner sc1 = new Scanner(System.in);
    System.out.println("Enter Employee Last Name:");
    String lname = sc1.nextLine();
    Employee e = new Employee();
    e.setF_Name(fname);
    e.setL_Name(lname);
    session.persist(e);
    tx.commit();
    session.close();        
} }

这是我的数据检索类

 package com.Beans;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Scanner; 
  import org.hibernate.Query;
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  import org.hibernate.cfg.Configuration;

  public class DataRetrieve {
  public static void main(String[] args) {
  Configuration cfg= new Configuration(); 
    cfg.configure("hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();
    Transaction tx = session.beginTransaction();
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the ID you want to see the record:");
    int i = sc.nextInt();
    Query query = session.createQuery("from employee where id =" + i);
    List list = query.list();
    Employee emp = new Employee();
        int j = emp.getId();
        if (j == i) {
            System.out.println(emp.getId()+"");
        } else {
            System.out.println("no fields found");
        }       
    tx.commit();
    session.close();
}}

我得到的错误

Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibernate.hbm.xml
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3411)

Caused by: org.hibernate.PropertyNotFoundException: field [com.Beans.Employee] not found on com.Beans.Employee
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:182)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:174)

【问题讨论】:

  • 在 com.Beans.Employee 上找不到字段 [com.Beans.Employee]
  • 错误堆栈似乎不言自明。是否存在包命名空间冲突?
  • 项目中的每个类都在com.Beans包下,并且在Client.java和DataRetriev.java中已经明确指定
  • 同一个包中的所有类?那么为什么还要有包裹呢?找出你在哪里使用该字段,以及它应该是什么
  • 能否请您提供hibernate.hbm.xml文件

标签: java eclipse hibernate


【解决方案1】:

其中一种可能的解决方案是将Employees&lt;id name="Id"&gt; 更新为&lt;id name="id"&gt;,使Employees 实现Serializable 并添加一个默认构造函数。 这究竟是做什么的&lt;many-to-one name="com.Beans.Employee"&gt;&lt;/many-to-one&gt;,因为这看起来有问题。

【讨论】:

  • 我做了更改,但没有用
  • 您是否尝试删除此
【解决方案2】:

首先从一些东西中清除你的代码,让你的编程工具生成 getter 和 setter 方法。我认为这个错误是因为你的 getter 和 setter。您的 toString 方法也很糟糕。从中删除super.toString。 之后你会从这一行得到错误:

Query query = session.createQuery("from employee where id =" + i);

应该是查询

query = session.createQuery("from Employee where id =" + i); 

喜欢你的班级名称。

【讨论】:

  • 输入要查看记录的 ID:2 线程“main”中的异常 org.hibernate.hql.internal.ast.QuerySyntaxException:员工未映射 [from employee where id =2] at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
【解决方案3】:
public String F_Name;
public String L_Name;

这些属性名称应该像 fName、lName、 改变这个并在xml配置之后尝试,添加“column”属性

【讨论】:

    【解决方案4】:

    这是您代码中的小问题。更正DataRetrieve 类中的以下行。

    cfg.configure("hibernate.cfg.xml");
    

    cfg.configure("Hibernate.cfg.xml");
    

    【讨论】:

    • 我的配置文件名为hibernate.cfg.xml
    • 但您在 Client 类或文件名中提到了配置文件名 Hibernate.cfg.xml。所以请在提问时保持命名的一致性。
    【解决方案5】:
    <many-to-one name="com.Beans.Employee"></many-to-one>
    

    Hibernate 在这里需要一个名为“com.Beans.Employee”的字段。删除此行。

    这在错误信息中有明确说明:

    原因:org.hibernate.PropertyNotFoundException:在 com.Beans.Employee 上找不到字段 [com.Beans.Employee]

    【讨论】:

      【解决方案6】:

      请查看DataRetrieve

       package com.Beans;
        import java.util.List;
      import java.util.Scanner;
      
      import org.hibernate.Query;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.Transaction;
      import org.hibernate.cfg.Configuration;
      
        public class DataRetrieve {
        public static void main(String[] args) {
        Configuration cfg= new Configuration(); 
          cfg.configure("hibernate.cfg.xml");
          SessionFactory sf = cfg.buildSessionFactory();
          Session session = sf.openSession();
          Transaction tx = session.beginTransaction();
          Scanner sc = new Scanner(System.in);
          System.out.print("Enter the ID you want to see the record:");
          int i = sc.nextInt();
          Query query = session.createQuery("from Employee where id =" + i);
          List list = query.list();
          if(list.size() > 0){
              Employee emp = (Employee) list.get(0);//new Employee();
              int j = emp.getId();
              if (j == i) {
                  System.out.println(emp.getId()+"");
              } else {
                  System.out.println("no fields found");
              } 
          }else {
              System.out.println("no Record found");
          } 
      
          tx.commit();
          session.close();
      }}
      

      编辑:

      hibernate.hbm.xml
      

      还有配置中的编辑。

      <?xml version='1.0' encoding='UTF-8'?>  
      <!DOCTYPE hibernate-mapping PUBLIC  
       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
      
      <hibernate-mapping>  
       <class name="com.Beans.Employee" table="employee">  
      <id name="Id">  
       <generator class="increment"></generator>  
      </id>  
      
      <property name="F_Name"></property>  
      <property name="L_Name"></property>  
       <!-- <many-to-one name="com.Beans.Employee"></many-to-one> -->
      </class>  
      
      </hibernate-mapping>
      

      编辑:

        hibernate.cfg.xml
      

      也检查一下

      <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
      <hibernate-configuration>
      <session-factory>
          <property name="show_sql">true</property>
          <!-- <property name="hbm2ddl.auto">create</property> -->
          <!--Below are other values for hbm2ddl.auto validate: validate the schema, 
              makes no changes to the database. update: update the schema. create: creates 
              the schema, destroying previous data. create-drop: drop the schema at the 
              end of the session. -->
          <!--property name="hibernate.cache.use_query_cache">true</property -->
          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
          <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
          <property name="hibernate.connection.username">root</property>
          <property name="hibernate.connection.password"></property>
          <mapping resource="hibernate.hbm.xml"/>
      
      </session-factory>
      </hibernate-configuration>
      

      下图有hierarchy of cfg 文件

      对你有帮助。

      【讨论】:

      • INFO:HHH000221:从资源读取映射:hibernate.hbm.xml 线程“main”中的异常 org.hibernate.InvalidMappingException:无法在 org.hibernate.internal.util.xml.MappingReader 读取 XML .readMappingDocument(MappingReader.java:109)
      • 我的层次结构和你给@shivam的完全一样
      • 您的 src 文件夹或其他文件夹中的配置 xml 文件在哪里。当您的映射文件未找到配置时,会出现此错误。你把你的 hibernate.hbm.xml 放在包里,所以把它从你的包中删除
      • 它在包之外但在src文件夹中
      • 你能给我提供层次结构的截图吗
      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 2013-09-11
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多