关于HQL
HQL与SQL非常类似,只不过SQL的操作对象是数据表,列等对象,而HQL操作的是持久化类,实例,属性等。
HQL是完全面向对象的查询语言,因此也具有面向对象的继承,多态等特性。
使用HQL的一般步骤为:
获取session对象
编写HQL语句
使用session的createQuery方法创建查询对象(Query对象)
使用SetXxx(index/para_name, value)为参数复制
使用Query对象的list()方法返回查询结果列表(持久化实体集)
下面演示一下HQL的基本用法,演示之前先附上之前的一个例子,双向N-N关联映射,
假设有下面两个持久化类Person和Event之间成N-N双向关联,代码如下,
Person类
1 package hql; 2 3 import java.util.*; 4 5 import javax.persistence.*; 6 7 8 @Entity 9 @Table(name = "person_inf") 10 public class Person 11 { 12 @Id @Column(name = "person_id") 13 @GeneratedValue(strategy=GenerationType.IDENTITY) 14 private Integer id; 15 private String name; 16 private int age; 17 @ManyToMany(cascade=CascadeType.ALL, targetEntity=MyEvent.class) 18 @JoinTable(name = "person_event" , 19 joinColumns = @JoinColumn(name = "person_id" 20 , referencedColumnName="person_id"), 21 inverseJoinColumns = @JoinColumn(name = "event_id" 22 , referencedColumnName="event_id") 23 ) 24 private Set<MyEvent> myEvents 25 = new HashSet<>(); 26 @ElementCollection(targetClass=String.class) 27 @CollectionTable(name="person_email_inf", 28 [email protected](name="person_id" , nullable=false)) 29 @Column(name="email_detail" , nullable=false) 30 private Set<String> emails 31 = new HashSet<>(); 32 33 public void setId(Integer id) 34 { 35 this.id = id; 36 } 37 public Integer getId() 38 { 39 return this.id; 40 } 41 42 public void setName(String name) 43 { 44 this.name = name; 45 } 46 public String getName() 47 { 48 return this.name; 49 } 50 51 public void setAge(int age) 52 { 53 this.age = age; 54 } 55 public int getAge() 56 { 57 return this.age; 58 } 59 60 public void setMyEvents(Set<MyEvent> myEvents) 61 { 62 this.myEvents = myEvents; 63 } 64 public Set<MyEvent> getMyEvents() 65 { 66 return this.myEvents; 67 } 68 69 public void setEmails(Set<String> emails) 70 { 71 this.emails = emails; 72 } 73 public Set<String> getEmails() 74 { 75 return this.emails; 76 } 77 public Person() {} 78 public Person(String name, int age) { 79 this.name = name; 80 this.age = age; 81 } 82 83 }
Event类
1 package hql; 2 3 import java.util.*; 4 5 import javax.persistence.*; 6 7 @Entity 8 @Table(name="event_inf") 9 public class MyEvent 10 { 11 @Id @Column(name="event_id") 12 @GeneratedValue(strategy=GenerationType.IDENTITY) 13 private Integer id; 14 private String title; 15 private Date happenDate; 16 @ManyToMany(targetEntity=Person.class , mappedBy="myEvents") 17 private Set<Person> actors 18 = new HashSet<>(); 19 20 public void setId(Integer id) 21 { 22 this.id = id; 23 } 24 public Integer getId() 25 { 26 return this.id; 27 } 28 29 public void setTitle(String title) 30 { 31 this.title = title; 32 } 33 public String getTitle() 34 { 35 return this.title; 36 } 37 38 public void setHappenDate(Date happenDate) 39 { 40 this.happenDate = happenDate; 41 } 42 public Date getHappenDate() 43 { 44 return this.happenDate; 45 } 46 47 public void setActors(Set<Person> actors) 48 { 49 this.actors = actors; 50 } 51 public Set<Person> getActors() 52 { 53 return this.actors; 54 } 55 public MyEvent() {} 56 public MyEvent(String title, Date happenDate) { 57 this.title = title; 58 this.happenDate = happenDate; 59 } 60 }
PersonManager类
1 package hql; 2 3 import org.hibernate.SessionFactory; 4 import org.hibernate.Transaction; 5 import org.hibernate.Session; 6 import org.hibernate.cfg.Configuration; 7 8 import java.text.ParseException; 9 import java.text.SimpleDateFormat; 10 import java.util.Date; 11 import java.util.Set; 12 import java.util.HashSet; 13 14 public class PersonManager 15 { 16 17 public static void testPerson() throws ParseException 18 { 19 Configuration conf = new Configuration().configure(); 20 conf.addAnnotatedClass(Person.class); 21 conf.addAnnotatedClass(MyEvent.class); 22 SessionFactory sf = conf.buildSessionFactory(); 23 Session sess = sf.openSession(); 24 Transaction tx = sess.beginTransaction(); 25 Person p1 = new Person("张三",20); 26 p1.getEmails().add("[email protected]"); 27 p1.getEmails().add("[email protected]"); 28 29 30 Person p2 = new Person("李四",30); 31 p2.getEmails().add("[email protected]"); 32 33 34 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 35 MyEvent e1 = new MyEvent("大学毕业", sdf.parse("2012-06-01")); 36 MyEvent e2 = new MyEvent("参加工作", sdf.parse("2012-10-01")); 37 MyEvent e3 = new MyEvent("出国旅游", sdf.parse("2013-05-01")); 38 MyEvent e4 = new MyEvent("回家过年", sdf.parse("2013-12-20")); 39 MyEvent e5 = new MyEvent("升职加薪", sdf.parse("2014-01-01")); 40 41 p1.getMyEvents().add(e1); 42 p1.getMyEvents().add(e3); 43 p1.getMyEvents().add(e4); 44 p1.getMyEvents().add(e5); 45 46 p2.getMyEvents().add(e2); 47 p2.getMyEvents().add(e3); 48 p2.getMyEvents().add(e4); 49 50 sess.save(p1); 51 sess.save(p2); 52 53 tx.commit(); 54 sess.close(); 55 } 56 57 public static void main(String[] args) throws ParseException { 58 testPerson(); 59 } 60 }
执行上面的
时间: 2024-10-10 00:48:37