Updating entity - dirty checking vs repository save method [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've seen that programmers use two different ways to update entity:
First method is to make service update method transactional and using hibernate dirty checking. This is simple update and User class doesn't contains any lazy collections.
#Transactional
public void updateUser(Long id, String name) {
User user = userRepository.find(id);
user.update(name);
}
Second way use method save from spring-data.
public void updateUser(Long id, String name) {
User user = userRepository.find(id);
user.update(name);
userRepository.save(user);
}
Which way should I use for that simple update?

If you modify an object that is known by the hibernate session, it will automatically be saved by hibernate when session is flushed.
I would advise against this because in your code you don't know if you are using hibernate or another ORM. Additionally, if your object is not in the session when you modify it, it will not be saved. So always call save explicitely to make sure your object is saved.

Related

Java transaction rollback [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
Some methods in the transaction successfully call the interface, but in the end the transaction fails, resulting in a rollback, resulting in inconsistent data between the interface side and the local place
For example:
"pickTicketList" is the transaction you want to ship,but one of the "pickTicket" has an exception, causing the transaction to roll back
#Override
#Transactional
public void shipBol(WmsMasterBOL masterBol) {
for (WmsPickTicket pickTicket : pickTicketList) {
wmsPickticketManager.ship(pickTicket);
}
}
But the first few "pickTickets" have already called the sendMessageByI10() method. How can I keep the data of the client calling the interface and the local data consistently?
#Transactional
#Override
public DtWms0010Res prdoDeliv(WmsPickTicket pt){
DtWms0010Res resp = this.sendMessageByI10(outLog.getId());
return resp;
}
What are the ways to solve this problem
I am writing over as my understanding based on questions.
This is related to transaction propagation like: you need to mention transaction on methods as per reuqiredment.
Propagation
#Transactional(propagation=Propagation.Required) like this.

How to dynamically add columns in Database? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I have a spring boot project with Mysql & Hibernate, all I need is to provide a privilege to the user to change or alter columns of the database dynamically(runtime), I haven't found any good approach for that XML approach is quite hectic and will require a lot changes to be made.
Example:
A table User with columns
"Username" & "Password"
And from UI client will add another column "email" so it should make changes in Database table.
What you are specifically asking here would need changes to your entities during runtime, and that is not possible. You can achieve a similar solution by adding one additional column customerAttributes to the User table and store that information as map:
#Convert(converter = HashMapConverter.class)
private Map<String, Object> customerAttributes;
There is a good tutorial for that: https://www.baeldung.com/hibernate-persist-json-object

Is it a good practice to have POJO settings class as a bean? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a simple POJO class with few fields. This POJO serves the role of settings and it is passed to one method which actually executes some logic. Fields from this POJO are taken from the app.properties and they will not change.
What is a better pratice?
Create this POJO every time we want to call the method which needs it.
Make it a singleton bean, create it once and then autowire it?
Spring Boot provides the #ConfigurationProperties mechanism specifically to automate this for you. In general, it's best to avoid direct dependencies on MyServiceProperties and to do the injection in an #Bean method, but the MyServiceProperties instance is available as a context bean.

Java model best practices SQL - JSON -VIEW [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
While working on a Java web application I was wondering if my model layer was written as it should be. For instance, let's say we have a table USER in our SQL database which consists of 15 columns. Now, when we SELECT all of the columns with SQL we map it to a Java class, serialize via JSON and send it via network to some View and show it on screen.
In a second scenario, we want to select only 2 columns on screen so we do SELECT c1,c2 FROM USER. Thats where my question comes in... am I supposed to map those columns to a same Java model class? Or should i create a new mapper and class to fit it? Both of the approaches seem to have drawbacks, separate class for each query is more work, but it makes sure you always know what data it contains, rather than checking for nulls or working with optionals, also it prevents you from mapping columns you actually don't need.
What is your opinion? Thanks a lot!
Technically you could reuse the same User class for full 15-attribute as well as partial 2-attribute entity. But that will come with a price. Every time you'll see an instance of User class in the code your will have to think if it's the full entity or the partial? Which fields may or may not be null? This will make it much harder to reason about code.

Where to use #NamedQueries [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am using #NamedQueries but not sure what is the best way to use it.
Use it above entity class
Use it above DAO class
Create One NamedQueriesFactory class which will have centralized named queries for all Entity
Any other better way.
i good way would be to put all of them in an xml file which is used a lot. this externalizes your queries and gives you the freedom of just sending over this xml file to some DBA for easier analysis later on.
2 (Dao) and 3 (factory class) are not real options, assuming that those are not also entities, because according documentation:
The NamedQueries annotation can be applied to an entity or mapped
superclass.
That leaves as with 1 (entity) and 4 (mapped superclass). I would locate queries by return type and/or main entity accessed in query. Because mapped superclass cannot be returned from the JPQL queries, answer would be 1 (entity).

Categories

Resources