Spring JPA data not inserting into the order in which it posted - spring

I don't know wheather anyone had this type of issue before or not! So but basically when i do save object it insert random order but not actual order in which i am sending it from POST api via UI. Below is my json object which i am sending to spring jpa for insertion,
{"expense":"wwww","amounts":[{"amount":"23","version":0},{"amount":"12","version":0},{"amount":"27","version":0},{"amount":"22","version":0},{"amount":"22","version":0},{"amount":"1111","version":0}],"version":0}
and this amounts are the Set<expense> amounts into my parent object as #manytomany relationship. And data insert into the random order but it should have insert the order which it sent from, correct me if i am wrong anywhere. In database it saves like 22,12,22,1111,,23,27 which is random.

In Java the Set interface doesn't guarantee any order. You can use a SortedSet for your mapping, or use a List. Both can be ordered and will preserve the one received from some JSON.
Some documentation :
Set Javadoc
SortedSet Javadoc

Related

Save Related Documents In Mongo Reactive But Not In The Same Collection

I would like to know, how to save related documents in reactive mongo ?. Because I find a code that attempts to do the magic... But when it should save the related document in another collection, it serializes inside the "father" of the relationship let say... I know that in spring data reactive mongo, #DbRef doesnt have support... How could I save the data in a way that, if I query the collection, I see that the attributesare the name of the collection and the generated id instead all of the object attributes ?.
If the pic above is seen, you will see that the attribute "user" is saved as a nested document but not in the corresponding collection. Do I need to hook in another event ?.
I had put a listener onbeforeconvert to scan every time a save operation is to be applied and save the object... How to proceed ?... think I should verify if it has a related doc from another collection and if its nonnull... If the object doesnt have any attr l
Alike, then save it... if not continue the scanning... dunno

Spring data with R2DBC(Mysql) Example for one to many association

Can I know how to do association(One to Many) in Spring data JDBC with R2DBC(Mysql).Please provide small code example or git link if possible.
For Example, I have one employee table and address table is child of employee.
One employee can have multiple addresses.
If I want to retrieve employee, I want address as well to corresponding employee as JSON.
Note: Software stack is Spring Functional Reactive, R2DBC with Mysql
Spring Data R2DBC currently does not support aggregates.
This means every entity gets mapped to just one table and can't have object references to other entities.
Therefore the correct way to model your example is to have no object reference between Empolyee and Address but have an employeeId in the Address and use that to select the desired addresses.

Save class field in particular order in Spring couchbase

I am using couchbase as Spring-data in Spring boot application,
I am saving a class let say Employee
which contain fields empId, empName, empDesc etc.
I wanted to save this object in couchbase in particular order.
Lets say I wanted to save this json in couchbase in the order
{
"empName": "hello",
"empDesc": "helloDesc",
"empId": "hello11"
}
How can I can acheive this?
Any help is appreciated.
I think you miss the point. Order of keys is irrelevant in a JSON object, be it in Couchbase or not.
An object is an unordered set of name/value pairs.
Cf http://json.org/
If the order is so important to you, you should use a JSON Array.
Don't use an API or framework in a devious way, it usually ends bad.

Save multiple row data and some other data into two tables simultaneously in spring hibernate

I want to save data of a form into two tables simultaneously. The form contains multiple identical rows with some fields like
item, unit, unit_price, total.
1.) In the table1 I would like to save some data like id(auto generated, primary key), date, creator.
2.) In table2 I would like to save the id(auto generated, primary key), fid(id of the table1, foreign key), item, unit, unit price, total. Remember, in table2 multiple rows will be save from the form data.
I can't figure it out. Please help me with a valid step by step example, and please don't use static or pre-defined data, take the data from form only.
Is your form an HTML form on a web page, and are you writing a Spring MVC application? Have you setup Spring Security, or some other authentication mechanism such that you know who the user is? If so, here is one approach:
Define a plain old Java object (POJO) with fields id, unit, unit-price, total, creator, currentDate; perhaps you would name it Item.
Define another plain old Java object perhaps named "ItemHistory", with its own ID and a reference to an Item.
In the first POJO ("Item") include a reference to a list of ItemHistorys.
Annotate both classes with Hibernate/JPA annotations, such that the Item has a OneToMany relationship to ItemHistory.
Define a Spring MVC controller with a method annotated as a POST method, with an Item as one parameter, and an Authentication as another parameter (supposing you have configured Spring Security, the Authentication will automatically be populated with the identity of the currently-logged-in user. In this method, set the currentDate and author of the Item to the current date and the principal from the Authentication parameter. If it is a new Item (no id yet), add the first ItemHistory as a copy of the Item itself. If it is an existing item, add another ItemHistory if needed.
Define a Spring bean, perhaps named ItemService, with a method annotated as transactional, that takes an Item and saves it. Call this bean from the REST controller. Such a Spring bean is known as a DAO (data access object).
There are other strategies, but if you already have Spring MVC, Spring Security, and Hibernate/JPA configured to your needs, this one is pretty straight-forward.

how can i update an object/entity that is not completely filled out?

I have an entity with several fields, but on one view i want to only edit one of the fields. for example... I have a user entity, user has, id, name, address, username, pwd, and so on. on one of the views i want to be able to change the pwd(and only the pwd). so the view only knows of the id and sends the pwd. I want to update my entity without loading the rest of the fields(there are many many more) and changing the one pwd field and then saving them ALL back to the database. has anyone tried this. or know where i can look. all help is greatly appreciated.
Thx in advance.
PS
i should have given more detail. im using hibernate, roo is creating my entities. I agree that each view should have its own entity, problem is, im only building controllers, everything was done before. we were finders from the service layer, but we wanted to use some other finders, they seemed to not be accessible through the service layer, the decision was made to blow away the service layer and just interact with the entities directly (through the finders), the UserService.update(user) is no longer an option. i have recently found a User.persist() and a User.merge(), does the merge update all the fields on the object or only the ones that are not null, or if i want one to now be null how would it know the difference?
Which technologies except Spring are you using?
First of all have separate DTOs for every view, stripped only to what's needed. One DTO for id+password, another for address data, etc. Remember that DTOs can inherit from each other, so you can avoid duplication. And never pass business/ORM entities directly to view. It is too risky, leaks in some frameworks might allow users to modify fields which you haven't intended.
After the DTO comes back from the view (most web frameworks work like this) simply load the whole entity and fill only the fields that are present in the DTO.
But it seems like it's the persistence that is troubling you. Assuming you are using Hibernate, you can take advantage of dynamic-update setting:
dynamic-update (optional - defaults to false): specifies that UPDATE SQL should be generated at runtime and can contain only those columns whose values have changed.
In this case you are still loading the whole entity into memory, but Hibernate will generate as small UPDATE as possible, including only modified (dirty) fields.
Another approach is to have separate entities for each use-case/view. So you'll have an entity with only id and password, entity with only address data, etc. All of them are mapped to the same table, but to different subset of columns. This easily becomes a mess and should be treated as a last resort.
See the hibernate reference here
For persist()
persist() makes a transient instance persistent. However, it does not guarantee that the
identifier value will be assigned to the persistent instance immediately, the assignment
might happen at flush time. persist() also guarantees that it will not execute an INSERT
statement if it is called outside of transaction boundaries. This is useful in long-running
conversations with an extended Session/persistence context.
For merge
if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance
if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance
the persistent instance is returned
the given instance does not become associated with the session, it remains detached
persist() and merge() has nothing to do with the fact that the columns are modified or not .Use dynamic-update as #Tomasz Nurkiewicz has suggested for saving only the modified columns .Use dynamic-insert for inserting not null columns .
Some JPA providers such as EclipseLink support fetch groups. So you can load a partial instance and update it.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/AttributeGroup

Resources