Hibernate核心API
01Session API
Session代表的是Hibernate与数据库的连接对象,不是线程安全的,定义在局部而不是全局。
1.1保存方法
- save方法
Serializable save(Object object)
Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update"
Parameters:
object - a transient instance of a persistent class
Returns:
the generated identifier
例子:
Serializable id = session.save(customer)
1.2查询方法
- get方法
<T> T get(Class<T> entityType,
Serializable id)
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance is already associated with the session, return that instance. This method never returns an uninitialized instance.)
Parameters:
entityType - The entity type
id - an identifier
Returns:
a persistent instance or null
例子:
Customer customer = session.get(Customer.class, 1l)
- load方法
<T> T load(Class<T> theClass,
Serializable id)
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed.
You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.
Parameters:
theClass - a persistent class
id - a valid identifier of an existing persistent instance of the class
Returns:
the persistent instance or proxy
例子:
Customer customer = session.load(Customer.class, 2l)
- get方法和load方法的区别
get方法:
- 采用的是立即加载,执行到这行代码的时候,就会马上发送SQL语句去查询。
- 返回的是对象本身。
- 查询一个找不到的对象的时候,返回null
load方法:
- 采用的是延迟加载(lazy懒加载),执行到这行代码的时候,不会发送SQL语句,当真正使用这个对象的时候才会发送SQL语句。
- 返回的是代理对象。利用javassist-3.18.1-GA.jar,利用javassist技术产生代理。
- 查询一个找不到的对象的时候,返回ObjecNotFoundException
1.3修改方法
void update(Object object)
Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown. This operation cascades to associated instances if the association is mapped with cascade="save-update"
Parameters:
object - a detached instance containing updated state
例子:
/*
//创建对象,进行覆盖(需要每条信息都更改,不推荐)
Customer customer = new Customer();
customer.setCust_id(1l);
...
customer.setCust_name("赵高");
session.update(customer);
*/
//先查询再修改(推荐使用)
Customer customer = session.get(Customer.class, 1l)
customer.setCust_name("赵高“);
session.update(customer)
1.4删除方法
void delete(Object object)
Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state. This operation cascades to associated instances if the association is mapped with cascade="delete"
Parameters:
object - the instance to be removed
例子:
/*
//创建对象,进行覆盖后删除(不推荐)
Customer customer = new Customer();
customer.setCust_id(1l);
session.delete(customer);
*/
//先查询再删除(推荐使用,尤其是级联删除)
Customer customer = session.get(Customer.class, 1l)
session.delete(customer)
1.5保存或更新
void saveOrUpdate(Object object)
Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).
This operation cascades to associated instances if the association is mapped with cascade="save-update"
Parameters:
object - a transient or detached instance containing new or updated state
See Also:
save(java.lang.Object), update(Object object)
例子:
/*
//保存
Customer customer = new Customer();
customer.setCust_name("李斯");
session.saveOrUpdate(customer);
*/
//更新
Customer customer = new Customer();
customer.setCust_id(1l)
customer.setCust_name("李斯");
session.saveOrUpdate(customer);
1.6查询所有
/*
//接受HQL:Hibernate Query Language
Query query = session.createQuery("from Customer"); //面向对象
List<Customer> list = query.list();
for(Customer customer:list){
System.out.println(customer);
*/
//
SQLQuery query = session.createSQLQuery("select * from cst_customer");
List<Objct[]) list = query.list();
for (Object[] objects:list){
System.out.println(Arrays.toString(objects));
}
02Transaction API
2.1commit方法
void commit()
Commit this transaction. This might entail a number of things depending on the context:
If the underlying transaction was initiated from this Transaction the Session will be flushed, unless the Session is in FlushMode.MANUAL FlushMode.
If the underlying transaction was initiated from this Transaction, commit the underlying transaction.
Coordinate various callbacks
Throws:
HibernateException - Indicates a problem committing the transaction.
2.2rollback方法
void rollback()
Rollback this transaction. Either rolls back the underlying transaction or ensures it cannot later commit (depending on the actual underlying strategy).
Throws:
HibernateException - Indicates a problem rolling back the transaction.