首先是Event类,
public class Event { private int id; private String title; private Date date; private Set participants = new HashSet(); public Event() {} public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Set getParticipants() { return participants; } public void setParticipants(Set participants) { this.participants = participants; }}
其对应的hibernate映射文件为:Event.hbm.xml
事件与人有多对多的关联。
Person类为:
public class Person { private int id; private int age; private String firstname; private String lastname; private Set events = new HashSet(); private Set emailAddresses = new HashSet(); public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public Set getEvents() { return events; } public void setEvents(Set events) { this.events = events; } public void setId(int id) { this.id = id; } public int getId() { return id; } public Set getEmailAddresses() { return emailAddresses; } public void setEmailAddresses(Set emailAddresses) { this.emailAddresses = emailAddresses; } @SuppressWarnings("unchecked") public void addToEvnet(Event event){ this.getEvents().add(event); event.getParticipants().add(this); } public void removeFromEvent(Event event){ this.getEvents().remove(event); event.getParticipants().remove(this); }}
其对应的hibernate配置文件Person.hbm.xml为:
这里,也体现出人与事件多对多的关系,同时,还加入的人的email地址的元素,用集合表示,说明有一个可能有多个email。
下面就要对定义的两个实体进行操作。
首先,创建一个Event实例,并将具体数据存入数据库:
private void createAndStoreEvent(String title, Date theDate){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event event = new Event(); event.setDate(theDate); event.setTitle(title); session.save(event); session.getTransaction().commit(); //HibernateUtil.getSessionFactory().close(); }
同样,对Person实例的操作:
private void createPerson(int age,String firstname,String lastname){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person person = new Person(); person.setAge(age); person.setFirstname(firstname); person.setLastname(lastname); session.save(person); session.getTransaction().commit(); }
上面两个方法只是简单的保存数据,并未建立关联。
为事件添加关联:
private void addPersonToEvent(int personId,int eventId){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person person = (Person)session.load(Person.class, personId); Event event = (Event)session.load(Event.class, eventId); person.getEvents().add(event); session.getTransaction().commit(); }为人添加关联,针对于人与email的关系:
private void addEmailToPerson(int personId,String emailAddress){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person person = (Person)session.load(Person.class, personId); person.getEmailAddresses().add(emailAddress); session.getTransaction().commit(); }
在为事件添加关联,也可以使用两个不同的事务进行操作:
private void addPersonToEvent2(int personId,int eventId){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person person = (Person)session.createQuery("select p from Person p left join fetch p.events where p.id=:pid").setParameter("pid", personId).uniqueResult(); Event event = (Event)session.load(Event.class, eventId); session.getTransaction().commit(); //End of first unit of work; person.getEvents().add(event); //begin second unit of work; Session session2 = HibernateUtil.getSessionFactory().getCurrentSession(); session2.beginTransaction(); session2.update(person); session2.getTransaction().commit(); System.out.println("finish"); }补充说明一下:
获取实体列表操作如下:
private List getEventList(String title, Date theDate){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List list = session.createQuery("from Event").list(); session.getTransaction().commit(); return list; }
说明:"from Event"中Event为实体的类名,而不数据库的表名。