Also schau mal zu dem seesion update hab ich dass hier in der hibernate tutorial gefunden
1.3.3. Working the association
Let’s bring some people and events together in a new method in EventManager:
private void addPersonToEvent(Long personId, Long eventId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
Event anEvent = (Event) session.load(Event.class, eventId);
aPerson.getEvents().add(anEvent);
session.getTransaction().commit();
}
After loading a Person and an Event, simply modify the collection using the normal collection methods. As you can see, there is no explicit call to update() or save(), Hibernate automatically detects that the collection has been modified and needs to be updated. This is called automatic dirty checking, and you can also try it by modifying the name or the date property of any of your objects. As long as they are in persistent state, that is, bound to a particular Hibernate Session (i.e. they have been just loaded or saved in a unit of work), Hibernate monitors any changes and executes SQL in a write-behind fashion. The process of synchronizing the memory state with the database, usually only at the end of a unit of work, is called flushing. In our code, the unit of work ends with a commit (or rollback) of the database transaction - as defined by the thread configuration option for the CurrentSessionContext class.
You might of course load person and event in different units of work. Or you modify an object outside of a Session, when it is not in persistent state (if it was persistent before, we call this state detached). You can even modify a collection when it is detached:
private void addPersonToEvent(Long personId, Long eventId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person aPerson = (Person) session
.createQuery("select p from Person p left join fetch p.events where p.id = :pid")
.setParameter("pid", personId)
.uniqueResult(); // Eager fetch the collection so we can use it detached
Event anEvent = (Event) session.load(Event.class, eventId);
session.getTransaction().commit();
// End of first unit of work
aPerson.getEvents().add(anEvent); // aPerson (and its collection) is detached
// Begin second unit of work
Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
session2.beginTransaction();
session2.update(aPerson); // Reattachment of aPerson
session2.getTransaction().commit();
}
The call to update makes a detached object persistent again, you could say it binds it to a new unit of work, so any modifications you made to it while detached can be saved to the database. This includes any modifications (additions/deletions) you made to a collection of that entity object.
laut dieser beschreibung bräuchte ich doch kein session.update…
Aber ich hab es mal mit versucht keine änderung
private void addEventsToPerson(Long personId, Long EventsId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
TestEvent anEvents = (TestEvent) session.load(TestEvent.class, EventsId);
anEvents.getPersonen().add(aPerson);
session.update(anEvents);
session.getTransaction().commit();
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
TestEvent Events = (TestEvent) session.load(TestEvent.class, EventsId);
System.out.println(Events.getPersonen().size());
}
Kommt immer noch 0 raus…
-
wie kann ich mir anschauen wie er die tabellen genannt hat??? um die db zu starten starte ich einfach das jar file wo man bei hibernate runterladen kann zum testen…
http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial-firstapp.html
-
Was genau muss man in dem mappedBy angeben
@ManyToMany(mappedBy = "events")
public List<Person> getPersonen() {
return personen;
}
public void setPersonen(List<Person> personen) {
this.personen = personen;
}
wenn ich hier was anderes als “events” eingebe bekomm ich ein fehler… was muss man in dem mapped by angeben???
- ich glaub meine ManytoMany verbidnung hat eine macke