当前位置: 动力学知识库 > 问答 > 编程问答 >

java - save an object into database using hibernate

问题描述:

I have a Model Object which named Student_Info in the package com.hibernate.

In the same package I have a Main class where I created an instance of 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();

}

}

In the hibernate.cfg.xml file i have this :

<?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>

But when I run the Main class I get this error :

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)

So what's wrong with what I wrote, I think all is set correctly.

Edit :

This is my project folder structure :

Edit 2:

This is the Student_Info class :

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

}

}

Edit 3 :

I created a file named Student_Info.hbm.xml in the package com.hibernate :

<?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>

And in the hibernate.cfg.xml I changed this line :

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

With :

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

But when I run the Main class I get this error :

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)

网友答案:

The problem is in mapping tag of your hibernate.cfg.xml file.

Solution For annotation based mappings

If you using annotation then in the hibernate.cfg.xml file in the mapping tag use class attribute instead of resource. It should be

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

Solution For XML-based mappings

If you want to use separate hbm file for mapping then in the hibernate.cfg.xml file you should use the resource attribute in the mapping file. But in that case you have to specify the hbm file location just like below (use / instead of .)

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

Hope this will resolve your problem.

网友答案:

Try this

@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;
    }



}

You did not specify any mapping to your table and If you are not provided any mapping then ORM engine will check for same column names in database as default option and still if if did not find anything It will throw mapping exception. Enable your log and find more information how does Hibernate works...

Hope this is helpful!

网友答案:

I think this is not correct >

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

This tell hibernate where are the hbm files located not the entities, then within the hbm.xml files you be able to add the entities.

<class name="Student_Info" table="Student_Info ">
    ...
</class>

Indeed

com.hibernate.Student_Info not found

Means there are not com.hibernate.Student_Info file, consider read this.

UPDATE

In order to fix the next exception you should consider renaming the file as java said.

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

It means you should use

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

And file name must to be name *Student_Info.hbm.xml* should be at the level of hibernate.cfg.xml

网友答案:

You didn't tell the hibernate the Student_Info is a entity.

Please refer http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-entity

网友答案:

After adding the file Student_Info.hbm.xml I had to move it into the folder src, and it worked, but I had to change this line :

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

to :

<mapping resource="Student_Info.hbm.xml" />
分享给朋友:
您可能感兴趣的文章:
随机阅读: