扫码一下
查看教程更方便
如果想利用数据库特定的功能,例如查询提示或 oracle 中的 connect 关键字,我们可以使用本机 sql 来表示数据库查询。 hibernate 允许我们为所有创建、更新、删除和加载操作指定手写 sql,包括存储过程。
我们的应用程序将使用会话接口上的 createsqlquery()
方法从会话创建本机 sql 查询
public sqlquery createsqlquery(string sqlstring) throws hibernateexception
将包含 sql 查询的字符串传递给 createsqlquery()
方法后,我们可以使用 addentity()
、addjoin()
和 addscalar()
方法将 sql 结果与现有 hibernate 实体、连接或标量结果相关联。
最基本的 sql 查询是从一个或多个表中获取标量(值)列表。 以下是将本机 sql 用于标量值的语法
string sql = "select first_name, salary from employee";
sqlquery query = session.createsqlquery(sql);
query.setresulttransformer(criteria.alias_to_entity_map);
list results = query.list();
上面的查询都是关于返回标量值,基本上是从结果集中返回“原始”值。 以下是通过 addentity()
从本机 sql 查询中获取整体实体对象的语法。
string sql = "select * from employee";
sqlquery query = session.createsqlquery(sql);
query.addentity(employee.class);
list results = query.list();
以下是通过 addentity()
并使用命名 sql 查询从本机 sql 查询中获取实体对象的语法。
string sql = "select * from employee where id = :employee_id";
sqlquery query = session.createsqlquery(sql);
query.addentity(employee.class);
query.setparameter("employee_id", 10);
list results = query.list();
考虑以下 pojo 类
public class employee {
private int id;
private string firstname;
private string lastname;
private int salary;
public employee() {}
public employee(string fname, string lname, int salary) {
this.firstname = fname;
this.lastname = lname;
this.salary = salary;
}
public int getid() {
return id;
}
public void setid( int id ) {
this.id = id;
}
public string getfirstname() {
return firstname;
}
public void setfirstname( string first_name ) {
this.firstname = first_name;
}
public string getlastname() {
return lastname;
}
public void setlastname( string last_name ) {
this.lastname = last_name;
}
public int getsalary() {
return salary;
}
public void setsalary( int salary ) {
this.salary = salary;
}
}
让我们创建以下 employee 表来存储 employee 对象
create table employee (
id int not null auto_increment,
first_name varchar(20) default null,
last_name varchar(20) default null,
salary int default null,
primary key (id)
);
以下是映射文件
this class contains the employee detail.
最后,我们将使用 main()
方法创建我们的应用程序类来运行我们将使用本机 sql 查询的应用程序
import java.util.*;
import org.hibernate.hibernateexception;
import org.hibernate.session;
import org.hibernate.transaction;
import org.hibernate.sessionfactory;
import org.hibernate.sqlquery;
import org.hibernate.criteria;
import org.hibernate.hibernate;
import org.hibernate.cfg.configuration;
public class manageemployee {
private static sessionfactory factory;
public static void main(string[] args) {
try {
factory = new configuration().configure().buildsessionfactory();
} catch (throwable ex) {
system.err.println("failed to create sessionfactory object." ex);
throw new exceptionininitializererror(ex);
}
manageemployee me = new manageemployee();
/* 在数据库中添加一些员工记录 */
integer empid1 = me.addemployee("zara", "ali", 2000);
integer empid2 = me.addemployee("daisy", "das", 5000);
integer empid3 = me.addemployee("john", "paul", 5000);
integer empid4 = me.addemployee("mohd", "yasee", 3000);
/* 使用标量查询列出员工及其工资 */
me.listemployeesscalar();
/* 使用实体查询列出完整的员工信息 */
me.listemployeesentity();
}
/* 在数据库中创建员工的方法 */
public integer addemployee(string fname, string lname, int salary){
session session = factory.opensession();
transaction tx = null;
integer employeeid = null;
try {
tx = session.begintransaction();
employee employee = new employee(fname, lname, salary);
employeeid = (integer) session.save(employee);
tx.commit();
} catch (hibernateexception e) {
if (tx!=null) tx.rollback();
e.printstacktrace();
} finally {
session.close();
}
return employeeid;
}
/* 使用标量查询读取所有员工的方法 */
public void listemployeesscalar( ){
session session = factory.opensession();
transaction tx = null;
try {
tx = session.begintransaction();
string sql = "select first_name, salary from employee";
sqlquery query = session.createsqlquery(sql);
query.setresulttransformer(criteria.alias_to_entity_map);
list data = query.list();
for(object object : data) {
map row = (map)object;
system.out.print("first name: " row.get("first_name"));
system.out.println(", salary: " row.get("salary"));
}
tx.commit();
} catch (hibernateexception e) {
if (tx!=null) tx.rollback();
e.printstacktrace();
} finally {
session.close();
}
}
/* 使用实体查询读取所有员工的方法 */
public void listemployeesentity( ){
session session = factory.opensession();
transaction tx = null;
try {
tx = session.begintransaction();
string sql = "select * from employee";
sqlquery query = session.createsqlquery(sql);
query.addentity(employee.class);
list employees = query.list();
for (iterator iterator = employees.iterator(); iterator.hasnext();){
employee employee = (employee) iterator.next();
system.out.print("first name: " employee.getfirstname());
system.out.print(" last name: " employee.getlastname());
system.out.println(" salary: " employee.getsalary());
}
tx.commit();
} catch (hibernateexception e) {
if (tx!=null) tx.rollback();
e.printstacktrace();
} finally {
session.close();
}
}
}
以下是编译和运行上述应用程序的步骤。 在继续编译和执行之前,请确保已正确设置 path 和 classpath。
我们将获得以下结果,并且将在 employee 表中创建记录。
$ java manageemployee
.......various log messages will display here........
first name: zara, salary: 2000
first name: daisy, salary: 5000
first name: john, salary: 5000
first name: mohd, salary: 3000
first name: zara last name: ali salary: 2000
first name: daisy last name: das salary: 5000
first name: john last name: paul salary: 5000
first name: mohd last name: yasee salary: 3000
如果检查你的 employee 表,它应该有以下记录
mysql> select * from employee;
---- ------------ ----------- --------
| id | first_name | last_name | salary |
---- ------------ ----------- --------
| 26 | zara | ali | 2000 |
| 27 | daisy | das | 5000 |
| 28 | john | paul | 5000 |
| 29 | mohd | yasee | 3000 |
---- ------------ ----------- --------
4 rows in set (0.00 sec)