Setting default values in Java Beans / Data transfer Objects - spring

We use Data Transfer Objects (DTO) in our code. When constructing them, it is inevitable that some of their fields will have null values as some of those fields are null in the database. We were told that all null values must be default to "NA".
I know we can implements this the "hard way" by putting logic in the get methods of the DTOs. Question is, is there a better way to do this? I tried using #Value annotation of Spring to set its default values but this does not work.
Can anyone help? Thank you.

if you dont have a front end, you have to do it at the get method. (add in if statement for null?)

Related

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

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

adding a constraint to spring entity set field value to one of possible predefined values

good day everyone,
i'm currently working on a spring application for school project, it's about a form generator. anyway i have a question entity which has a field called type, i want that field to be one of 3 different values (radio which is the default, checkbox, or input) but i didn't know how to do this with hibernate annotations or even with the spring constraints annotations so i was hoping someone here has an idea on how to do this.
thanks for your time everyone and hope you have a nice day.

How to get rid off dependency between Model Attributes and Table Column Names in Spring

I know how to use BeanPropertyRowMapper and RowMapper as well.
But if I use BeanPropertyRowMapper,
i.e.
BeanPropertyRowMapper<MyClass> rowMapper = new BeanPropertyRowMapper<MyClass>(MyClass.class);
MyClass attributes becomes tightly coupled with tables column names and if I use RowMapper still MyClass attributes becomes tightly coupled and I have to write few more lines of code populate MyClass attribute with first GET and then SET operation extra.
Is there any way we can get rid of this dependency. Even after Table Columns names are changed, MyClass attributes should be populated as before and I do not need to change my code.
The Dozer mapping tool sounds like a good fit for what you're trying to do, and it's pretty simple to use. You'd still have to change something when changing column names, but it should be pretty easy. Check this out: http://dozer.sourceforge.net/

Same query method and parameters with different return in Spring Data

I want to use projections in order to return less elements for the same queries.
Page<Network> findByIdIn(List<Long> ids);
Page<NetworkSimple> findByIdIn(List<Long> ids);
Since the queries are created using the name of the method, what options do I have to do the same query but with different name ?
I ran into this today, and the accepted answer is actually incorrect; you can change the method name without altering the behavior. According to the Spring Data documentation:
Any text between find (or other introducing keywords) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/First to limit query results.
Thus you can have a method named findByIdIn and another named findNetworkSimpleByIdIn and the two will return the same data (optionally converted to a different form depending on the defined return type).
Spring Data query via method is constructed by convention and you can't change the name and yet expecting a same behavior.
You can try to use #Query annotations which doesn't depend on the method name, or possibly implementing custom DAO using JPAQuery plus FactoryExpression which has the same effect as projections.

How to display entity framework object as model with validation?

I have a populated object from using the entity framework. Let's call it Order. The order has different properties such as Id, OrderDate, BillingAddress and so on. I need to let users update this data.
What's the best way to display this data in a form, while enforcing data annotations such as [Required]? I see MetadataType mentioned a lot, but I haven't seen how I can connect the dots with displaying the data as well.
One approach that I could take, but I'd like to avoid because of redundancy, is creating my own model object that has nearly identical properties. Then I would just need to basically just copy entity framework object A to new object B, where B has all my lovely data annotations. It just seems like there might be a better way.
Could anyone provide me with an example of a good way to accomplish this?
The "better way" is a big reason EF Code First is great. Otherwise, the only other way to do what you need is to do a mapping.

Resources