hibernate 3.6 小实例 经过调试半天终于成功。刚接触3.6版本头疼的看看

1、开始……新建java project

 

project name:fuckhibernate

 

next

 

添加 用户自定义包 或建立工程后再添加

 经反复验证

用户自定义的包应包含:hibernate3.6基础程序所需包有/hibernate-distribution-3.6.0.Final/lib/required目录下所有的+hibernate3.jar核心包+slf4j-1.6.1中的slf4j-simple-1.6.1.jar,至于slf4j关于日志的,用slf4-log4j包仍然报错,所以建议用simple

 

2、ok,为了方便配置文件,我们用hibernate反向工程这样hibernate.cfg.xml配置文件大部分可以自动生成.

 

window-show view -myeclipse database -db browser  右击-new 添加mysql connector/j

 

dirvername 自己起个名,一会配置文件需引用下。

修改url    jdbc:mysql://hostname:3306/myproject

添加username   root

和password      123

加载驱动包

ok db driver连接创建成功。

 

3、给工程添加hibernate支持。声明下,手工添加支持的话,比如myeclipse 8.5最高显示hibernate 3.3所以手工添加会出现版本错误,如前面一篇文章所示。所以不要手工添加支持。

  myeclipse -project capabilities-add hibernate capabilities

我用的8.5的myeclipse所以只显示到hibernate 3.3无所谓,选3.3至于下面的添加jar包全部不要。它自动添加的是3.3的jar而我们所需的3.6的jar在第二步已经由用户自定义包添加进工程了。选user libraries就好。把前面的打钩去掉。ok

 next-hibernate config file new 一个就好。next  直接打开配置。DB Driver选项选择刚我们第二步建立的那个连接。下面自动天上咯。next生成工厂 就不用了。

生成的配置文件<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://hostname:3306/myproject</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">zhangqing</property>
   
    </session-factory>

</hibernate-configuration>

 

看到没少了mapping属性。我们先创建类

package org.zhangqing;


public class User {
      private int id;
      private String username;
      private String password;
      private String email;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
}

数据库跟这个一致。就省略了。

 

然后映射文件

 

注意 文件头 不同于配置文件,别copy一个拿来就用了。我先犯这个错误。。

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

     <class name="org.zhangqing.User" table="myusertable">
       <id name="id"><generator class="identity"/></id>
       <property name="username"/>
       <property name="password"/>
       <property name="email"/>
     </class>

</hibernate-mapping>

 

 

接下来我们的hibernate.cfg.xml里面配置mapping选项。

 

在配置文件里添加:

 

<mapping resource="org/zhangqing/User.hbm.xml"/>我是按照我的包结构写的。因地制宜哦。

 

然后添加测试类 test.java

 

package org.zhangqing;

import org.hibernate.*;
import org.hibernate.cfg.*;
public class test {
       public static void main(String args[])
       {
       
        SessionFactory sf=new Configuration().configure().buildSessionFactory();
        Session session=sf.openSession();
        Transaction tx=session.beginTransaction();
        User user=new User();
        user.setId(1);
        user.setUsername("Hibernate");
        user.setPassword("123");
       
        user.setEmail("mail@mail.com");
        session.save(user);
        tx.commit();
      
     
       }
}

 

我的目录结构:

 

 项目结构

ok运行

203 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
219 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
219 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
235 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
235 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
375 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
375 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
469 [main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
500 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : org/zhangqing/User.hbm.xml
594 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
672 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: org.zhangqing.User -> myusertable
703 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
719 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
719 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
719 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
719 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
735 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/myproject
735 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****}
1594 [main] INFO org.hibernate.cfg.SettingsFactory - Database ->
       name : MySQL
    version : 5.1.53-community
      major : 5
      minor : 1
1594 [main] INFO org.hibernate.cfg.SettingsFactory - Driver ->
       name : MySQL-AB JDBC Driver
    version : mysql-connector-java-5.1.14 ( Revision: ${bzr.revision-id} )
      major : 5
      minor : 1
1953 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
2110 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
2125 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
2125 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
2125 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2250 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
2250 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
2250 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
2250 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2250 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
2250 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
2344 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
2344 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
2360 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
2391 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
2391 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
2391 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
2391 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
2391 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
2422 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
3031 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Hibernate: insert into myusertable (username, password, email) values (?, ?, ?)

 

下面显示sql语句也是cfg.xml里面的一项设置 <property name="show_sql">true</property>

 

 

查看数据库。添加成功。至此,3.6小实例就完成了。

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值