Accessing Transient field of Entity through Service - spring-boot

Currently, I am learning Spring Boot. I got trouble with accessing the #Transient variable of Entity class from my #Service class.
Actually, I want to calculate the popularity of the book in my Service and want to assign it to a specific book.
#Transient
private Double popularity;
Any tips on solving this problem.
Let me know if you need more information.
Thank you in advance!

Transient properties are properties that do not have an appropriate column in the database table. But of course, they should have getters and setters, as any other property.
So after you calculate the popularity, just simply use the setter (setPopularity) as for any other class property.

Related

Bit confused with #JsonAutoDetect, ObjectMapper()

With reference to my previous linked in question I'm bit confused with the usability of #JsonAutoDetect.
I solved the problem by adding #Getter to FieldValues class and removed the #JsonAutoDetect.
So now it let me thinking, what would be the scenario where #JsonAutoDetect can be used, as I can achieve the same result without having it. What is the purpose of having #JsonAutoDetact annotation over having getter methods. Am I missing something.
Not able to write any comment for previous question so created a new one.
Here is an article that I think can help you. The url is https://www.baeldung.com/jackson-jsonmappingexception .
At my point, if you use jackson-databind jar, spring underlying use ObjectMapper to serialize JavaBean. If neither javaBean's field nor getter method is public, spring could not serialize JavaBean automaticlly. Annotation #JsonAutoDetect is used to custom your javaBean, by which way you can set field limits to any level (e.g. protected public private... so that you can serialize the javaBean successfully).
If I don't understand wrong, the #Getter is from lombok that automaticlly help you generate public getter method.

Spring Boot Spring Data - #ID

By default spring data does not expose #Id field in response. But my return object contains #ID value.
I have looked up online most questions are how to expose the ID field.
I can create new POJO, iterate through return objects and set only the values I need. But I'm wondering why the default behavior is not working.
If the question is how to manage entity's property and make them appear or hide in the response, then you better have to use Jackson's framework annotations. #JsonProperty("responseName") will forse a property to appear in the response with a given name, #JsonIgnore will make a property do not appear in the response.
Hope it'll help.

How to make flexible database for Spring MVC project

I want to make flexible database design for a Spring-mvc application, I mean that I want for example let user delete or add some attributes on the application withou disturbing it.
For example:
User want to add "adress mail" to his costumer or he want to delete "postal code" because he don't need this information ...etc
Something like that.
Is any body has an idea how to make that?
Use schemaless database, for example MongoDB. With Spring Data MongoDB you can persist maps that will give you flexibility you need:
#Document
public class Customer{
#Id
private ObjectId id;
private Map<String, String> properties;
...
}
Even though it is doable, I am not convinced if your design is valid. You can persist custom attributes in DB, but showing them in GUI at some point, when you need to customize look and feel for particular field it can become quite frustrating issue.

Update field annotated with #Value in runtime

Let's imagine we have such a component in Spring:
#Component
public class MyComponent {
#Value("${someProperty}")
private String text;
}
If we define the property placeholder:
<context:property-placeholder location="classpath:myProps.properties"/>
And myPropos.properties contains the value for someProperty the value will be injected to the text field when the context is initialized. That's quite simple and easy.
But let's say that I have a service that enables user to change the value of the someProperty:
public void changeProp(String name, String newValue);
Is there a chance I can re-inject the newValue to text field. I mean it should be quite straight forward.. Basically it's nothing different than the after-initialization injection. I can not imagine that Spring does not have support for this? Can I fire some event or something?
I could do this on my own basically, but I wander is it maybe something there already? If not does anyone know what Spring class is in fact handling the injections at the first place? I could probably reuse the code there do perform this on my own if a solution does not exists.
I expect spring does not have a support for this, because the normal injection is done while creating the bean, but not will it is put in service.
Anyway: in this blog entry "Reloadable Application Properties with Spring 3.1, Java 7 and Google Guava", you can find the idea for an solution.
The key idea is to use a post processor to build a list of all fields with property fields. And if the properties are changed on can use this list to update the fields.

Spring Data Neo4j - Indexing and Inheritance

Lets say i have the following data model:
public class A {
#Indexed(indexType = IndexType.FULLTEXT, indexName = "property1")
String property1;
}
public class B extends A {
#Indexed(indexType = IndexType.FULLTEXT, indexName = "property2")
String property2;
}
Can i tell the Spring framework to index property1 of class B under a different index name?
If not, what would you do in such case? I mean, what would you do if you have few classes that all extends the same base class, but in the same time, all the properties that those classes inherit form the base class should be indexed. I can annotate those properties for indexing only in the base class, and it is very limiting. What can i do?
Thanks.
The level attribute in the index definition annotation can be set to Level.INSTANCE. For more help please refer spring-data-neo4j documentation here
Here is an excerpt from the doc :
If a field is declared in a superclass but different indexes for
subclasses are needed, the level attribute declares what will be used
as index. Level.CLASS uses the class where the field was declared and
Level.INSTANCE uses the class that is provided or of the actual entity
instance.
I don't think that's possible. Your property1 will always be indexed in index property1. Being able to specify multiple indexes on a single field would probably fix your issue, but it's currently not possible. A while ago, I've raised an issue for this, but it's not yet implemented.
If you really want a domain (entity) object approach, you could also opt for the domain entity approach. It's not related to Spring or Spring Data Neo4j, but it also does the trick. By manually handling your entities this way, you could also manage the indexes yourself, thus gaining all the flexibility you want.
Just a question, why would you want to specify a different index per subclass?

Resources