Save object and link ing

Source: Internet
Author: User

The first is the event class,

 

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;}}

The corresponding hibernate ing file is event. HBM. xml.

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

There are many-to-many associations between events and people.

The person class is:

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);}}

The corresponding hibernate configuration file person. HBM. XML is:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

Here, it also shows the many-to-many relationship between people and events. At the same time, the e-mail address element of the added person is represented by a set, indicating that there may be multiple emails.

The following operations are performed on the two defined entities.

First, create an event instance and save the specific data to the database:

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();}

Similarly, operations on the person instance:

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();}

The above two methods only save data and do not establish association.

Add an association for an event:

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();}

Add associations for people, targeting the relationship between people and 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();}

When adding an association for an event, you can also perform operations using two different transactions:

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");}

Additional instructions:

To obtain the Object List, perform the following operations:

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;}

Note: In "from event", event is the class name of the object, instead of the Table Name of the database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.