Spring Boot app testing with database vendor specific JPA annotation - oracle

I got an app with an existing database that has Oracle only field type (e.g. "binary-float") mapped on the entity class with #ColumnDefinition annotation.
Things runs fine when running the app normally by launching the Application class.
However I can't seem to find a way to write junit tests easily. In another Spring Boot app, I have been using different profile to define a normal datasource that points to Oracle and a junit test datasource that points to the h2 in-memory db. I stayed mostly within JPAQL and common sql standard when using direct sql. Problem is, this scheme doesn't work if the JPA mapping annotation is database specific.
Any suggestions?

Related

Spring Boot Rest application: Testing JDBC DAO layer

How can I test my DAO layer in Spring Boot Application if my application only selects information from database and doesn't write anything?
Even more, my application selects data from view.
The common approach is to write some testing data by method with annotation #BeforeEach and to delete them by method with annotation #AfterEach.
But because my application performs query to view, I can't insert any data in database.
Is there any opportunity to test my DAO layer?
You have a few options:
Use an embedded H2 database then seed it with a data.sql, which you can dump from your test database.
Use DBUnit and define your data in an xml file.
For you I think data.sql is the way to go. Just add a data.sql to your test/resources file and it will be picked up by JPA.

Disable hibernate hbm2ddl

I have a spring boot project that uses flyway to insert the database structure and it works perfectly except when testing. When I run my tests I can see that the flyway sql files are executed so that works however for some reason hibernate drops and creates the database structure. This only happens when in testing so it seams as if Spring is telling Hibernate to drop-create the schema when testing.
How can I completly disable the hbm2dll?
I can understand that this is the default behaviour however it this the right thing to do? Spring should never assume that someone wants hbm2dll eneabled when testing, that should be indicated by the user not spring.

How To Use Different Strategy Which Dependent To Activate Profile To Generate The Id In Spring Boot Application

I have A Question.
I have a Spring Boot Application. It has Tree Application Profiles, Main (application.yml), Development (application-h2.yml) And Production (application-oracle.yml) File/Profile. I Have JPA Entities In My Project. How Can I Say, If I Want To Work With/On Oracle Use Please This GeneradValue See The Screenshot. And When I Will Work With/On H2 Database Use Please This GeneratedValue See The Screenshot.
enter image description here
Couldn't the #GenericGenerator impl you provide act as a delegate based on a system property? Let's say you read the profile VM arg in the implementation and routes / delegates to h2 or Oracle.
You would keep the REPORT_ID_GEN generated value annotation and the strategy implementation would take care of deciding if using H2 or Oracle instead of including 2 #GeneratedValue annotations.

Spring JDBCTemplate and Hibernate

I have a Spring, Spring Data, JPA/Hibernate application.
The legacy part of the application uses JdbcTemplate the new stuff uses spring-data/hibernate and everything is wrapped in a transaction.
Problem is when I modify an entity via hibernate and the legacy part of the system attempts to query something that's been modified I don't get the updated values with out having to explicitly "flush" the entity manager each time.
Is it possible execute the JdbcTemplate queries against hibernate's first-level cache?
What about trying this?
Edit: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/jpa/JpaTransactionManager.html
This transaction manager also supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services which access JPA and services which use plain JDBC (without being aware of JPA)! Application code needs to stick to the same simple Connection lookup pattern as with DataSourceTransactionManager (i.e. DataSourceUtils.getConnection(javax.sql.DataSource) or going through a TransactionAwareDataSourceProxy). Note that this requires a vendor-specific JpaDialect to be configured.

Is it possible to change properties of a bean (defined for a service) and reload it when the application is running?

I migrated a simple CRUD application developed in Java using OSGi to Grails using Spring. I converted all the REST resources to controllers and HTML pages to GSP views, keeping the rest of the Java code as such.
I have a DBService service, which helps connect to the DB and run queries on it, and a ProcessorService, which uses DBService to perform business operations.
I created beans for these services as follows:
beans = {
dbServiceBean(DBService, "test_db")
processorServiceBean(ProcessorService,ref("dbServiceBean"))
}
Everything is working fine with the above config.
Now, I want the application to be able to process multiple DBs (multi-tenant). I won’t know the name of the DB beforehand, however, so I can’t have a list of dbServiceBeans predefined.
Is it possible to rebuild/reload a bean with dynamically obtained values and reload the dependent beans as well when the application is running?
Grails already have the option to use multiple datasources.
You can change your DBService to get a connection from the datasources configured. If you just change it to a Groovy class and put it in grails-app/service you will get transactions and dependency injection by attribute name for free.

Resources