无法解决Hibernate中的错误

时间:2018-01-02 07:42:50

标签: java eclipse hibernate

My directory structure我试图通过使用控制台提供行的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 class

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)

6 个答案:

答案 0 :(得分:0)

其中一个可能的解决方案是Employees<id name="Id">更新到<id name="id">,让Employees实现Serializable并添加默认构造函数。 此外,由于这看起来有问题,这究竟是什么呢<many-to-one name="com.Beans.Employee"></many-to-one>

答案 1 :(得分:0)

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

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

应该是查询

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

喜欢你的班级名称。

答案 2 :(得分:0)

请查看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个文件

enter image description here

这对你有帮助。

答案 3 :(得分:0)

public String F_Name;
public String L_Name;

这些属性名称应该像fName,lName, 更改此项并在xml配置中尝试并添加“列”属性

答案 4 :(得分:0)

这是您的代码中的小问题。更正cfg.configure("hibernate.cfg.xml"); 类中的以下行。

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

b+mx/c-mx

答案 5 :(得分:0)

<many-to-one name="com.Beans.Employee"></many-to-one>

Hibernate期待一个名为&#34; com.Beans.Employee&#34;的字段。这里。删除此行。

错误信息中明确说明了这一点:

引起:org.hibernate.PropertyNotFoundException:com.Beans.Employee上找不到字段[com.Beans.Employee]