[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 ...
随机推荐
- 多线程编程3 - NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...
- nginx+fast-cgi+c
1. 下载fastcgi开发包,编译安装 http://www.fastcgi.com/dist/fcgi-current.tar.gz #wget http://www.fastcgi.com/di ...
- Linux Shell 命令
(1) 操作一个文件并对文件查询行进行切分处理 (或者1-) (2) 操作文件进行去重并显示重复次数 | sort | uniq -c (3) 查看总的汇总行数 | sort | uniq -c | ...
- java NIO-我们到底能走多远系列(39)
献给各位: Satisfied MindRed Hayes and Jack RhodesHow many times have you heard someone say,"If I ha ...
- windows下apache及mysql定时自动重启设置
有时候觉得,服务器运行时间过长,造成服务器内存等压力过大.因此,不用重新启动服务器的情况下,完成apache和mysql的内存释放,是非常有益处的(把重启时间设置在访问量最低的).首先,apache的 ...
- 工作中遇到的问题--实现CustomerSetting的实时更新
首先在项目运行时就初始化CustomerSettings的值,采用@Bean,默认是singtone模式,只会加载一次. @Configuration@Order(3)@EnableWebMvcSec ...
- JavaWeb学习记录总结(二十九)--Servlet\Session\Cookie\Filter实现自动登录和记住密码
一.Servlet package autologin.servlet.login; import java.io.IOException;import java.security.MessageDi ...
- Hibernate两个列作为唯一索引
<hibernate-mapping package="hjds.domain.privilege"> <class name="AdminRol ...
- 1-3-2 Windows应用程序常用消息
主要内容:介绍Windows编程中常用的消息 1.WM_LBUTTONDOWN产生单击鼠标左键的消息 lParam: 低字节包含当前光标的X坐标值 X = LOWORD(lParam); 高字节包含当 ...
- spark新能优化之序列化的持久化级别
除了对多次使用的RDD进行持久化操作之外,还可以进一步优化其性能.因为很有可能,RDD的数据是持久化到内存,或者磁盘中的.那么,此时,如果内存大小不是特别充足,完全可以使用序列化的持久化级别,比如ME ...