The shortest way of using Hibernate

原创 2004年09月03日 14:57:00

The shortest way of using Hibernate

 

l         Download Hibernate 2.0

l         Open the tools of JBuilder 8.0

       Set Environment: include Hibernate.jar and all of the dir of lib/*.jar;

       add Support Database JDBC.

 

l         To create a table in your database (our using sql2000 in Test DB)

CREATE TABLE CUSTOMER
(
    CID INTEGER NOT NULL PRIMARY KEY,
    USERNAME VARCHAR(12) NOT NULL,
    PASSWORD VARCHAR(12)
);

l         To create a Class of Customer ( add methods of setting and getting in the class)

public class Customer {

    private int id;

    private String username;

    private String password;

}

 

l         To create a Class to execute what you wanna do

import net.sf.hibernate.*;

import net.sf.hibernate.cfg.*;

 

public class Test {

    public static void main(String[] args) {

        try {

          SessionFactory sf =new Configuration().configure().buildSessionFactory();

          Session session = sf.openSession();

          Transaction tx = session.beginTransaction();

 

          for (int i = 0; i < 200; i++) {

            Customer customer = new Customer();

            customer.setUsername("customer" + i);

            customer.setPassword("customer");

            session.save(customer);

            /*customer.setId(i+1);

            session.delete(customer);*/

          }

          tx.commit();

          session.close();

        } catch (HibernateException e) {

          e.printStackTrace();

        }

    }

}

 

l         To create a file, the file name is Customer.hbm.xml, put it to the same of classes directory.

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

 

<hibernate-mapping>

    <class name="Customer" table="test.dbo.customer">

        <id name="id" column="CID">

            <generator class="increment" />

        </id>

        <property name="username" column="USERNAME" />

        <property name="password" column="PASSWORD" />

    </class>

</hibernate-mapping>

 

l         To create a file, the file name is hibernate.cfg.xml, put it to the same of classes directory.

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE hibernate-configuration

    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

 

<hibernate-configuration>

    <session-factory name="java:/hibernate/HibernateFactory">

        <property name="show_sql">true</property>

        <property name="connection.driver_class">

            com.microsoft.jdbc.sqlserver.SQLServerDriver

        </property>

        <property name="connection.url">

            jdbc:microsoft:sqlserver://127.0.0.1:1433

        </property>

        <property name="connection.username">

            sa

        </property>

        <property name="connection.password">

            123

        </property>

        <property name="dialect">

            net.sf.hibernate.dialect.SQLServerDialect

        </property>

        <mapping resource="Customer.hbm.xml" />

    </session-factory>

</hibernate-configuration>

 

 

l         OK, just run the program of Test.

 

Leetcode全字符问题

目录 1、编号3 Longest Substring Without Repeating Characters 2、编号6 ZigZag Conversion 3、编号20 Valid Parent...
  • wangxiaojun911
  • wangxiaojun911
  • 2014年02月07日 08:09
  • 2824

你写论文时发现了哪些神网站?

文理科皆可,本硕博不限,专业不限。只要给你写论文带来便利的,能给人相见恨晚之感的都可以说说。 (违法乱纪的不要说。) 周不润 ,神经科学 收录于 编辑推荐 •18860 人赞同 神网站...
  • GarfieldEr007
  • GarfieldEr007
  • 2016年05月26日 12:54
  • 5421

iOS项目被拒原因总结及解决办法

- 情况one: 被拒原因:类似App store, 或者基于购买或者促销的目的而展示其他应用的应用将会被拒绝。详情: 2.25 Details The 应用推荐 feature in yo...
  • ai379558502
  • ai379558502
  • 2015年10月16日 17:31
  • 2605

多路歸併堆排序 K-way sorting using Minium Heap

// For K-Way sorting typedef struct tagHeapData { int m_data; int m_listID; int m_pos; tagHe...
  • lesliealantam
  • lesliealantam
  • 2013年11月25日 21:57
  • 699

Graph problem of Island of shortest road

  • 2012年11月12日 16:52
  • 3KB
  • 下载

源码 for The.Right.Way.Using.HTML.&amp;.CSS[用正确的方法构建自己的网站]

  • 2009年09月24日 23:45
  • 8.96MB
  • 下载

杭电-1595find the longest of the shortest(记录路径最短路径中最长路)

find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32...
  • nvliba
  • nvliba
  • 2015年11月27日 22:18
  • 372

HDU1595 find the longest of the shortest(最短路,Dijkstra)

题目: find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 3...
  • riba2534
  • riba2534
  • 2017年07月18日 10:14
  • 120

HDU1595find the longest of the shortest(SPFA,删边求最短路中的最长路)

find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/3276...
  • u010372095
  • u010372095
  • 2015年03月31日 18:50
  • 466

【HDU】1595 find the longest of the shortest 枚举+最短路

传送门:【HDU】1595 find the longest of the shortest
  • u013368721
  • u013368721
  • 2014年08月20日 17:45
  • 357
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The shortest way of using Hibernate
举报原因:
原因补充:

(最多只允许输入30个字)