Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conventions for complex business logic

 
Rick Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us your mapping between the two associated classes. Where is your mappedBy and cascade options.

Transaction propagation and isolation are important topics to understand, but not related to your problem at all.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic