分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

hibernate单独使用参考示例

发布时间:2023-09-06 02:01责任编辑:赖小花关键词:暂无标签

库配置

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --><dependency> ??<groupId>org.hibernate</groupId> ??<artifactId>hibernate-core</artifactId> ??<version>4.3.8.Final</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency> ??<groupId>mysql</groupId> ??<artifactId>mysql-connector-java</artifactId> ??<version>5.1.46</version></dependency>

hibernate配置

hibernate.cfg.xml

<?xml version="1.0" encoding="GBK"?><!-- 指定Hibernate配置文件的DTD信息 --> ?<!DOCTYPE hibernate-configuration PUBLIC ?<span style="white-space:pre;"></span>"-//Hibernate/Hibernate Configuration DTD 3.0//EN"<span style="white-space:pre;"></span>"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!-- hibernate-configuration是配置文件的根元素 --><hibernate-configuration> ??<session-factory> ?????<!-- 指定连接数据库所用的驱动 --> ?????<property name="connection.driver_class">com.mysql.jdbc.Driver</property> ?????<!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 --> ?????<property name="connection.url">jdbc:mysql://localhost/hibernate</property> ?????<!-- 指定连接数据库的用户名 --> ?????<property name="connection.username">test</property> ?????<!-- 指定连接数据库的密码 --> ?????<property name="connection.password">******</property> ?????<!-- 指定连接池里最大连接数 --> ?????<property name="hibernate.c3p0.max_size">20</property> ?????<!-- 指定连接池里最小连接数 --> ?????<property name="hibernate.c3p0.min_size">1</property> ?????<!-- 指定连接池里连接的超时时长 --> ?????<property name="hibernate.c3p0.timeout">5000</property> ?????<!-- 指定连接池里最大缓存多少个Statement对象 --> ?????<property name="hibernate.c3p0.max_statements">100</property> ?????<property name="hibernate.c3p0.idle_test_period">3000</property> ?????<property name="hibernate.c3p0.acquire_increment">2</property> ?????<property name="hibernate.c3p0.validate">true</property> ?????<!-- 指定数据库方言 --> ?????<property name="dialect">org.hibernate.dialect.MySQLDialect</property> ?????<!-- 根据需要自动创建数据库 --> ?????<property name="hbm2ddl.auto">update</property> ?????<!-- 显示Hibernate持久化操作所生成的SQL --> ?????<property name="show_sql">true</property> ?????<!-- 将SQL脚本进行格式化后再输出 --> ?????<property name="hibernate.format_sql">true</property> ?????<!-- 罗列所有持久化类的类名 --> ?????<mapping class="hibernate.test.EntityA" /> ?????<mapping class="hibernate.test.EntityB" /> ??</session-factory></hibernate-configuration>
View Code

获得session的工具类

public class HibernateUtil { ???public static final SessionFactory sessionFactory; ???static { ???????try { ???????????// 使用默认的hibernate.cfg.xml配置文件创建Configuration实例 ???????????Configuration cfg = new Configuration().configure(); ???????????// 以Configuration实例来创建SessionFactory实例 ???????????ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()) ???????????????????.build(); ???????????sessionFactory = cfg.buildSessionFactory(serviceRegistry); ???????} catch (Throwable ex) { ???????????System.err.println("Initial SessionFactory creation failed." + ex); ???????????throw new ExceptionInInitializerError(ex); ???????} ???} ???// ThreadLocal可以隔离多个线程的数据共享,因此不再需要对线程同步 ???public static final ThreadLocal<Session> session = new ThreadLocal<Session>(); ???public static Session currentSession() throws HibernateException { ???????Session s = session.get(); ???????// 如果该线程还没有Session,则创建一个新的Session ???????if (s == null) { ???????????s = sessionFactory.openSession(); ???????????// 将获得的Session变量存储在ThreadLocal变量session里 ???????????session.set(s); ???????} ???????return s; ???} ???public static void closeSession() throws HibernateException { ???????Session s = session.get(); ???????if (s != null) { ???????????s.close(); ???????} ???????session.set(null); ???}}
View Code

实体类

package hibernate.test; ?????import java.io.Serializable; ?import java.util.HashSet; ?import java.util.Set; ?????import javax.persistence.Entity; ?import javax.persistence.GeneratedValue; ?import javax.persistence.GenerationType; ?import javax.persistence.Id; ?import javax.persistence.OneToMany; ?????@Entity ?public class EntityA implements Serializable { ?????@Id ?????@GeneratedValue(strategy = GenerationType.AUTO) ?????private Integer ida; ?????private String columnOne; ?????private String columnTwo; ?????????@OneToMany(targetEntity = EntityB.class, mappedBy = "ea") ?????private Set<EntityB> ebs = new HashSet<EntityB>(); ?????????public EntityA() { ?????????} ?????????public EntityA(String columnOne, String columnTwo) { ?????????this.columnOne = columnOne; ?????????this.columnTwo = columnTwo; ?????} ?????????public Integer getIda() { ?????????return ida; ?????} ?????????public void setIda(Integer ida) { ?????????this.ida = ida; ?????} ?????????public String getColumnOne() { ?????????return columnOne; ?????} ?????????public void setColumnOne(String columnOne) { ?????????this.columnOne = columnOne; ?????} ?????????public String getColumnTwo() { ?????????return columnTwo; ?????} ?????????public void setColumnTwo(String columnTwo) { ?????????this.columnTwo = columnTwo; ?????} ?????????public Set<EntityB> getEbs() { ?????????return ebs; ?????} ?????????public void setEbs(Set<EntityB> ebs) { ?????????this.ebs = ebs; ?????} ?????} ?
View Code
package hibernate.test; ?????import java.io.Serializable; ?????import javax.persistence.Entity; ?import javax.persistence.GeneratedValue; ?import javax.persistence.GenerationType; ?import javax.persistence.Id; ?import javax.persistence.JoinColumn; ?import javax.persistence.ManyToOne; ?????@Entity ?public class EntityB implements Serializable { ?????@Id ?????@GeneratedValue(strategy = GenerationType.AUTO) ?????private Integer idb; ?????private String columnOne; ?????private String columnTwo; ?????@ManyToOne(targetEntity = EntityA.class) ?????@JoinColumn(name = "b_a", nullable = false) ?????private EntityA ea; ?????????public EntityB() { ?????????} ?????????public EntityB(String columnOne, String columnTwo) { ?????????this.columnOne = columnOne; ?????????this.columnTwo = columnTwo; ?????} ?????????public Integer getIdb() { ?????????return idb; ?????} ?????????public void setIdb(Integer idb) { ?????????this.idb = idb; ?????} ?????????public String getColumnOne() { ?????????return columnOne; ?????} ?????????public void setColumnOne(String columnOne) { ?????????this.columnOne = columnOne; ?????} ?????????public String getColumnTwo() { ?????????return columnTwo; ?????} ?????????public void setColumnTwo(String columnTwo) { ?????????this.columnTwo = columnTwo; ?????} ?????????public EntityA getEa() { ?????????return ea; ?????} ?????????public void setEa(EntityA ea) { ?????????this.ea = ea; ?????} ?????} ?
View Code

测试

package test; ???import org.hibernate.Session; ?import org.hibernate.Transaction; ???import hibernate.test.EntityA; ?import util.HibernateUtil; ???public class Main { ?????public static void main(String[] args) { ?????????Session session = HibernateUtil.currentSession(); ?????????EntityA a1 = new EntityA("cOne_r1", "cTwo_r1"); ?????????Transaction t = session.getTransaction(); ?????????t.begin(); ?????????session.save(a1); ?????????t.commit(); ?????????HibernateUtil.closeSession(); ?????} ?} ?

hibernate单独使用参考示例

原文地址:https://www.cnblogs.com/suheng/p/9222577.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved