1)解压Hibernate,在eclipse中导入jar包,其中lib\required里的jar包是必需包括在里头的。这里用的是sql server,所以要导入sqljdbc4.jar

2)在src根目录下新建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>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://127.0.0.1;DatabaseName=DBNAME;integratedSecurity=True;</property>
<property name="connection.username"></property>
<property name="connection.password"></property> <property name="connection.pool_size">2</property>
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="show_sql">true</property> <mapping resource="com/my/test/mapping/Account.hbm.xml" />
</session-factory>
</hibernate-configuration>

3)加入Account.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="com.my.bean.Account" table="PPM_Account">
<id name="accountId" type="java.lang.String">
<column name="AccountId" />
<generator class="assigned" />
</id>
<property name="targetType" type="java.lang.String" length="30">
<column name="TargetType" />
</property>
</class>
</hibernate-mapping>

4)加入bean:Account.java

package com.my.bean;

public class Account {

    private int id;
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String accountId;
public String getAccountId() {
return accountId;
} public void setAccountId(String accountId) {
this.accountId = accountId;
} private String targetType;
public String getTargetType() {
return targetType;
} public void setTargetType(String targetType) {
this.targetType = targetType;
} }

5)测试Hibernate:

package com.my.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.my.bean.Account; public class TestHibernate { @SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration cfg = new Configuration();
@SuppressWarnings("deprecation")
SessionFactory factory = cfg.configure().buildSessionFactory();
Session session = factory.openSession();
org.hibernate.Transaction trans = session.beginTransaction(); String hql = "from Account";
Query query = session.createQuery(hql);
List<Account> list = query.list(); trans.commit();
session.close(); for (Account account : list) {
System.out.println(account.getTargetType());
} } }

也可以使用HSQL的Select来写:

package com.my.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.my.bean.Account; public class TestHibernate { @SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration cfg = new Configuration();
@SuppressWarnings("deprecation")
SessionFactory factory = cfg.configure().buildSessionFactory();
Session session = factory.openSession();
org.hibernate.Transaction trans = session.beginTransaction(); String hql = "select new Account(accountId, targetType) from Account";
Query query = session.createQuery(hql);
List<Account> list = query.list(); trans.commit();
session.close(); for(Account account : list){
System.out.println(account.getAccountId());
} } }

HSQL的Where条件可以这样写:

        String hql = "select new Account(accountId, targetType) from Account where accountId=:accountId";
Query query = session.createQuery(hql);
query.setParameter("accountId", "7AC8352C-9F6B-4B06-A481-FFEFAC7B3E7D");
List<Account> list = query.list();

[Hibernate] - Study 1的更多相关文章

  1. [Hibernate] - Study test project

    最近玩Hibernate的测试代码工程: http://files.cnblogs.com/HD/TestHibernate.7z

  2. Hibernate(二)——一对多查询

    1. 前言 本章节我们讨论Hibernate一对多查询的处理. 在上一章节中(Hibernate(一)——入门),我们探讨了Hibernate执行最基本的增删改查操作.现在我们将情况复杂化:加入我们在 ...

  3. Hibernate(一)——入门

    1. 前言         Hibernate是一个开放源代码的ORM持久化框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库.         ...

  4. Hibernate---数据操作示例BY实体类注释

    通过实体的映射文件创建表的示例,除了基本jar包外,还需要jar包如下 ejb3-persistence.jar.hibernate-annotations.jar这两个包均在hibernate-an ...

  5. Hibernate---数据操作示例BY实体映射文件

    创建一个Student.java类:该类需要一个无参的构造函数,以及属性的get/set方法 public class Student implements Serializable { privat ...

  6. Hibernate实现向数据库插入一条数据全过程(Study By Example)

    1.数据库(直接在cmd下进入数据库操作亦可) (1)启动Navicat for MySQL (2)打开连接,创建一个数据库,名为testdb (3)新建表user1,表结构如图所示 2.数据库池 ( ...

  7. hibernate 入门

    工程截图 1.jar包 和 hibernate配置文件 /src/hibernate.cfg.xml    , /src/log4j.properties   ,   /src/db.sql < ...

  8. java.lang.ExceptionInInitializerError /NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;

    java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nati ...

  9. Hibernate框架简单应用

    Hibernate框架简单应用 Hibernate的核心组件在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.proper ...

随机推荐

  1. Java中的DatagramPacket与DatagramSocket的初步(转)

    1.基本概念: a.DatagramPacket与DatagramSocket位于java.net包中 b.DatagramPacket表示存放数据的数据报,DatagramSocket表示接受或发送 ...

  2. Git 基础学习篇(应用-windows篇)

    此篇教程主要是讲应用,因为理论,,,额,我也说不出来.大家要深入学习还是看廖老师的教程吧. 可以把这篇当作一个简单应用的参考,因为当初看廖老师的也难看啊!!! 以下是资料: 廖雪峰-Git教程 [Gi ...

  3. 六 GPU 并行优化的几种典型策略

    前言 如何对现有的程序进行并行优化,是 GPU 并行编程技术最为关注的实际问题.本文将提供几种优化的思路,为程序并行优化指明道路方向. 优化前准备 首先,要明确优化的目标 - 是要将程序提速 2 倍? ...

  4. (实用篇)php无限遍历目录

    使用的函数有: isset()判断某个变量是否定义 chdir() 将当前目录改变为指定的目录. opendir() 打开目录. readdir()读取目录. getcwd().获取当前目录. 还用到 ...

  5. 转载:LBP代码详细注释

    %LBP returns the local binary pattern image or LBP histogram of an image.% J = LBP(I,R,N,MAPPING,MOD ...

  6. 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  7. error when loading the sdk 发现了元素 d:skin 开头无效内容 转自http://blog.csdn.net/yueqinglkong/article/details/46340571

    把devices.xml这个文件删除,再把sdk里面tools\lib下的这个文件拷贝到你删除的那个文件夹里,重启eclipse

  8. Codeforces Round #132 (Div. 2)

    A. Bicycle Chain 统计\(\frac{b_j}{a_i}\)最大值以及个数. B. Olympic Medal \(\frac{m_{out}=\pi (r_1^2-r_2^2)hp_ ...

  9. URAL 1320 Graph Decomposition(并查集)

    1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...

  10. timus 1109 Conference(二分图匹配)

    Conference Time limit: 0.5 secondMemory limit: 64 MB On the upcoming conference were sent M represen ...