我是Java Hibernate的新手。我正在编写一个基本程序,但收到此错误“Class not found pack1.Manager1”... Manager1是我的主要类。任何帮助将不胜感激..
我的文件如下:
1)hibernate / src / pack1 / Employeeh.hbm.xml:
<hibernate-mapping package="pack1">
<class name="Employeeh" table="EMPLOYEEH">
<id name="empid" column="EMP_ID" type="long">
<generator class="native"/>
</id>
<property name="fname" column="FNAME">
</property>
<property name="email"/>
</class>
</hibernate-mapping>
2)hibernate / src / pack1 / Employeeh.java:
package pack1;
public class Employeeh
{
private int empid;
private String fname;
private String email;
public int getEmpid()
{
return empid;
}
..
}
3)hibernate / src / pack1 / Manager1.java:
package pack1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Manager1
{
public static void main(String[] args)
{
Configuration c1=new Configuration();
c1.configure();
SessionFactory sf=c1.buildSessionFactory();
Session s1=sf.openSession();
Employeeh e1=new Employeeh();
e1.setEmpid(1);
e1.setFname("gourav");
e1.setEmail("a@b.com");
}
}
4)hibernate / src / hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">gourav</property>
<property name="hibernate.connection.password">baba</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping resource="pack1/Employeeh.hbm.xml"/>
</session-factory>
</hibernate-configuration>
提前致谢..
答案 0 :(得分:0)
我按照您发布的内容创建了一个新项目(我刚刚使用了MySql而不是Oracle)。 结果对我来说很成功......没有例外!
我会把你的所有文件发回给你......
Manager1
和Employeeh
类与pack1
包中的类完全相同。
我的hibernate.cfg.xml
类似于:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping resource="pack1/Employeeh.hbm.xml" />
</session-factory>
</hibernate-configuration>
我的Employeeh.hbm.xml
如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pack1">
<class name="Employeeh" table="EMPLOYEEH">
<id name="empid" column="EMP_ID" type="long">
<generator class="native" />
</id>
<property name="fname" column="FNAME">
</property>
<property name="email" />
</class>
</hibernate-mapping>
如果一切都相同,我的建议是检查所有文件是否在配置文件中指定的位置,并记住您需要一个连接器来使用您的数据库。
希望这有用。