How do I call a postgresql trigger function from spring jpa - spring

I want to update my product_inventory table with a trigger. So I have a function and not a stored procedure. I want to call that function or trigger from my springboot app, maybe from the jpa repository. I don't know which one to call, the function(which returns a trigger) or trigger and how would I do that from the jpa repository. Any help?

Related

How to create a stored procedure, with Spring JPA but without using Entities

In a project that I am working with Spring and JPA, I need to call a stored procedure but it does not have any table stored on the application side, only an input parameter is sent and returns an output parameter to know if I finish the process.
In all examples with JPA it always relates to JPA entities and the BD Tables.
Thank you for your contributions.
Take JPA out the pricture then. Assuming your application has a Datasource configured then inject that to the relevant code and get a connection:
https://docs.oracle.com/javase/7/docs/api/javax/sql/DataSource.html#getConnection()
Then just use standard JDBC.
https://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html

Business logic before to save an entity in Spring JPA

I'm using Spring Boot 1.5.4, Spring Data REST, Spring JPA, Hibernate and I'm developing a Angular client consuming REST API.
Spring Data REST helps a lot and I'm trying to follow best practice, so a repository is like:
#Transactional
#PreAuthorize("isAuthenticated()")
public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {
}
and automagically I've all my save(), delete(), findXX() methods. That's great.
Now I'm wondering how if I need custom business logic to do before the entity is saved. let's say I need to do some kind of complex validation (involving queries on the db), and other backstage activities (maybe saving related entities, updating related objects, ect).
My goals are:
Ensure every time the entity is saved (either from a REST call or a JPA call) my business logic is called before the object is saved
Avoid to create a custom repository because a developer could call the standard repository breaking my rules
Find a way to do this in a simple way in order to keep the app easy to mantain
The #RepositoryEventHandler is not enough for me because I want ensure my business logic is always verified even when the call to the method come from internal classes.
Could you suggest me the best approach to reach my goals?
JPA has a bunch of entity listener.
#PrePersist Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation.
#PreRemove Executed before the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
#PostPersist Executed after the entity manager persist operation is actually executed or cascaded. This call is invoked after the database INSERT is executed.
#PostRemove Executed after the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
#PreUpdate Executed before the database UPDATE operation.
#PostUpdate Executed after the database UPDATE operation.
#PostLoad Executed after an entity has been loaded into the current persistence context or an entity has been refreshed.

Execute a method once every transaction begins inside methods annotated with spring - #Transactional

I want to make a database call before every method within my Spring application annotated with #Transactional started executing transaction.
There are few requirements for this:
This database call has to be part of the actual transaction that is about to begin.
The database connection used for the Transaction has to be same to be used for this database call and should setup some database data before 'real' transaction begins.
Is there something that Spring supports to achieve this goal ? One way I am thinking is to write own Aspect cloning more or less all Transactional code as is. Other way is to write Aspect. Not sure if there are more ways.

How to insert data to table on spring boot application start?

How can I insert data to a table on Spring Boot application start? My application is generated by JHipster. What I need to check is that if that particular data already exist in that table or not. If it doesn't I should add it.
If your application was generated by JHipster, then it should already include and be properly configured for Liquibase.
You have a few options:
You can use Liquibase's insert change to insert data.
You can put your data in a CSV file and use the loadData change to load it. Look for the loadData tag in 00000000000000_initial_schema.xml for examples.
You can use the sql change to run native SQL directly.
All three options can be combined with preconditions to make sure the data doesn't exist before trying to insert it. If you need the changeset to run every time you boot your application, you can use the runAlways="true" attribute (docs for that are on this page).
You can create a function in your service class which checks if the data already exists or not and if not then save that data.
You can implement an ApplicationRunner and use a repository to do whatever you need to do.
If the ApplicationRunner is a spring bean it is run on application startup.
For more sophisticated requirements I would try to rely on a tool like flyway that has good integration with spring boot

GORM (Hibernate) Interceptor to run certain SQL before Hibernate runs it in the db in a Grails application?

I have a grails application in which I am using GORM. This works great. However I have a requirement where, before any SQL is called in the database, it has to run a certain stored procedure. So, is there a way I can do something that will trigger my method that kicks off a stored procedure before Hibernate runs the SQL for select, insert, update, delete etc.
Your response will be greatly appreciated.
(P.S.- The reason I have to run certain stored procedure is to change Oracle Workspace)
There might be several options.
You could use a dataSource wrapper that extends org.springframework.jdbc.datasource.DelegatingDataSource and runs the statement whenever getConnection is called.
I guess it's ok to call the statement (stored proc) only once per transaction, so DelegatingDataSource is probably the most approriate solution.
You could also use http://www.grails.org/plugin/jdbc-pool plugin (wraps Tomcat JDBC Pool) and try to hook to the datasource pool implementation. Tomcat JDBC Pool supports interceptors.
It's also possible to modify the SQL sent by GORM/Hibernate using a Hibernate interceptor, if that helps: Is it possible to map a table name for a domain object dynamically in grails? .
It's not clear whether you want to run this stored proc once before any SQL is executed, or before every SQL statement is executed. Here's a suggestion for both cases:
Once Only
Call the method that invokes the stored proc in Bootrap.init()
Before Every
Call the method from the before* GORM event handlers

Resources