[Hibernate] - Study 1
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的更多相关文章
- [Hibernate] - Study test project
最近玩Hibernate的测试代码工程: http://files.cnblogs.com/HD/TestHibernate.7z
- Hibernate(二)——一对多查询
1. 前言 本章节我们讨论Hibernate一对多查询的处理. 在上一章节中(Hibernate(一)——入门),我们探讨了Hibernate执行最基本的增删改查操作.现在我们将情况复杂化:加入我们在 ...
- Hibernate(一)——入门
1. 前言 Hibernate是一个开放源代码的ORM持久化框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. ...
- Hibernate---数据操作示例BY实体类注释
通过实体的映射文件创建表的示例,除了基本jar包外,还需要jar包如下 ejb3-persistence.jar.hibernate-annotations.jar这两个包均在hibernate-an ...
- Hibernate---数据操作示例BY实体映射文件
创建一个Student.java类:该类需要一个无参的构造函数,以及属性的get/set方法 public class Student implements Serializable { privat ...
- Hibernate实现向数据库插入一条数据全过程(Study By Example)
1.数据库(直接在cmd下进入数据库操作亦可) (1)启动Navicat for MySQL (2)打开连接,创建一个数据库,名为testdb (3)新建表user1,表结构如图所示 2.数据库池 ( ...
- hibernate 入门
工程截图 1.jar包 和 hibernate配置文件 /src/hibernate.cfg.xml , /src/log4j.properties , /src/db.sql < ...
- java.lang.ExceptionInInitializerError /NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nati ...
- Hibernate框架简单应用
Hibernate框架简单应用 Hibernate的核心组件在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.proper ...
随机推荐
- Xen虚拟机磁盘镜像模板制作(一)—Windows Server 2008(2012)
这段时间一直在研究如何制作一个适合Xen虚拟化的Windows Server 2008(2012)磁盘镜像,中间虽然遇到了一些阻挠,不过最终还是顺利解决,成功制作出了Xen Windows Serve ...
- PAT (Basic Level) Practise:1008. 数组元素循环右移问题
[题目连接] 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… A ...
- log4j常见问题
1.日志文件不能生成日期后缀 需求:想要生成的文件是以日期格式为后缀或者结尾的. 设置: log4j.appender.appenderName.DatePattern='.'yyyy-MM-dd 也 ...
- 元素设置position:fixed属性后IE下宽度无法100%延伸
元素设置position:fixed属性后IE下宽度无法100%延伸 IE bug 出现条件: 1.div1设置position:fixed属性,并且想要width:100%的效果. 2.div2(下 ...
- ehcache memcache redis -- java中的三大缓存
三个缓存在java代码中用的是较多的,但是它们都有自己的应用场合,和优缺点. Ehcache 1.初衷:减少数据库操作的高延时而设计.(缓存的目的,好像都是这个吧) 2.Apache Licen ...
- 319. Bulb Switche
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- python 函数基础
定义: def intersect(seq1,seq2): res = [] for x in seq1: if x in seq2: res.append(x) yield res 运行效果 > ...
- C++ Primer : 第九章 : 顺序容器的定义、迭代器以及赋值与swap
顺序容器属于C++ STL的一部分,也是非常重要的一部分. 顺序容器包括: std::vector,包含在头文件<vector>中 std::string, 包含在头文件<strin ...
- Discuz论坛搭建过程
1.系统环境 操作系统版本:CentOS Linux 5.7 内核版本:2.6.18-274.el5 arch:x86_64 apache版本:Apache/2.4.6 (Unix) mysql版本 ...
- scala言语基础学习六
trait的学习 1.将trait作为接口来使用 trait中可以实现一些方法,子类只要extends 了trait就可以直接使用父trait里面的方法 普通继承的话父类和子类之间的field是通过引 ...