[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 ...
随机推荐
- 101+ Manual and Automation Software Testing Interview Questions and Answers
101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...
- timeZoneGetter
function timeZoneGetter(date) { // getTimezoneOffset 返回格林威治时间和本地时间之间的时差,以分钟为单位 var zone = -1 * date. ...
- 转载几篇关于GNU autotools的文章
http://www.laruence.com/2009/11/18/1154.html http://www.ibm.com/developerworks/cn/linux/l-makefile/ ...
- java访问webservce,保持会话,服务端保存session验证
在进行程序开发的过程中,遇到一个问题,怎么保持会话. 因为一帮进行方法调用很少涉及到即时身份验证的. 例如: 1:客户端登录后服务端保存登录用户信息: 2:客户端持有验证通过key再次请求: 3:服务 ...
- [转载]新功能:用微软的Live Writer离线写博文
原文地址:Writer离线写博文">新功能:用微软的Live Writer离线写博文作者:新浪博客 Writer离线写博文" title="[转载]新功能:用微软的 ...
- php常用配置(php.ini)
查看php配置文件的位置 # /usr/local/php/bin/php -i | head php配置文件中的注释是用;号 1.disable_functions(php要禁用的函数) phpin ...
- Codeforces Round #121 (Div. 2)
A. Funky Numbers 记\(a \le b\),枚举\(a\)即可. B. Walking in the Rain 二分时间,然后\(dp(i)\)表示是否能从1到达i. C. Dynas ...
- UVa 489 HangmanJudge --- 水题
UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...
- 使用a标签直接下载图片
通常情况下,使用a标签链接到图片,会在浏览器中打开这个图片,而不会下载 如果要直接下载这个图片,可以使用download属性配合href属性 <a href="./1.jpg" ...
- 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke 解决办法
增加IsHandleCreated 判断 if (this.IsHandleCreated) { this.Invoke(new EventHandler(delegate { ...... })); ...