#Transient - Why data is getting saved in Database? - spring-boot

I have an entity variable annotated with #Transient like below. Which means it should not be stored in the Database.
#Transient
private String passwordConfirm;
But when I go to H2-Console, I can see the data is saved there.
Why so? and How can I avoid it?

You're probably using #org.springframework.data.annotation.Transient.
Change it to the right import: #javax.persistence.Transient
This will do the job.
#javax.persistence.Transient is used by the persistence provider (eg.: Hibernate). The persistence provider looks for JPA spec annotations. #org.springframework.data.annotation.Transient is not a part of the JPA spec, so, the persistence provider ignores it.
The #org.springframework.data.annotation.Transient is intended to be used when Spring Data is the actual ORM. Some examples are Spring Data Elasticsearch and Spring Data MongoDB. These Spring Data implementations use the #org.springframework.data.annotation.Transient just like Hibernate uses the #javax.persistence.Transient - not mapping the marked field into the database.

Related

Adding new DB support in spring data

Currently spring data has multiple db support (mysql, cassandra, mongo.. very big list), however i want to add my custom repository from the scratch like adding custom db support in spring data. I don't want to extend any existing repositories, instead I want to create a parallel repository strutcutre for my custom datasource. Looking at current implementation it looks like tedious task. It would be a great if someone could help me with minimal requirement to do this.
You could create a repository annotated bean where you would inject EntityManager or the proper bean that is acting like that, depending on database type that you are using.
#Repository
public class MyCustomRepositoryImpl implements MyCustomRepository {
#Autowired
private EntityManager entityManager;
//the methods that you are going to create.
}
For more details see:
https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html
Chapter: 1.3 Custom implementations for Spring Data repositories

Javers InstantiationError in spring environment

When trying to instantiate Javers using:
JaversBuilder.javers().build()
I keep getting the following runtime exception:
java.lang.InstantiationError: org.picocontainer.monitors.AbstractComponentMonitor
I am using javers-core 2.9.1 in a spring environment, however, I do not want to use any of the repository functionality, I just want to be use the object diff. In the getting started documentation it says you can simply use: Javers javers = JaversBuilder.javers().build(); and then compare to objects.
In a spring environment is there more that needs to be done? I am using JPA Entities and MappedSuperclasses (my object extends multiple levels of MappedSuperClasses of which the final one has a JPA #ID annotation.
Thanks,
Chris.

Spring Data REST MongoDB: Joda DateTime representation in REST

My Spring Data MongoDB entity has a property of Joda DateTime type:
#JsonProperty("myDate")
private DateTime myDate;
With Spring Boot 1.3.6, Spring Data 1.8.4, Spring Data REST 2.4.4 this property gets rendered as
"myDate": "2016-09-25T15:58:37.486Z"
in the REST representation of my Spring Data entity.
After I have updated the project dependencies to Spring Boot 1.4.1, Spring Data MongoDB 1.9.3, Spring Data REST 2.5.3 I suddenly get my date field represented as
"myDate": {
"content": "2016-09-25T15:58:37.486Z"
},
It looks like Joda's DateTime started to get treated as data entity again:
https://jira.spring.io/browse/DATAMONGO-624
Now I'm wondering how do I get back to my Spring Boot 1.3.6 DateTime representation in REST without downgrading to that Spring Boot version.
Edit:
Adding Jackson2 #JsonUnwrapped annotation to the property helps to get representation back:
#JsonProperty("myDate")
#JsonUnwrapped
private DateTime myDate;
This is a sub-optimal solution for me though, since my properties get auto-generated from JSON schema and I have limited control over generated annotations.
Edit 2:
The Javadoc for the CustomConversions class states that
These types will be considered simple ones (which means they neither
need deeper inspection nor nested conversion. Thus the
CustomConversions also act as factory for SimpleTypeHolder
Which is in fact not true for Spring Data REST [any more], from what I can tell debugging my code.
The types that are added via default CustomConversions are not treated as simple ones when rendering JSON and properties of those types get serialised as embedded entity objects.
Here is the place in the Spring Data REST PersistentEntityJackson2Module that calls to Spring Data MongoDB PersistentEntity implementation to check whether the property type is simple or not. And since the simpleTypeHolder in the Spring Data Commons AbstractMappingContext does not contain the types for which Spring Boot auto-configuration adds Joda DateTime converters, the DateTime field is treated as complex object.
Edit 3:
Tracking this issue in JIRA:
https://jira.spring.io/browse/DATAREST-907

spring boot could show sql even use JdbcTemplate directly

spring boot project, used JdbcTemplate, and want to show sql which is executed, the configuration is as below
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
but nothing output, it seems above configuration only support spring data jpa, So I'd like to know does exist some manner could show sql even used JdbcTemplate directly?
There is a reason why the property is named spring.jpa something: it is to indicate you that it relates to JPA in some form.
If you're using JdbcTemplate, you're not using JPA hence that property can't have any effect. You can enable logging for the org.springframework.jdbc.core.JdbcTemplate class
logging.level.org.springframework.jdbc.core.JdbcTemplate=debug

In-memory structure in Spring

I'm a Spring novice user.
I have a database table which is static in nature and contains only a few records.I want a Map-like structure(id - name) that holds two columns of this table. This Map must be loaded/initialized when the web application is started and must be applicable throughout the application's context, independent of the users sessions and must be read-only. This way, I can save a lot of DB queries as the different operations will simply read from this Map.
While I'm aware of ServletContextListener etc. of Java EE, I don't know how to achieve the same in Spring. Is a Spring Service bean the right place/way to initialize and store such a Map?
Please guide me about the same.
You can create a regular spring bean exposing a method which loads the data you require from the database and stores it in your map. Annotate this method with #PostConstruct and spring will ensure that it is called when your application context starts, hence loading your map.
You could use springs JdbcTemplate to load your data within this method
See Spring PostConstruct doco for information on the #PostConstruct annotation
See JdbcTemplate doco for information on JdbcTemplate
You can configure lists, sets and maps in a Spring XML configuration. See here for more examples.

Resources