posted 14 years ago
I have a case where I need ot perform a number of operations in a businesslogic method.
I'm using Hibernate annotations alongside Spring, and the @Transactional annotation to demark the transactional boundaries.
In my database (JavaDB) I have a table which generates the PK for new records ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (Entities have the annotation @GeneratedValue(strategy = GenerationType.IDENTITY)).
Now the problem is that for some records my business logic needs to create an additional database record in the form of a join table record using the generated PK as the foraign key.
The optional piece of business logic throws a NullPointerException because the ID of the newly created Entity has not been generated I believe because that business logic is within the scope of the transaction, therefore before the new recrod has been committed to the database.
I would have hoped that transaction attributted such as 'propagation' and 'isolation' would help, but I have not found their documentation very clear, although if the Database is entirly responsible it really will not be generated at any point where my suplimentry business logic can make use of it.
So what I want to know is, what a better approach to solve this problem, I really want the optional business logic to be within the scope of the transaction to avoid an inconsistant database, but at present my only way around things is to commit each step of the business logic (this is actually done by auto-commit).
Any pointers would be greatfully recieved.