Hibernate-学習ノート01
最初のHibernateウィジェット
Hibernateプロファイル:
モデルクラス:
モデルクラスプロファイル:
テストファイル:
結果:
結果は
Hibernate: insert into STUDENT (NAME, AGE, ID) values (?, ?, ?)
データベースフィールドはモデルフィールドと同じで、省略します.
Hibernateプロファイル:
<?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://127.0.0.1:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) --><!-- hibernate -->
<!--<property name="connection.pool_size">1</property>-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management SessionFactory.getCurrentSession() -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- hbm:hibernate mapping To ddl:datebase definition language-->
<property name="hbm2ddl.auto">update</property>
<!-- : hibernate Model -->
<mapping resource="com/ibm/hibernate/model/Student.hbm.xml"/>
<!-- AnnotationConfiguration -->
<!-- <mapping class="com.ibm.hibernate.model.Teacher"/> -->
</session-factory>
</hibernate-configuration>
モデルクラス:
package com.ibm.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "id:"+id+"\tname:"+name+"\tage:"+age;
}
}
モデルクラスプロファイル:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ibm.hibernate.model">
<class name="Student" table="STUDENT">
<id name="id" column="ID"></id>
<property name="name" column="NAME"></property>
<property name="age" column="AGE"></property>
</class>
</hibernate-mapping>
テストファイル:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.ibm.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(001);
s.setName("Tom");
s.setAge(10);
Session session = null;
SessionFactory sf = null;
Transaction t = null;
//1、
Configuration cfg = new Configuration();
//2、 SessionFactory SessionFactory
sf = cfg.configure().buildSessionFactory();
//3、 session
// session web session
//session , concation,session 。
//session , 。
//session , session.
session = sf.openSession();
//4、 ( hibernate.cfg.xml )
t = session.beginTransaction();
try {
//5、 , , hibernate
//
session.save(s);
//6、 /
t.commit();
} catch (HibernateException e) {
t.rollback();
e.printStackTrace();
}
//7、 session
session.close();
//8、 sessionfactory
sf.close();
}
}
結果:
結果は
Hibernate: insert into STUDENT (NAME, AGE, ID) values (?, ?, ?)
データベースフィールドはモデルフィールドと同じで、省略します.