Hibernate 增删改查操作
来源:互联网 发布:linux下tomcat自启动 编辑:程序博客网 时间:2024/05/29 05:57
首先需要使用Hibernate逆向生成实体类。
参考文章:点击打开
我这里有两张表。
数据如下:
整个工程结构如下:
MySessionFactory.java:
package entity;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class MySessionFactory { // 加载hibernate.cfg.xml private static final Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); private static SessionFactory sessionFactory; private static Session session; public static Session getSession() throws HibernateException { if (session == null || session.isOpen() == false) { if (sessionFactory == null) { try { sessionFactory = cfg.buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); } } session = sessionFactory.openSession(); } return session; }}
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 加上两个实体类的映射 --> <mapping resource="entity/Username.hbm.xml" /> <mapping resource="entity/Password.hbm.xml"/> </session-factory></hibernate-configuration>
hibernate.cfg.xml是逆向工具自动生成的。不过我们得加上<map resource="...">
查询例子:
package entity;import java.util.Iterator;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import org.osgi.service.useradmin.User;public class Main { @SuppressWarnings("unchecked") public static void main(String[] args) { Session session = MySessionFactory.getSession(); Transaction transaction = session.beginTransaction(); List<Username> list = session.createQuery("from Username u, Password p where u.id = p.id and u.id = ?").setParameter(0, 2).list(); Iterator<?> iterator = list.iterator(); while(iterator.hasNext()) { Object[] objects = (Object[]) iterator.next(); Username username = (Username) objects[0]; Password passwordId = (Password) objects[1]; System.out.println(username.getId() + "\t" + username.getUsername() + "\t" + passwordId.getPassword()); } session.close(); }}
【注意.setParameter(0, 1)方法是从0开始计算】
控制台输出:
增加例子:
Username username = new Username(); username.setId(4); username.setUsername("Aiden4"); session.save(username); transaction.commit();
Navicat数据反馈:
【注意:一定要调用commit方法,才会生效】
修改例子:
Username username = new Username(); username.setId(2); username.setUsername("Aiden3"); session.update(username); transaction.commit();
Navicat数据反馈:
删除例子:
Username username = new Username(); username.setId(2); username.setUsername("Aiden3"); session.delete(username); transaction.commit();
Navicat数据反馈:
补充下:
刚博主运行了有个错误
Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [entity.Username#1]
错误的原因就是:session本来携带了数据,比如说session已经做了查询username.id=2的操作,则session中存在了Username id为2的对象。
如果此时继续
Username username = new Username();
username.setId(2);
username.setUsername("Aiden3");
session.update(username);
transaction.commit();
则会产生错误。
解决的办法:调用session.clear方法。删除session携带的数据,再去做增删改查动作。
文章最后附上Hibernate增删改查的例子:
附件下载:点击下载
1 0
- hibernate 增删改查操作
- Hibernate 增删改查操作
- Hibernate增删改查操作
- Hibernate基本增删改查操作
- Hibernate 实体基本增删查改操作
- Hibernate 基本增删改查操作
- Hibernate 批量增删改查操作
- Hibernate 批量增删改查操作
- Hibernate之Session增删改查操作
- hibernate基本操作增删改查
- Hibernate---curd(增删查改)操作
- hibernate增删查改
- hibernate增删改查
- Hibernate增删改查
- hibernate 增删改查
- hibernate 增删改查
- Hibernate 增删改查
- Hibernate增删改查
- 如何写线程安全的方法
- 7 多态
- 手动备份禅道3.0 MYSQL数据库
- linux如何安装jdk
- 【数据结构】邻接表表示法的图的深度广度优先遍历递归和非递归遍历
- Hibernate 增删改查操作
- 这是一篇测试文章
- android获取音频、视频、图片信息
- 华为机试题: 二叉搜索树
- 堆排序模板
- 欢迎使用CSDN-markdown编辑器
- 饱后思味
- 电子之模拟与数字电路学习笔记
- HashMap和HashTable的区别