Hibernate 系列问题(入门)
Hibernate入门
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。
有问题可以留言,大家一起交流。
1、首先建立一个java project;
2、建立一个folder,导入所需jar包(个人导入可能有多余的);
3、建立一个实体类demo;
package com.scau.hibernate1;
public class demo {
private Integer id;
private String name;
private double score;
private int age;
public demo() {
super(); // TODO Auto-generated constructor stub
}
public demo(String name, int age,double score) {
super();
this.name = name;
this.score = score;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "demo [id=" + id + ", name=" + name + ", score=" + score + ", age=" + age + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
4、配置hibernate.cfg.xml文件;
点击next,填写相关内容:
然后点击finish,如果没反应,选择低版本的,就可以了:
这个文件位置是,不要放错:
5、配置其他属性
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 配置数据库方言 -->
<!-- 数据库连接池 c3p0 -->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!-- 配置当前session 上下文 一个线程一个session -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 配置自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置显示sql -->
<property name="show_sql">true</property>
<!-- 配置格式化sql -->
<property name="format_sql">true</property>
<!-- 注册映射文件 -->
<mapping resource="com/scau/hibernate1/demo.hbm.xml"/>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018??5??19?? ????12:52:47 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.scau.hibernate1.demo" table="t_demo">
<id name="id" column="t_id>
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<property name="score" type="double">
<column name="SCORE" />
</property>
</class>
</hibernate-mapping>
7、测试类:
package com.scau.hibernateTest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;
import com.scau.hibernate1.demo;
public class hibernateTest {
@Test
public void testSave() {
//1.加载主配置文件
Configuration configure = new Configuration().configure();
//2.获取sessionFactory对象
try {
ServiceRegistry servicRegistry = new StandardServiceRegistryBuilder().configure().build();
//3.获取sessionFactory对象
SessionFactory sessionFactory=new MetadataSources(servicRegistry).buildMetadata().buildSessionFactory();
//4.获取session对象
Session session = sessionFactory.openSession();
//Session session = sessionFactory.getCurrentSession();
//获取事物
Transaction transaction = session.beginTransaction();
//操作数据库
// demo user = new demo();
// user.setName("chenjunquan");
// user.setAge(18);
// user.setScore(100);
// session.save(user);
session.save(new demo("陈俊全",19,100));
//提交事务
transaction.commit();
session.close();
sessionFactory.close();
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
完成即可;
问题总结:
//实例化configuration
Configuration con=new Configuration().configure();
//实例化ServiceRegistry
ServiceRegistry service=new StandardServiceRegistryBuilder().applySettings(con.getProperties()).build();
//创建SessionFactory实例
SessionFactory fac=con.buildSessionFactory(service);
StandardServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory=new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
2.1错误信息 Error executing DDL via JDBC Statement 解决办法
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构