0

我是 Java Hibernate 的新手。我正在编写一个基本程序,但收到此错误“找不到类 pack1.Manager1”... Manager1 是我的主类。任何帮助将不胜感激..

我的文件如下:

1)休眠/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)休眠/src/pack1/Employeeh.java:

package pack1;
public class Employeeh
{
private int empid;
private String fname;
private String email;
public int getEmpid()
{
    return empid;
}
..
}

3)休眠/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)休眠/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>

提前致谢..

4

1 回答 1

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>

如果一切都一样,我的建议是检查所有文件是否都在您在配置文件中指定的位置,并记住您需要一个连接器来使用您的数据库。

希望这会有所帮助。

于 2013-10-01T11:13:58.513 回答