• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conventions for complex business logic

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic