spring data jdbc and sequence - mariadb

I use spring data jdbc and mariadb.
With spring data jpa for a sequence we can do
#Id
#GeneratedValue(generator="xxxx_id_seq")
#SequenceGenerator(name="xxxx_id_seq",sequenceName="xxxx_id_seq", allocationSize=1)
Long id;
What is the solution for spring data jdbc?

Related

Query method not supported - Redis datasource, Spring boot starter data redis (2.6.3) and CRUD repository

I am using redis as my data store and i am using spring boot starter data redis as the dependency and a crud repository for CRUD operation. When i do deleteById it is working. But when i do deleteByName (Name is not an id column) it is saying query method not supported. When the data source is redis and if we use spring boot starter data redis, it is possible only to deleteById column and not by other columns?
I had the same problem that after a lot of searching and also checking the following link:
Redis 2.1.2.RELEASE doc reference
I realized that since Redis is based on Key and Valve, the following solution should be used:
List<SampleClass> lst = cacheTokenRepository.findAllBySampleFields(... sampleFields);
lst.forEach(item->
SampleRepository.deleteById(item.getId())
);
In this solution we search the list of records that match our conditions and delete them all based on id

Flyway + Oracle issues with baseline

I'm trying to get flyway to work with a Spring Boot application and Oracle.
So far I have been successful in getting flyway working on several databases (postgreSQL, mysql, SQLServer) but on Oracle I keep getting in a chicken and egg problem.
If I run my app for the very first time, I get an error: Found non-empty schema "SPRING" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.
I'm connecting with a user spring to a specific namespace configured on Oracle. and I'd like to have the schema_version table on the same namespace
So if I try to set flyway.baseline-on-migrate=true it does create the migration table, but then, it does not create any of the tables, as it says there's already a version 1 installed on the schema identified by << Flyway Baseline >>
How can I get away from this chicken and egg issue? Seems to happen only with oracle, all other dbs, it worked straight on the first time.
So far I'm running it, the app fails, I then delete entries from the schema_version table and run it again.
Ideas?
You may need to specify the schema in Spring Boot e.g.:
flyway:
schemas: SPRING
Let's say, you have the following setup in Oracle XE:
CREATE TABLESPACE SPRING DATAFILE 'spring.dbf' SIZE 40M ONLINE;
CREATE USER SPRING IDENTIFIED BY SPRING DEFAULT TABLESPACE SPRING TEMPORARY TABLESPACE TEMP;
GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, UNLIMITED TABLESPACE TO SPRING;
You should now be able to populate the database using the following Boot properties:
spring:
datasource:
url: jdbc:oracle:thin:#localhost:1521:xe
username: SPRING
password: SPRING
flyway:
schemas: SPRING

How to return Auto Generated Id in Spring Boot with Hibernate

I am new to Spring framework and trying to implement a simple CRUD application in spring boot with MySQL as database. Everything is working fine.
I have the Auto Increment enabled on Id field in the database. I am using EntityManager.persist() method to save the data in database and it is working fine. Now I want to return the auto generatedId back to the client as response of POST method but EntityManager.persist()return type is void.
Can anyone help me that how I can return the Id back?
the id is guaranteed after flush operation or when the transaction is completed.
em.persist(employee)
em.flush();
long id = employee.getId();
for more details read
What's the advantage of persist() vs save() in Hibernate?

JdbcTemplate not updating

I am trying to issue a simple batchUpdate via JdbcTemplate on ORACLE database. I keep getting update return values as -2. I can complete the update via simple jdbc preparedStatement. But JdbcTemplate isn't able to do the same. What does the return value -2 from JdbcTemplate means anyway?

Mysql TimeStamp datatype

I am developing a asp.net web application with ado.net entity framework and MySQL as backend.
I have taken some columns in DB as timestamp datatype when I get date from DB these fields of timestamp datatype returns null. In application these fields are mapped as datetimeoffset.
See the accepted answer to this post
How to convert a Unix timestamp to DateTime and vice versa?
You need to adapt the timestamps into datetimeoffset objects in .net.

Resources