Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.
To better imagine the situation, I have a Jtable - which saves some records. When I want to edit a record - I have a button for this - I click on it, edit the cell. I suppose there is something wrong with the query not too specific? The previous version of the program did not allow to point to a given record with the mouse, but I entered a given record for editing in the text field and the query worked - there I pointed to a specific id for editing, now it points only to a given cell.
And I am wondering what is the problem, after all, the cell is edited but not saved? How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Learn more. Update a record value in java database Ask Question. Asked 4 years, 10 months ago. Active 3 years, 9 months ago. Viewed 7k times. Mark Rotteveel Mark Rotteveel please help — mcquelle. This is what you get when you copy an answer to a specific question without understanding what the code does. Also note that the way you are performing the update is not safe, it is open to SQL injection.
Use a prepared statement, just like I did for the insert in my answer that you copied. Am not sure if i follow, can you copy and paste the code above and add your suggestion. Add a comment. Active Oldest Votes.
Parfait Parfait You need to move the call into the if block. StaceyGirl 7, 12 12 gold badges 35 35 silver badges 64 64 bronze badges. Farah Smadi Farah Smadi 1. It will always post a new entity. DTechNet I was experiencing a similar problem to you, DtechNet, and it turned out my problem was that I had the wrong primary key type specified in my Spring Data repository interface. Does that help? I know this is almost a year later. Hope it helps someone else.
Show 7 more comments. Modifying Query "update User u set u. Make sure you run it in transaction — hussachai. Thanks am using spring data beans. So it will automatically takes care of my update. Thanks anyways! Anytime : The save method does work if you want to save the entity It will delegate the call to either em. Anyway, the custom query is useful when you want to update just some fields in database.
To update an entity by querying then saving is not efficient these are not the only two choices. There is a way to specify id and get the row object without querying it. Show 10 more comments. Kalifornium Kalifornium 7 7 silver badges 10 10 bronze badges. You can use userRepository. I just want to add supplement about what behind the save method.
Eugene Eugene 9, 4 4 gold badges 39 39 silver badges 60 60 bronze badges. Just call the save and enjoy the life :. Artem Arkhipov 5, 5 5 gold badges 29 29 silver badges 51 51 bronze badges. Amir Mhp Amir Mhp 99 1 1 silver badge 2 2 bronze badges. Up in this way if i sent different Id will save it , how can i avoid save new record. AbdAbughazaleh check whether incoming Id exists in your repository or not. Using save we can avoid adding new record in the targeted table update operation if that table has some unique column say Name.
Now the passed entity to be updated should have same Name value as table otherwise it will create a new record. AbdAbughazaleh What about OneToMany deleting the child entities that are not available in incoming request? I'm stuck since 15 days. Can you please provide your precious inputs? Ram Isn't Id field sufficient? That will be the unique column right? ParagKadam, make sure your entity has ID on it before you saving it. Adam Adam 2, 27 27 silver badges 23 23 bronze badges. Use Transaction above method for several db request.
An in this case no need in userRepository. BinLee BinLee 29 3 3 bronze badges. This statement needs a bit change. Andreas Gelever Andreas Gelever 1, 2 2 gold badges 14 14 silver badges 24 24 bronze badges. Golam Kibria Golam Kibria 1 5 5 bronze badges. Tarek Sahalia Tarek Sahalia 33 4 4 bronze badges. Would be much helpful if you add a simple example here. Sign up or log in Sign up using Google.
Sign up using Facebook.
0コメント