Saving entities fetched from a query with hibernate - java

I hava a basic Hibernate/JPA question. I want to find a best practice solution for saving entities. I have a List of Entities and many of them might be altered so I want to save them all at once.
I believe everything is pretty much standard. (Just example code for readability reasons)
Entity: Car
#Entity
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private id;
private String model;
// .... Setter
// .... Getter
}
Service Class: CarService
#Named
#Transactional
public class CarServiceImpl implements CarService {
#PersistenceContext
private EntityManager entityManager;
#Override
public List<Car> findAll() {
TypedQuery<Car> q = entityManager.createQuery(
"FROM Car", Car.class);
return q.getResultList();
}
#Override
public void saveEntity (Car car) {
/* What exactly am I doing here? */
}
}
Controller: CarEditController
#Named
public class CarEditController implements Serializable {
private static final long serialVersionUID = 1L;
#Inject
private CarService carService;
private List<Car> cars;
public List<Car> getCars () {
return carService.findAll();
}
public void setCars (List<Car> cars) {
this.cars = cars;
}
public void btn_newClick() {
Car newCar = new Car();
car setModel("BMW");
cars.add(newCar);
}
public void btn_saveClick() {
for (Car car : cars) {
carService.saveEntity(car);
}
}
}
I found quite a few ways of saving the entity. The obvious are entityManager.merge(car) for existing entities and entityManager.persist(car) for new ones. In theory thats easy but how do I know which entity is new?
The documentation suggests entityManager.flush(), which in theory updates all existing and inserts all new entities. It works but only if I fetch the entity with find().
The Question:
I want to fetch all entities in one step, add new ones and then save them all in one methode (btn_saveClick()). How is this task best accomplished?

Just check if the #Id is been set.
#Override
public void saveEntity (Car car) {
if (car.getId() == null) {
entityManager.persist(car);
} else {
entityManager.merge(car);
}
}
More common approach, however, is to offer separate create() and update() service methods.

I'm not familiar with JPA but in hibernate there is session.saveOrUpdate()
for(Car car : existingAndNewCars)
{
session.saveOrUpdate(car);
}
Update:
As i understand JPA, its merge is like session.merge which is totally different as it doesn't track changes to object supplied to it while persist/save/update/saveOrUpdate would track subsequent changes to car, leading to subtle differences
Update:
since you use the same entitymanager it should suffice to
#Override
public void saveEntity (Car car) {
if (car.getId() == null) {
entityManager.persist(car);
}
without the subtle difference of persist and merge

The flush operation will operate on all entities in the current Hibernate session - new entities are inserted and existing entities are updated if they have changed.
You need to ensure that all entities are attached to the session. You do this by using merge as you correctly say. After you have loaded all of the entities the session is closed when the transaction ends. Your objects are then in a detached state i.e. have been persisted but are no longer attached to a session.
I would amend your logic so that your carService#save takes a List. It can then call merge on each one (attaching them to the session). Then when your transaction ends Hibernate will flush all changes to the database at once.

Related

current/previous node relations in spring data neo4j (LinkedList)

I’m trying to create linked list of nodes with CURRENT/PREVIOUS relation similar to the picture below.
I'm not sure if my solution is the right way to handle this scenario, but to achieve that I created two nodes with a single method to populate new messages as below:
#Builder
#Data
#NoArgsConstructor
public class Person {
#Id
#GeneratedValue
private Long id;
private String name;
#Relationship(type = "LATEST")
private Message message;
void newMessage(Message newMessage) {
newMessage.setPrevious(message);
message = newMessage;
}
}
#Builder
#Data
#NoArgsConstructor
public class Message {
#Id
#GeneratedValue
private Long id;
private String text;
#Relationship(type = "PREVIOUS")
private Message previous;
}
I also created a sample code to test this solution:
#SpringBootApplication
public class NewsFeedApplication {
public static void main(String[] args) {
SpringApplication.run(NewsFeedApplication.class, args);
}
#Bean
CommandLineRunner init(PersonRepository personRepository) {
return args -> {
Person personToAdd1 = Person.builder().name("John").build();
personToAdd1.newMessage(Message.builder().text("first message").build());
personToAdd1.newMessage(Message.builder().text("second message").build());
personToAdd1.newMessage(Message.builder().text("third message").build());
personRepository.save(personToAdd1);
personToAdd1.newMessage(Message.builder().text("New message.").build());
personRepository.save(personToAdd1);
};
}
}
I feel like I'm close, but I don't know how to reset the previous CURRENT relation and my solution produces output as:
So the question is:
If my approach is okay, how could I remove previous CURRENT relation.
If this approach is wrong, how could I implement linked list with CURRENT/PREVIOUS relations for nodes correctly.
I found the missing puzzle, i.e. detaching the supplier relation. I don't know why I assumed in advance that this relation should be deleted automatically by the spring data repository "save" method.
Working solution:
public interface PersonRepository extends Neo4jRepository<Supplier, Long> {
#Query("MATCH (n:Person {name: $name})-[r:LATEST]->() DELETE r")
void detachLatestFromPerson(String name);
}
void newMessage(PersonRepository personRepository, Message newMessage) {
personRepository.detachLatestFromPerson(name);
newMessage.setPrevious(message);
message = newMessage;
}
PS. I still have doubts, as I'm not sure if that's a good approach to handle this scenario, so if you know a better solution, go ahead and post it, we can always swap the 'best answer' :)

Concurrent data access with Spring Data

I'm new to Spring Data/Hibernate, and I'm trying to get my head around how you're supposed to handle concurrent users accessing data.
Suppose I've got a very simple domain model, consisting of houses and people who live in those houses:
House:
#Entity
public class House {
#Id
#GeneratedValue
private Long id;
#OneToMany(cascade = CascadeType.ALL)
private Set<Person> persons;
public void addPerson(Person p) {
persons.add(p);
public Set<Person> getPersons() {
return persons;
}
}
Person:
#Entity
public class Person {
#Id
#GeneratedValue
private Long id;
}
Currently I'm loading a House from a HouseRepository, and using this object to get/add persons.
That all works fine for a single user, but how are you supposed to support concurrent users? That is, say I've got a web application that has 2 concurrent users, who both want to view and add/edit persons from the same house.
What's the standard/best practice?
edit: to clarify what I'd like to do:
User 1 gets houseA from repository
User 2 gets houseA from repository
User 1 adds personA to houseA
User 2 gets persons from houseA, which contains personA
edit: Problem with #Transactional -
#SpringBootApplication
public class ExampleApplication implements CommandLineRunner {
#Autowired
private HouseRepository houseRepository;
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
#Override
public void run(String... strings) throws Exception {
House house = new House();
Person person = new Person();
person.setName("Bob");
house.addPerson(person);
houseRepository.save(house);
printPeople(house.getId());
}
#Transactional
public void printPeople(Long id) {
House house = houseRepository.findOne(id);
for (Person person : house.getPersons()) {
System.out.println(person.getName());
}
}
}
Throws org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.House.persons, could not initialize proxy - no Session at the foreach loop in printPeople
JPA is typically expected to be used within transactions. Use Spring's #Transactional AOP to mark your transaction boundaries, and it will handle concurrent access. Apply #Transactional to the highest-level method where it is relevant, which is typically the MVC controller method for a Web application.

How to get old entity value in #HandleBeforeSave event to determine if a property is changed or not?

I'm trying to get the old entity in a #HandleBeforeSave event.
#Component
#RepositoryEventHandler(Customer.class)
public class CustomerEventHandler {
private CustomerRepository customerRepository;
#Autowired
public CustomerEventHandler(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
#HandleBeforeSave
public void handleBeforeSave(Customer customer) {
System.out.println("handleBeforeSave :: customer.id = " + customer.getId());
System.out.println("handleBeforeSave :: new customer.name = " + customer.getName());
Customer old = customerRepository.findOne(customer.getId());
System.out.println("handleBeforeSave :: new customer.name = " + customer.getName());
System.out.println("handleBeforeSave :: old customer.name = " + old.getName());
}
}
In the event I try to get the old entity using the findOne method but this return the new event. Probably because of Hibernate/Repository caching in the current session.
Is there a way to get the old entity?
I need this to determine if a given property is changed or not. In case the property is changes I need to perform some action.
If using Hibernate, you could simply detach the new version from the session and load the old version:
#RepositoryEventHandler
#Component
public class PersonEventHandler {
#PersistenceContext
private EntityManager entityManager;
#HandleBeforeSave
public void handlePersonSave(Person newPerson) {
entityManager.detach(newPerson);
Person currentPerson = personRepository.findOne(newPerson.getId());
if (!newPerson.getName().equals(currentPerson.getName)) {
//react on name change
}
}
}
Thanks Marcel Overdijk, for creating the ticket -> https://jira.spring.io/browse/DATAREST-373
I saw the other workarounds for this issue and want to contribute my workaround as well, cause I think it´s quite simple to implement.
First, set a transient flag in your domain model (e.g. Account):
#JsonIgnore
#Transient
private boolean passwordReset;
#JsonIgnore
public boolean isPasswordReset() {
return passwordReset;
}
#JsonProperty
public void setPasswordReset(boolean passwordReset) {
this.passwordReset = passwordReset;
}
Second, check the flag in your EventHandler:
#Component
#RepositoryEventHandler
public class AccountRepositoryEventHandler {
#Resource
private PasswordEncoder passwordEncoder;
#HandleBeforeSave
public void onResetPassword(Account account) {
if (account.isPasswordReset()) {
account.setPassword(encodePassword(account.getPassword()));
}
}
private String encodePassword(String plainPassword) {
return passwordEncoder.encode(plainPassword);
}
}
Note: For this solution you need to send an additionally resetPassword = true parameter!
For me, I´m sending a HTTP PATCH to my resource endpoint with the following request payload:
{
"passwordReset": true,
"password": "someNewSecurePassword"
}
You're currently using a spring-data abstraction over hibernate.
If the find returns the new values, spring-data has apparently already attached the object to the hibernate session.
I think you have three options:
Fetch the object in a separate session/transaction before the current season is flushed. This is awkward and requires very subtle configuration.
Fetch the previous version before spring attached the new object. This is quite doable. You could do it in the service layer before handing the object to the repository. You can, however not save an object too an hibernate session when another infect with the same type and id it's known to our. Use merge or evict in that case.
Use a lower level hibernate interceptor as described here. As you see the onFlushDirty has both values as parameters. Take note though, that hibernate normally does not query for previous state of you simply save an already persisted entity. In stead a simple update is issued in the db (no select). You can force the select by configuring select-before-update on your entity.
Create following and extend your entities with it:
#MappedSuperclass
public class OEntity<T> {
#Transient
T originalObj;
#Transient
public T getOriginalObj(){
return this.originalObj;
}
#PostLoad
public void onLoad(){
ObjectMapper mapper = new ObjectMapper();
try {
String serialized = mapper.writeValueAsString(this);
this.originalObj = (T) mapper.readValue(serialized, this.getClass());
} catch (Exception e) {
e.printStackTrace();
}
}
}
I had exactly this need and resolved adding a transient field to the entity to keep the old value, and modifying the setter method to store the previous value in the transient field.
Since json deserializing uses setter methods to map rest data to the entity, in the RepositoryEventHandler I will check the transient field to track changes.
#Column(name="STATUS")
private FundStatus status;
#JsonIgnore
private transient FundStatus oldStatus;
public FundStatus getStatus() {
return status;
}
public FundStatus getOldStatus() {
return this.oldStatus;
}
public void setStatus(FundStatus status) {
this.oldStatus = this.status;
this.status = status;
}
from application logs:
2017-11-23 10:17:56,715 CompartmentRepositoryEventHandler - beforeSave begin
CompartmentEntity [status=ACTIVE, oldStatus=CREATED]
Spring Data Rest can't and likely won't ever be able to do this due to where the events are fired from. If you're using Hibernate you can use Hibernate spi events and event listeners to do this, you can implement PreUpdateEventListener and then register your class with the EventListenerRegistry in the sessionFactory. I created a small spring library to handle all of the setup for you.
https://github.com/teastman/spring-data-hibernate-event
If you're using Spring Boot, the gist of it works like this, add the dependency:
<dependency>
<groupId>io.github.teastman</groupId>
<artifactId>spring-data-hibernate-event</artifactId>
<version>1.0.0</version>
</dependency>
Then add the annotation #HibernateEventListener to any method where the first parameter is the entity you want to listen to, and the second parameter is the Hibernate event that you want to listen for. I've also added the static util function getPropertyIndex to more easily get access to the specific property you want to check, but you can also just look at the raw Hibernate event.
#HibernateEventListener
public void onUpdate(MyEntity entity, PreUpdateEvent event) {
int index = getPropertyIndex(event, "name");
if (event.getOldState()[index] != event.getState()[index]) {
// The name changed.
}
}
Just another solution using model:
public class Customer {
#JsonIgnore
private String name;
#JsonIgnore
#Transient
private String newName;
public void setName(String name){
this.name = name;
}
#JsonProperty("name")
public void setNewName(String newName){
this.newName = newName;
}
#JsonProperty
public void getName(String name){
return name;
}
public void getNewName(String newName){
return newName;
}
}
Alternative to consider. Might be reasonable if you need some special handling for this use-case then treat it separately. Do not allow direct property writing on the object. Create a separate endpoint with a custom controller to rename customer.
Example request:
POST /customers/{id}/identity
{
"name": "New name"
}
I had the same problem, but I wanted the old entity available in the save(S entity) method of a REST repository implementation (Spring Data REST).
What I did was to load the old entity using a 'clean' entity manager from which I create my QueryDSL query:
#Override
#Transactional
public <S extends Entity> S save(S entity) {
EntityManager cleanEM = entityManager.getEntityManagerFactory().createEntityManager();
JPAQuery<AccessControl> query = new JPAQuery<AccessControl>(cleanEM);
//here do what I need with the query which can retrieve all old values
cleanEM.close();
return super.save(entity);
}
The following worked for me. Without starting a new thread the hibernate session will provide the already updated version. Starting another thread is a way to have a separate JPA session.
#PreUpdate
Thread.start {
if (entity instanceof MyEntity) {
entity.previous = myEntityCrudRepository.findById(entity?.id).get()
}
}.join()
Just let me know if anybody would like more context.
Don't know if you're still after an answer, and this is probably a bit 'hacky', but you could form a query with an EntityManager and fetch the object that way ...
#Autowired
EntityManager em;
#HandleBeforeSave
public void handleBeforeSave(Customer obj) {
Query q = em.createQuery("SELECT a FROM CustomerRepository a WHERE a.id=" + obj.getId());
Customer ret = q.getSingleResult();
// ret should contain the 'before' object...
}

State Pattern: States as Hibernate singleton entities

What I have:
I've implemnted a `State Pattern. Now I have a dozen of States in my app, all of them are Spring's beans, like:
#Service(value = "FinishedState")
#Scope("singleton")
public class FinishedState extends AbstractState {
private final String stateName = "Finished";
private final String badgeColor = "#459852";
//implementation of state API
}
What I want:
Be able to change stateName and badgeColor without recompiling my application. Or in other words I want to store my states in DB.
What a question:
Is it good idea to make all States as Singleton entities?
What diffculties may I face?
Means that I will manually set an id to each State , like id = this.getClass().getSimpleName(), and map all state-siblings into one table.
Edit (for #Adam Arold):
And also its worth to say that I need that other entities can reference that Singleton's states.
#Service(value = "FinishedState ")
#Scope("singleton")
public class FinishedState extends AbstractState {
private final String stateName = "Finished";
private final String badgeColor = "#459852";
#Override
public void someMethod1(Session session, String newState, MyEntity entity) {
entity.setStatus(newState);
writeHistory(entity);
}
#Override
protected boolean someMethod2(Session session, AbstractState newState, MyEntity entity)
{
return true;
}
}
The only time I make my states Singletons is when I am changing states frequently. This is useful when performance is essential -- in the case that I do not wish to call new every time I switch back and forth from states.
Suggestion
My suggestion to you is that you analyze:
How does your system operate in regards to the states?
Do you change states frequently?
Can you afford the space in memory of having a bunch of instances of
states alive during the execution of the system?
The only issue I see that you can come across is that while your system is running the Singleton states will be alive until the system finishes.
Personally
I would make them Singletons though
I ended with:
#MappedSuperclass
#Inheritance(strategy= InheritanceType.SINGLE_TABLE)
#Table(name = "state")
public class StateBase {
/**
* Entity API
*/
#Id
#Column(name = "id")
private String id = this.getClass().getSimpleName(); //prevent from creating two instances of state
#Column(name = "name")
private String name;
#Column(name = "badgeColor")
private String badgeColor;
//only Getters! also hashCode and Equals
//.....................
/**
* ======== State API ========
* Use TEMPLATE METHOD and hooks
*/
final public void changeState(Session session, State newState, StateEntity entity) { //StateEntity is interface that is implemented by all entities that have a state
if(newState.equals(this)) return;
if (canMove(session, newState, entity)) {
hook(session, newState, entity);
beforeTransition(session, newState, entity);
makeTransition(session, newState, entity);
afterTransition(session, newState, entity);
} else {
throw new TransitionDeniedException("Transition from " + getName() + " to " + newState.getName() + " is denied.");
}
}
//Defauls implementation for all (or almost all hooks)
protected void beforeTransition(Session session, State newState, StateEntity entity) {
entity.setState(newState);
}
protected void afterTransition(Session session, State newState, StateEntity entity) {
writeHistory(entity);
session.merge(entity);
}
//..........................
}
And than:
#Entity
public class SpecificState extends StateBase {
//Override hook
#Override
public void makeTransition(Session session, State newState, StateEntity entity) {
MyEntity myEntity = (MyEntity ) entity;
String commentText = "some comment";
String author = "[autogenerated]";
addProfileComment(session, myEntity, commentText, author);
}
}
Due to my implementation, all states are entites, but they have only private String field without Setters. So they have no muttable state. So it isn't realy matter if there can exist one or more entities of specific state (in memory). In DB I can store only one entity of specific state class at once.
Advantages
It's allow me to forget about troubles of state synchronization and another possible difficulties linked with replacement of Spring singleton beans with Hibernate entities.
It's also allows user to change name and badgeColor through admin tool without recompiling the whole application.
Disadvantages:
Not observed.

Merge failing to update Entity with byte[] member variable

I have an entity with a byte[] member variable annotated as a #Lob. I perform some calculations and then I do a merge() call. This is done in a stateless bean. For some reason it does not update the entity. No exceptions are thrown either. Help me www dot stackoverflow dot com, you're my only hope.
Here is basically what I have.
My entity:
#Entity
#Table(name="my_entity")
public class MyEntity implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="id")
private long id;
#Lob
#Column(name="data")
private byte[] data;
// Getters and Setters for both variables...
}
Then I have my stateless bean. I will make the skeleton of exactly what I have. I have a timer that is called every minute which acts on the data.
public #Stateless class MyBean implements IMyBeanRemote {
#PersistenceContext
private EntityManager em;
#Resource
javax.ejb.TimerService timerService;
#Timeout
public void doLogic(javax.ejb.Timer time) {
MyEntity e = getMyEntity(1L);
doMoreLogic(e);
}
private MyEntity getMyEntity(long id) {
return em.find(MyEntity.class, id);
}
private void doMoreLogic(MyEntity entity) {
entity.getData()[0] = 123;
em.merge(entity);
}
}
I think this basically mirrors what my code is doing. When I initially create MyEntity and persist() it, that works.
After typing all this, I decided to do a check to see if I modify that data anywhere else. Turns out I did and I was resetting the data as a debugging test. =( All solved!
Lack of sleep caused me to not see previous debugging code resetting the data.

Categories

Resources