• 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

Updating different subset of columns in multiple scenarios using JPA

 
Ranch Hand
Posts: 33
1
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have found few work around of my query in internet but still posting here for different solution.

While updating an row through JPA (I'm using toplink), I need to pass a subset of column values and update only those columns. Other column values should not be forced set to null. In different scenarios I will provide values for different columns. Is there any was to do it (PS: without making any query to database to get the object and then updating it)?

Thanks
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use an update statement and set only the columns you want. You'll need JPA 2.1 to use the criteria API, but with older versions you can still build a JPQL query.
 
Debajyoti Kundu
Ranch Hand
Posts: 33
1
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
Thanks for the response. Please let me elaborate my problem and what I'm trying to achieve.

I'm taking a generic approach in insert and update operations. The data that i'm saving in db is coming from some JSON and I can retrieve the Entity class name from URL. I'm creating an object from JSON. In different scenarios object will be of different entity classes and I'm also able to cast them to the respective entity using reflection.

I could have used update query but the incoming data might contain some OneToMany relationships, so I've stick with merge() so that in single call the whole data can be saved. Due to generic approach my update logic cannot contain any code that sets the fields of a particular class. Is there any way to achieve this staying with my current approach.

Thanks
 
Men call me Jim. Women look past me to this 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