I'm experiencing a very weird problem using Hibernate 2.0 in a Session bean environment (JBoss and
eventually WebLogic). I'm using a DataSource enlisted with JTA.
I have some nested objects (say, Person with a list of Address
objects attached). If I populate all required fields (e.g.
not-null in the database), everything works fine. However,
if I omit a required field from a nested object (e.g. line1 from
an Address object) and then save the Person object, I get a NULL
field violation, just like I expect to. However, the Person object
still gets saved. This sort of makes sense, because as far as the
database is concerned, there's nothing wrong with the Person record. However, from an app server perspective, this seems like aberrant behaviour, and I would definitely like to avoid it!
I would like to roll back the whole transaction if anything
fails. If I make my session bean use BMT, I can do that, but only then. Is there any other way to get the behaviour I need?
Here's an example of some code in my session bean:
Code:
public Person save(Person p)
throws MyAppException
{
Session session = null;
try
{
session = getSessionFactory().openSession();
if(p.getId() == null)
session.save(p);
else
session.update(p);
session.flush();
}
catch(Exception e)
{
throw new MyAppException(e);
}
finally
{
try
{
session.close();
}
catch(Exception e)
{}
}
return p;
}