• Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating different subset of columns in multiple scenarios using JPA  RSS feed

 
Debajyoti Kundu
Ranch Hand
Posts: 32
1
Java
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 32
1
Java
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!