使用Hibernate将对象保存到数据库中

3

我有一个名为Student_Info的模型对象,它位于com.hibernate包中。

在同一包中,我有一个主类,在其中创建了Student_Info的实例:

public class Main {

    public static void main(String[] args) {
        Student_Info student = new Student_Info();
        student.setRollNo(1);
        student.setName("Ichigo");

        SessionFactory sessionFactory = 
                new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.save(student);

        session.getTransaction().commit();
        session.close();
        sessionFactory.close();

    }

}

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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">toor</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="com.hibernate.Student_Info" />
    </session-factory>
</hibernate-configuration>

但是当我运行Main类时,会出现以下错误:
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:767)
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:123)
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:213)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:201)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:183)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:177)
    at com.hibernate.Main.main(Main.java:16)

我认为我写的一切都是正确的,那我写错了什么呢?

编辑:

这是我的项目文件夹结构:

enter image description here

编辑2:

这是Student_Info类:

@Entity
@Table(name="STUDENT_INFORMATION")
public class Student_Info {

    @Id
    private int rollNo;
    private String name;

    public int getRollNo() {
        return rollNo;
    }

    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



}

编辑 3:

我在包com.hibernate中创建了一个名为Student_Info.hbm.xml的文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.hibernate.Student_Info" table="STUDENT_INFORMATION" catalog="hibernatedb">
        <id name="rollNo" type="int">
            <column name="rollNo" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" />
        </property>
    </class>
</hibernate-mapping>

hibernate.cfg.xml 文件中,我更改了这一行:

<mapping resource="com.hibernate.Student_Info" />

使用:

<mapping resource="com.hibernate.Student_Info.hbm.xml" />

但是当我运行主类时,我收到了以下错误提示:
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:767)
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:123)
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:213)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:201)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:183)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:46)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:177)
    at com.hibernate.Main.main(Main.java:16)

请包含 Student_Info 实体的导入语句。 - Abhishek Nayak
5个回答

1

试试这个

@Entity

@Table(name="STUDENT_INFORMATION")

public class Student_Info {

    @Id
    @column(name="your id column name") 
    private int rollNo;

    @column(name="your name column name") 
    private String name;

    public int getRollNo() {
        return rollNo;
    }

    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



}

您没有为您的表指定任何映射,如果没有提供任何映射,则ORM引擎将检查数据库中是否有相同的列名作为默认选项,如果仍然找不到任何内容,则会抛出映射异常。启用日志并查找更多信息,了解Hibernate的工作原理...希望这对您有所帮助!

hibernate.cfg.xml 文件中,我使用了 <property name="hbm2ddl.auto">create</property> 这个配置项,这样如果数据库中没有名为 STUDENT_INFORMATION 的表格,并且该表格的列与 Student_Info 类的属性相匹配,Hibernate 就会自动创建一个新的表格。 - Renaud is Not Bill Gates

1
我认为这不正确 >
<mapping resource="com.hibernate.Student_Info" />

这将告诉Hibernate hbm文件的位置,而不是实体的位置,然后在hbm.xml文件中,您可以添加实体。
<class name="Student_Info" table="Student_Info ">
    ...
</class>

确实

com.hibernate.Student_Info not found

意思是没有com.hibernate.Student_Info文件,请考虑阅读this

更新

为了解决下一个异常,您应该考虑按照Java的说法将文件重命名。

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info.hbm.xml not found

这句话的意思是:你应该使用


<mapping resource="Student_Info.hbm.xml" />

文件名必须为*Student_Info.hbm.xml*,并且位于hibernate.cfg.xml的级别。

请查看我对帖子所做的修改。 - Renaud is Not Bill Gates

1
问题出在你的hibernate.cfg.xml文件中的标签上。
针对基于注释的映射的解决方案
如果你使用注释,则在hibernate.cfg.xml文件中的标签中使用class属性而不是resource。 应该这样做:
<mapping class="com.hibernate.Student_Info"/>

XML映射的解决方案

如果您想使用单独的hbm文件进行映射,则在hibernate.cfg.xml文件中,您应该在映射文件中使用资源属性。但在这种情况下,您必须像下面这样指定hbm文件的位置(使用/而不是.

<mapping resource="com/hibernate/Student_Info.hbm.xml"/>

希望这能解决你的问题。

0
在添加了文件Student_Info.hbm.xml之后,我不得不将其移动到src文件夹中,这样它才能正常工作,但我还需要更改这一行代码:

<mapping resource="com.hibernate.Student_Info.hbm.xml" />

至:

<mapping resource="Student_Info.hbm.xml" />

0

1
我使用了@Entity注解来实现这个, 请检查我对帖子所做的修改。 - Renaud is Not Bill Gates

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接