Hibernate删除表记录示例代码,hibernate示例代码,package cn.o
分享于 点击 16014 次 点评:88
Hibernate删除表记录示例代码,hibernate示例代码,package cn.o
package cn.outofmemory.snippets.enterprise;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class DeletePersistentObjectWithHibernate { public static void main(String[] args) { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.getCurrentSession(); long id = 2; try { session.beginTransaction(); Employee employee = (Employee) session.get(Employee.class, id); session.delete(employee); session.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); session.getTransaction().rollback(); } }}
package cn.outofmemory.snippets.enterprise;import java.util.Date;public class Employee { private Long id; private String name; private String surname; private String title; private Date created; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; }}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- JDBC connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/companydb</property> <property name="connection.username">jcg</property> <property name="connection.password">jcg</property> <!-- JDBC connection pool, use Hibernate internal connection pool --> <property name="connection.pool_size">5</property> <!-- Defines the SQL dialect used in Hiberante's application --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Display and format all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <!-- Mapping to hibernate mapping files --> <mapping resource="Employee.hbm.xml" /> </session-factory></hibernate-configuration>
Employee.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"><hibernate-mapping> <class name="cn.outofmemory.snippets.enterprise.Employee" table="employee"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" not-null="true" length="50" /> <property name="surname" not-null="true" length="50" /> <property name="title" length="50" /> <property name="created" type="timestamp" /> </class></hibernate-mapping>
CREATE TABLE `companydb`.`employee` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NOT NULL, `surname` VARCHAR(45) NOT NULL, `title` VARCHAR(45) NOT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`));
输出:
Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_, employee0_.surname as surname0_0_, employee0_.title as title0_0_, employee0_.created as created0_0_ from employee employee0_ where employee0_.id=?Hibernate: delete from employee where id=?
用户点评