Hi all.
I am facing a behaviour with Hibernate that I don't fully understand.
Given this basic client code:
Code:
1. SessionFactory sf = config.buildSessionFactory();
2. Session sess = sf.openSession();
3. Transaction tx = null;
4. CD cd = new CD(); //CD is a basic POJO
// ... set some properties on the POJO
// CD and ARTIST tables have a m2m relationship (NO inverse)
// The List "artists" has been previously populated
5. cd.setArtists(artists)
6. tx = sess.beginTransaction();
7. sess.save(cd);
8. tx.commit();
9. sess.close();
10. long id = cd.getID();
11. sess = sf.openSession();
12. CD cdx = (CD)sess.get(CD.class,new Long(id));
13. System.err.println(cdx.getArtists().size()); //This call fails, because getArtists is null.
14. sess.close();
What happen here is that the ARTIST table is not populated, because the inverse attribute is set to false. So, this piece of code is ok.
But, if I remove lines 6 and 8 (the tx calls), line 13 doesn't raise an exception, even if the ARTIST table and the ARTIST_CD table are not populated.
Can someone of you guys explain me this behaviour?
Thanks,
Luciano