從以下5個方面學習hibernate ORM。
(1)配置文件:hibernate.cfg.xml XML文件和hibernate.properties屬性文件
(2)實體映射:1對多、多對多
(3)會話工廠與會話:SessionFactory&Session
java web實戰,(4)查詢:SQL原生查詢、HQL通用查詢、Criteria條件查詢
(5)事務:Transanction
Hibernate的5個核心對象Conifguration、SessionFactory、Session、Query和Transanction是必須掌握的。另外,沒有類似Linq的語言集成查詢。
1.配置文件:hibernate.cfg.xml XML文件和hibernate.properties屬性文件
Hibernate使用Configuration表示配置信息,配置文件的信息最終會適配到Configuration對象。雖然XML文件一直被hibernate支持,但使用hibernate.properties屬性文件更簡潔。
HSQLDB數據庫是一個常用的JAVA版的測試數據庫,我們通過下面兩種方式演示HSQLDB數據庫的配置。其中connection.driver_class, connection.url, connection.username 和 connection.password提供了JDBC使用的數據庫鏈接信息,dialect配置SQL方言,hbm2ddl.auto配置啟用自動更新數據庫模式,show_sql和format_sql配置便于我們在控制臺查看輸出信息,generate_statistics配置生成統計信息。
Web站點基礎階段包含哪些階段、(1)XML方式配置Hibernate:
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <property name="hibernate.connection.driver_class">org.h2.Driver</property> 9 <property name="hibernate.connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property> 10 <property name="hibernate.connection.username">sa</property> 11 <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property> 12 <property name="hibernate.hbm2ddl.auto">update</property> 13 <property name="hibernate.show_sql">true</property> 14 <property name="hibernate.format_sql">true</property> 15 <property name="hibernate.generate_statistics">true</property> 16 </session-factory> 17 </hibernate-configuration>
?
(2)屬性文件方式配置Hibernate:
1 hibernate.connection.driver_class org.h2.Driver 2 hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE 3 hibernate.connection.username sa 4 hibernate.dialect org.hibernate.dialect.H2Dialect 5 hibernate.hbm2ddl.auto update 6 hibernate.show_sql true 7 hibernate.format_sql true 8 hibernate.generate_statistics true
2.實體映射:1對多、多對多
Hibernate的實體映射可以采取XML和代碼注解兩種, .NET中的EntityFramework的實體映射也有對應的注解(特性)方式,但提供了讓實體類更加干凈的代碼配置方式。無論是依賴注入還是實體映射,Spring和Hibernate在這方面始終相對落后和繁瑣。
各種JAVA框架的核心從來不是xml,框架的核心功能和核心對象才是最重要的。注解配置的核心注解如下:
idea javaweb、(1)@Entity:標注類為實體。
(2)@Id和@GeneratedValue:前者標注POJO字段為主鍵,后者標注字段為數據庫自動生成。
(3)@OneToMany和@ManyToOne:在關聯字段上標注1對多和多對1。
(4)@ManyToMany:在關聯字段上標注多對多,cascade參數指定級聯處理規則。
(5)@Version:標注字段為樂觀并發控制版本字段。
java的web開發?下面分別演示常見的1對多、多對多的映射配置。
(1)1對多:Category-Post
Category代碼:
1 @Entity 2 public class Category { 3 4 @Id 5 @GeneratedValue 6 private int id; 7 8 private String Name; 9 10 @OneToMany 11 private List<Post> posts = new ArrayList<Post>(); 12 13 public int getId() { 14 return id; 15 } 16 17 public void setId(int id) { 18 this.id = id; 19 } 20 21 public String getName() { 22 return Name; 23 } 24 25 public void setName(String name) { 26 Name = name; 27 } 28 29 public List<Post> getPosts() { 30 return posts; 31 } 32 33 public void setPosts(List<Post> posts) { 34 this.posts = posts; 35 } 36 }
Post代碼:
@Entity public class Post {@Id@GeneratedValueprivate int id;private String Name;private String Text;@ManyToOneprivate Category category;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return Name;}public void setName(String name) {Name = name;}public String getText() {return Text;}public void setText(String text) {Text = text;}public Category getCategory() {return category;}public void setCategory(Category category) {this.category = category;} }
?
web前端開發基礎期末?(2)多對多+樂觀鎖:User-Role
User代碼:
1 @Entity 2 public class User { 3 @Id 4 @GeneratedValue 5 private int id; 6 7 private String userName; 8 9 private String password; 10 11 @Version 12 private long version; 13 14 @ManyToMany(cascade = CascadeType.ALL) 15 private List<Role> roles = new ArrayList<Role>(); 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getUserName() { 26 return userName; 27 } 28 29 public void setUserName(String userName) { 30 this.userName = userName; 31 } 32 33 public String getPassword() { 34 return password; 35 } 36 37 public void setPassword(String password) { 38 this.password = password; 39 } 40 41 public long getVersion() { 42 return version; 43 } 44 45 public void setVersion(long version) { 46 this.version = version; 47 } 48 49 public List<Role> getRoles() { 50 return roles; 51 } 52 53 public void setRoles(List<Role> roles) { 54 this.roles = roles; 55 } 56 57 }
Role代碼:
1 @Entity 2 public class Role { 3 @Id 4 @GeneratedValue 5 private int id; 6 7 private String roleName; 8 9 @ManyToMany(cascade = CascadeType.ALL) 10 private List<User> users = new ArrayList<User>(); 11 12 public int getId() { 13 return id; 14 } 15 16 public void setId(int id) { 17 this.id = id; 18 } 19 20 public String getRoleName() { 21 return roleName; 22 } 23 24 public void setRoleName(String roleName) { 25 this.roleName = roleName; 26 } 27 28 public List<User> getUsers() { 29 return users; 30 } 31 32 public void setUsers(List<User> users) { 33 this.users = users; 34 } 35 }
?
3.會話工廠與會話:SessionFactory&Session
(1)會話上下文SessionFactory
Web開發、SessionFactory始終是Hibernate的核心對象.通過Configuration創建的SessionFactory是Hibernate ORM的核心對象。Hibernate 4.3.5和Hibernate 5.x可以使用一致的代碼創建SessionFactory,但5.x需要引入jta(javax.transaction),否則創建失敗。
1 public SessionFactory sessionFactory() { 2 3 org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration(); 4 5 configuration.addAnnotatedClass(User.class); 6 configuration.addAnnotatedClass(Role.class); 7 configuration.addAnnotatedClass(Category.class); 8 configuration.addAnnotatedClass(Post.class); 9 10 SessionFactory sessionFactory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().build()); 11 return sessionFactory; 12 13 }
(2)會話Session
Session對象類似于EntityFramework中DbContext對象。Hibernate中通過SessionFactory獲取Session,有2種方式openSession()和 getCurrentSession()。openSession方式獲取單個打開的Session,需要自己寫代碼關閉。getCurrentSession方式則可以獲取自動管理的Session對象,這是依賴CurrentSessionContext接口的實現類來支持的,可以通過配置hibernate.current_session_context_class來適配,取值"jta","thread"和"managed"分別對應三個實現類。使用getCurrentSession時雖然不需要手動管理Session的關閉,但是需要手動管理Transaction事務的開啟和關閉。在Spring中繼承Hibernate時,Spring提供了CurrentSessionContext的實現類SpringJtaSessionContext,避免了我們手動管理事務。在不使用Spring的Servlet環境中,我們可以選擇使用Filter+getSession方式使用Session。也可以配置成"thread"+手動管理事務方式。
Filter+getSession可以直接使用click-extras程序包中的Filter和SessionContext,最好是復用并修改其源碼,其中SessionContext的實現核心是使用類型為ThreadLocal<Session>的靜態字段實現線程級別的Session共享。
click-extras的pom如下:
1 <dependency> 2 <groupId>org.apache.click</groupId> 3 <artifactId>click-extras</artifactId> 4 <version>2.3.0</version> 5 </dependency>
Web開發基礎、使用"thread"+手動管理事務方式需要先配置hibernate.properties屬性文件:hibernate.current_session_context_class thread。
1 private void Test(SessionFactory factory) 2 { 3 factory.getCurrentSession().beginTransaction(); 4 Query query = factory.getCurrentSession().createSQLQuery("select * from User where userName=?").addEntity(User.class); 5 User user = (User) query.uniqueResult(); 6 factory.getCurrentSession().getTransaction().commit(); 7 }
4.查詢:SQL原生查詢、HQL通用查詢、Criteria條件查詢
(1)SQL原生查詢:
原生查詢使用Query接口的子接口SQLQuery。通過session可以創建該接口的實例。下面的代碼中hsqldb的參數化查詢占位符是"?"。為了便于使用,使用了SQLQuery的addEntity方法配置查詢對應的實體類型。
1 private User SqlQuery() { 2 Session session = SessionContext.getSession(); 3 Query query = session.createSQLQuery("select * from User where userName=?").addEntity(User.class); 4 query.setString(0, "admin"); 5 return (User) query.uniqueResult(); 6 }
(2)HQL通用查詢:
Hibernate使用Query接口,通過自定義的HQL實現通用查詢,HQL提供了一個中間語言,屏蔽了不同數據庫的語法差異。通過session可以創建Query接口的實例。
1 private User SqlQuery() { 2 Session session = SessionContext.getSession(); 3 Query query = session.createQuery("from User where userName=:userName"); 4 query.setString("userName", "admin"); 5 return (User) query.uniqueResult(); 6 }
Web。(3)Criteria條件查詢:
Hibernate通過Criteria對象提供對自動化查詢的方法級別的支持,輔助類Restrictions提供了大量靜態方法創建Criteria對象,最大的作用就是防止寫錯SQL關鍵字。Java中沒有類似.NET中Linq一樣的語言集成查詢。
1 private User CriteriaQuery() { 2 Session session = SessionContext.getSession(); 3 Criteria query = session.createCriteria(User.class); 4 query.add(Restrictions.eq("userName", "admin")); 5 return (User) query.uniqueResult(); 6 }
5.事務
?Transanction接口是Hibernate中封裝事務的接口,支持JDBC數據庫事務和JTA分布式事務,可以通過Session對象使用Transanction進行事務管理。JAT事務則與JTA容器緊密相關,以后再續。
1 private void Test(SessionFactory factory) { 2 factory.getCurrentSession().beginTransaction(); 3 Query query = factory.getCurrentSession().createSQLQuery("select * from User where userName=?") 4 .addEntity(User.class); 5 User user = (User) query.uniqueResult(); 6 factory.getCurrentSession().getTransaction().commit(); 7 }?
參考
(1)http://docs.jboss.org/hibernate/orm/5.0/quickstart/html/
(2)https://www.ibm.com/developerworks/cn/java/j-lo-jta/
java基礎編程。(3)https://www.ibm.com/developerworks/cn/java/j-lo-hibernate3/