hibernate_Day1

hibernate学习第一天。

hibernate主要是完成数据库连接的操作封装。

需要的配置文件有:hibernate.cfg.xml( src下 ),   实体类名.hbm.xml

hibernate.cfg.xml配置如下:


"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">



 
     
      com.mysql.jdbc.Driver
      jdbc:mysql://localhost:3306/test?characterEncoding=utf8
      root
      111111

     
      org.hibernate.dialect.MySQL5InnoDBDialect


     
      true
     
      update
      



 



User.hbm.xml


"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">













@Test

public void deleteTest() {
Configuration configuration = new Configuration();
configuration.configure();

SessionFactory sessionFactory = 
configuration.buildSessionFactory(
new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

session.delete(session.get(User.class, 8));

transaction.commit();
session.close();

}

注意出错的地方:

1.老是在测试的时候忘记加@Test。

2.写配置文件的url时,因数据库和eclips编码格式不一样,导致系统报错如下:

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

解决办法:url后加 ?characterEncoding=utf8











你可能感兴趣的:(hibernate框架)