★→→SessionFactory (org.hibernate.SessionFactory)
(mappings), session ,
(transaction)
(1) SessionFactory
: 。
SessionFactory Hibernate 、 Hibernate SQL ;
SessionFactory , ,SessionFactory 。
, 。
(2) , SessionFactory
SessionFactory , , SessionFactory ,
SessionFactory 。
, SessionFactory。
(3) Configuration
Configuration configure () , hbm.xml , classpath
hibernate.cfg.xml , :
Configuration cfg = new Configuration().configure("myexample.xml");
★★→→Configuration
Configuration hibernate , Configuration ,hibernate classpath
hibernate.properties , , Properties GLOBAL_PROPERTIES ,
, :hibernate.properties not found
(System.getProperties()) GLOBAL_PROPERTIES 。 hibernate.properties ,
, , 。
(1) --- Hibernate
? Configuration Hibernate ,
Hibernate ,Configuration , , SessionFactory 。
? Configuration SessionFactory
SessionFactory , Hibernate Configuration , 。
,Configuration SessionFactory , SessionFactory ,Configuration , 。
(2) hibernate
Configuration Hibernate , hibernate 。
(3) Configuration config = new Configuration();
config.addClass(Customer.class);
sessionFactory = config.buildSessionFactory();
:
, :
sessionFactory = new Configuration().buildSessionFactory().addClass(Customer.class).buildSessionFactory();
:
。 , , , 。
★★★→→Session(org.hibernate.Session)
(1) , , 。 JDBC Connection
,
.
hibernate . session
hibernate ,
(2) Session
Session
, ( ), Session .
, .
, Session close() , Session
.
(3) Session
SessionFactory
, SessionFactory;
, SessionFactory,
SessionFactory .
SessionFactory Session
:::::
----------------------------------------------------------------------------------
Session session=sessionFactory.openSession();
Transaction tx;
try{
tx=session.beginTransaction();//
.......//
tx.commit();
}catch(Exception e){//
if(tx!=null)
tx.rollback();
throw e;
}
finally// , Session finally
session.close();
-----------------------------------------------------------------------------------
(4) Sessin
CURD (save() : Java 、update() :
Java 、delete() : Java 、load() :
Java find() : Java ); ;
◆ save() :save() persist() SQL INSERT
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
Book oneBook = new Book();
oneBook.setBookName(new String("J2EE ".getBytes("gb2312"), "ISO8859-1"));
oneBook.setBookKind('1');
oneBook.setBookPrice(7.4f);
session.save(oneBook);
tx.commit();
◆? Sessin ---- updata() merge() SQL UPDATE
Session load() , Customer , Customer 。
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
Book oneBook=(Book)session.load(Book.class,bookID);
oneBook.setBookName(new String("Java ".getBytes("gb2312"), "ISO8859-1"));
oneBook.setBookKind('1');
oneBook.setBookPrice(10.4f);
tx.commit();
◆? Sessin ---- load get
session load get OID ,load notFoundException ,get null;
get load session , , session , 。
,load() get() OID
public Iterator getAllCourses()throws HibernateException
{
String queryString = "select courses from Course as courses";
beginTransaction();
Query query = session.createQuery(queryString);
Iterator it= query.iterate();
return it;
}
/**
* course , Course Iterator。
*/
public Iterator getSomeCourse(String name)throws HibernateException
{
String queryString = "select c from Course as c where c.name like :name" ;
beginTransaction();
Query query = session.createQuery(queryString);
query.setString("name", "%"+name+"%");
Iterator it= query.iterate();
return it;
}
◆? Sessin ---- delete() SQL DELETE
, session delete 。
, , delete 。session delete 。
session close() session 。
session = HibernateUtil.currentSession();s
tx = session.beginTransaction();
Book oneBook=(Book)session.load(Book.class,bookID);
session.delete(oneBook);
tx.commit();
★★★★→→Transaction
★★★★★→→Query
Query , :HQL SQL 。
Query 、 , 。
Query query=session.createQuery();
List list = query.list();// query Query list , query
★★★★★★Callback
―― 、 、 ,Callback Hibernate ---
。 Interceptor、Lifecycle Validatable 。 ,
Callback , , 。
◎ .----> Sessionfactory
Sessionfactory sf=new Configuration().configure().buildSessionFactory();
◎ .----> session
Session session=sf.openSession();
◎ .---->
Transaction tx=session.beginTransaction();
◎ .----> session . :session.save( bean );session.update( bean ) ;
◎ .---->tx.commit(); ;
◎ . Query query=session.createQuery("from student where username='"+username"'");//student bean . hbm.xml javabean.