Hello,
I'am a using hibernate for the first time in my application, so I have no experience in using hibernate.
I did the tutorial [url]1.2.Part 1 - The first Hibernate Application[/url] and it works fine, the tables become created (Person - Person_Event - Event -> m:n connetion)
So my question is, how can I fill the tables, is it the best way first to insert a person with code 3., than to insert an event with code 2. and than to connect this two with code 1..
Is that the best way in this case (m:n) to insert a tuple.
Thanks for the answers,
All the best,
Michael
1.
Code:
Long eventId = mgr.createAndStoreEvent("My Event");
Long personId = mgr.createAndStorePerson("Foo", "Bar");
mgr.addPersonToEvent(personId, eventId);
System.out.println("Added person " + personId + " to event " + eventId);
2.
Code:
private Long createAndStoreEvent(String title) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Connection theEvent = new Connection();
theEvent.setConnectionName(title);
session.save(theEvent);
session.getTransaction().commit();
return theEvent.getId();
}
3.
Code:
private Long createAndStorePerson(String firstname, String lastname) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
MappingPool thePerson = new MappingPool();
thePerson.setName(firstname);
thePerson.setZustand(lastname);
session.save(thePerson);
session.getTransaction().commit();
return thePerson.getId();
}