无法解决Hibernate中的错误

问题描述 投票:0回答:6

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类

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)
java eclipse hibernate
6个回答
0
投票

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


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); 

喜欢你的班级名字。


0
投票
public String F_Name;
public String L_Name;

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


0
投票

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

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

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

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

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

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

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


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

它会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.