Hibernate CURD

本文通过具体实例介绍了Hibernate框架中各种核心操作的使用方法,包括创建、读取、更新、删除等,展示了不同状态的对象管理和数据库交互过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test.com.lesson5;
import javax.print.attribute.standard.Finishings;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.lesson5.User;

import junit.framework.TestCase;


public class TestUser extends TestCase {
    
    public void testCreate(){
        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        try {
            //瞬时态
            User user = new User();
            user.setName("test");
            //执行insert
            session.save(user);
            
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            
            e.printStackTrace();
        }finally{
            session.close();
        }
    }
    
    public void testGet(){
        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();        
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        try {
            //从数据 库读出的user是持久态
            //get方法先查一级缓存,再查二级缓存,最后再查数据 库
            User user = (User) session.get(User.class,0);
            System.out.println("name=" + user.getName());
            
            //从缓存中获取
            User user2 = (User) session.get(User.class,0);
            System.out.println("name=" + user2.getName());
            
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            
            e.printStackTrace();
        }finally{
            session.close();
        }
    }
    
    public void testLoad(){
        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();        
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        try {
            //load先查一级缓存,没有创建代理对象,访问对象属性时才进行实际查询
            //load方法如果没有找到记录则抛出
            //org.hibernate.ObjectNotFoundException: No row with the given identifier exists
            User user = (User) session.load(User.class,1);
            System.out.println("name=" + user.getName());
            
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            
            e.printStackTrace();
        }finally{
            session.close();
        }
    }
    
    public void testDelete(){
        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();        
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        try {
            User user = (User) session.get(User.class,0);
            System.out.println("name=" + user.getName());
            
            session.delete(user);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            
            e.printStackTrace();
        }finally{
            session.close();
        }
    }
    
    public void testSaveOrUpdate(){
        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        try {
            //瞬时态
            User user = new User();
            user.setId(123);
            user.setName("test123");
            //merge方法执行后,user不会纳入session管理,与saveOrUpdate相反
            session.saveOrUpdate(user);
            
            User user2 = (User) session.get(User.class,123);
            System.out.println("hashcode=" + user.hashCode());
            System.out.println("hashcode=" + user2.hashCode());
            //hashcode 一样
            
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            
            e.printStackTrace();
        }finally{
            session.close();
        }
    }
    
    public void testMerge(){
        SessionFactory sessionFactory = new AnnotationConfiguration().configure("com/lesson5/lesson5.cfg.xml").buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        try {
            //瞬时态
            User user = new User();
            user.setId(123);
            user.setName("test123");
            //merge方法执行后,user不会纳入session管理,与saveOrUpdate相反
            session.merge(user);
            
            User user2 = (User) session.get(User.class,123);
            System.out.println("hashcode=" + user.hashCode());
            System.out.println("hashcode=" + user2.hashCode());
            //hashcode 不一样
            
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            
            e.printStackTrace();
        }finally{
            session.close();
        }
    }
}
package com.lesson5;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {
    private int id;
    private String name;
    
    @Id
    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;
    }
    
    
}
<?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="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://127.0.0.2:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="format_sql">true</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="com.lesson5.User"/>
    </session-factory>
</hibernate-configuration>

 

转载于:https://www.cnblogs.com/xuruhong/p/3308632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值