【发布时间】: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文件