Java 使用hibernate将对象保存到数据库中
我在包Java 使用hibernate将对象保存到数据库中,java,hibernate,Java,Hibernate,我在包com.hibernate中有一个名为Student\u Info的模型对象 在同一个包中,我有一个主类,我在其中创建了Student\u Info的实例: public class Main { public static void main(String[] args) { Student_Info student = new Student_Info(); student.setRollNo(1); student.setNa
com.hibernate
中有一个名为Student\u Info
的模型对象
在同一个包中,我有一个主类,我在其中创建了Student\u 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>
所以我写的东西有什么问题,我认为所有的设置都是正确的
编辑:
这是我的项目文件夹结构:
编辑2:
这是学生信息
课程:
@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\u 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>
与:
<mapping resource="com.hibernate.Student_Info.hbm.xml" />
您没有告诉hibernate学生信息是一个实体
请参阅试试这个
@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如何工作的更多信息
希望这是有帮助的 我认为这是不对的>
<mapping resource="com.hibernate.Student_Info" />
表示没有COM .HiBurn.EngultIoFipe文件,考虑Read .< /P>
更新
< >为了修复下一个异常,您应该考虑将文件重命名为java。
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info.hbm.xml not found
这意味着你应该使用
<mapping resource="Student_Info.hbm.xml" />
文件名必须是name*Student_Info.hbm.xml*应该是hibernate.cfg.xml在添加文件Student_Info.hbm.xml
后,我不得不将其移动到文件夹src
,它工作了,但我不得不更改这一行:
<mapping resource="com.hibernate.Student_Info" />
<mapping resource="com.hibernate.Student_Info.hbm.xml" />
致:
问题出在hibernate.cfg.xml文件的映射中
基于注释的映射解决方案
如果使用注释–则在映射标记的hibernate.cfg.xml文件中,使用类属性,而不是资源。
应该是
<mapping class="com.hibernate.Student_Info"/>
基于XML的映射解决方案
如果要使用单独的hbm文件进行映射,那么在hibernate.cfg.xml文件中,应该使用映射文件中的resource属性。但是在这种情况下,您必须指定hbm文件位置,如下所示(使用/而不是。)
希望这能解决你的问题 是的,我使用@Entity注释来做这件事,请检查我对我的帖子所做的修改。包括Student\u Info
实体的导入语句。在hibernate.cfg.xml
中,我使用了这个create
,因此它应该创建一个名为Student\u INFORMATION的新表,如果数据库中没有将Student\u Info
类的属性作为列的表。请参阅我对我的帖子所做的修改
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info.hbm.xml not found
<mapping resource="Student_Info.hbm.xml" />
<mapping resource="com.hibernate.Student_Info.hbm.xml" />
<mapping resource="Student_Info.hbm.xml" />
<mapping class="com.hibernate.Student_Info"/>
<mapping resource="com/hibernate/Student_Info.hbm.xml"/>