Hibernate基础:快速入门(5):删除数据

数据库的操作增删改查,或者CRUD是最常见的操作。这篇文章中介绍删除数据的方法

hibernate.cfg.xml

创建如下所示的hibernate的设定文件




<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>

    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hbtestdb?useSSL=falseproperty>
    <property name="hibernate.connection.username">hbtestuser01property>
    <property name="hibernate.connection.password">hbtestuser01property>

    <property name="show_sql">trueproperty>

    <property name="current_session_context_class">threadproperty>
  session-factory>
hibernate-configuration>

Entity Class:User

我们将会创建一个Entity的User类。Entity类就是用来与数据库的Table进行Mapping的Class。

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;

@Entity
@Table (name="hbtableuser")
public class User {
  @Id
  @Column(name="userid")
  private int userid;

  @Column(name="username")
  private String username;

  @Column(name="country")
  private String country;

  public  User(){
    System.out.println("Default User construction is called...");
  }
  public User(int userid, String username, String country) {
    this.userid = userid;
    this.username = username;
    this.country = country;
  }

  public int getUserid() {
    return userid;
  }

  public void setUserid(int userid) {
    this.userid = userid;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }
}
类型 注解 import 作用
类注解 @Entity javax.persistence.Entity 类注解用
类注解 @Table javax.persistence.Table 类注解用:@Table (name=”Table名”)
属性注解 @Id javax.persistence.Id 主码注解
属性注解 @Column javax.persistence.Column 所有字段字段注解都需要

使用xml文件自然也可以,由于注解可能是今后的标准方向,相关的例子都回使用注解方式。

事前确认

Hibernate基础:快速入门(5):删除数据_第1张图片

创建Demo演示代码

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HbDemo03 {
  public static void main(String[] args){
    try{
      SessionFactory sessionFactory = new org.hibernate.cfg.Configuration()
        .configure("hibernate.cfg.xml")
        .addAnnotatedClass(User.class)
        .buildSessionFactory();

      Session session = sessionFactory.getCurrentSession();
      System.out.println("Successfully got connection from session factory.");
      User user = new User(1001,"","");
      session.beginTransaction();
      session.delete(user);
      session.getTransaction().commit();

      sessionFactory.close();
    }catch (Exception e){
      System.out.println("Exception happened...");
      e.printStackTrace();
    }
  }
}

执行结果

十二月 12, 2016 9:19:41 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.5.Final}
十二月 12, 2016 9:19:41 下午 org.hibernate.cfg.Environment 
INFO: HHH000206: hibernate.properties not found
十二月 12, 2016 9:19:41 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager 
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
十二月 12, 2016 9:19:42 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 12, 2016 9:19:42 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]
十二月 12, 2016 9:19:42 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****}
十二月 12, 2016 9:19:42 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 12, 2016 9:19:42 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections 
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 12, 2016 9:19:42 下午 org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Successfully got connection from session factory.
Hibernate: select user_.userid, user_.country as country2_0_, user_.username as username3_0_ from hbtableuser user_ where user_.userid=?
Hibernate: delete from hbtableuser where userid=?
十二月 12, 2016 9:19:43 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]

Hibernate基础:快速入门(5):删除数据_第2张图片

事前确认

Hibernate基础:快速入门(5):删除数据_第3张图片

创建Demo演示代码

删除和更新还可以使用另外一种方法来进行。代码如下

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HbDemo03 {
  public static void main(String[] args){
    try{
      SessionFactory sessionFactory = new org.hibernate.cfg.Configuration()
        .configure("hibernate.cfg.xml")
        .addAnnotatedClass(User.class)
        .buildSessionFactory();

      Session session = sessionFactory.getCurrentSession();
      System.out.println("Successfully got connection from session factory.");

      session.beginTransaction();
      session.createQuery("delete from User where userid = 1001").executeUpdate();
      session.getTransaction().commit();

      sessionFactory.close();
    }catch (Exception e){
      System.out.println("Exception happened...");
      e.printStackTrace();
    }
  }
}

注意delete语句的Table名称

执行结果

十二月 12, 2016 9:22:28 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.5.Final}
十二月 12, 2016 9:22:28 下午 org.hibernate.cfg.Environment 
INFO: HHH000206: hibernate.properties not found
十二月 12, 2016 9:22:29 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager 
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
十二月 12, 2016 9:22:29 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 12, 2016 9:22:29 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]
十二月 12, 2016 9:22:29 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****}
十二月 12, 2016 9:22:29 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 12, 2016 9:22:29 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections 
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 12, 2016 9:22:29 下午 org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Successfully got connection from session factory.
十二月 12, 2016 9:22:30 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: delete from hbtableuser where userid=1001
十二月 12, 2016 9:22:30 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]

Hibernate基础:快速入门(5):删除数据_第4张图片

你可能感兴趣的:(#,Hibernate,#,持久层框架快速入门)