Save object-relational mapping

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

}

Hibernate corresponding mapping 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">

<hibernate-mapping package="cjq.hibernate.tutorial.domain">
	<class name="Event" table="HI_EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
        <set name="participants" table="HI_PERSON_EVENT">
        	<key column="EVENT_ID"/>
        	<many-to-many column="PERSON_ID" class="PERSON"/>
        </set>
    </class>
</hibernate-mapping>

Events associated with many to many people.

Person class:

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

}

Corresponding to hibernate Person.hbm.xml profile:

<?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">

<hibernate-mapping package="cjq.hibernate.tutorial.domain">
	<class name="Person" table="HI_PERSON">
        <id name="id" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <property name="age"/>
        <property name="firstname"/>
        <property name="lastname"/>
        <set name="events" table="HI_EVENT">
        	<key column="PERSON_ID"/>
        	<many-to-many column="EVENT_ID" class="Event"/>
        </set>
        <set name="emailAddresses" table="HI_PERSON_EMAIL_ADDR">
        	<key column="PERSON_ID"/>
        	<element type="string" column="EMAIL_ADDR"/>
        </set>
    </class>
</hibernate-mapping>


Here, too, it reflects the relationship between people and events-many, while also adding an element of people's email addresses, with the collection represents, indicating a possible multiple email.

The following definition is necessary for two entities to operate.

First, create an Event instance and specific data stored in 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, the operation of 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 simply save the data, not associated.

Add association for the 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();
		
	}


Adding human association, and human relations for the 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();
	}

In add an association for the event can be used to operate two separate 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");
		
	}


Side note:

Get a list of entities as follows:

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

Description: "from Event" in the Event class name of an entity, rather than the name of the database table.




Reproduced in: https: //my.oschina.net/u/2552902/blog/543825

Guess you like

Origin blog.csdn.net/weixin_33736048/article/details/92326603