spring boot Filter selected columns from from data table connected by a foreign key using data JPA - spring-boot

I Have two tables as
student (student_id(primary key), student_index(unique key), student_name, student_batch, student_department, student_age)
exam_result_sem_one (result_id, subject_one, subject_two, subject_three, subject_4, fk_student_id)
then I want to get all data from exam_result_sem_one by student_index with the values of student_index and student_name from student table.
I want to do this using spring data JPA instead of using native query. I hopefully waiting for an answer.

Related

How to use ORACLE sequence for spring boot jpa non-id proprety

Is it possible to use a DB sequence (oracle 19c) for some column that is not the identifier/is not part of a composite identifier?
I'm using hibernate as jpa provider,spring boot and I have a table that has some columns that are generated values (using a sequence), although they are not part of the identifier.
What I want is to use a sequence to create a new value for an entity, where the column for the sequence is NOT (part of) the primary key:

how to query data from a postgres table ex 'pg_stat_statements' in Spring data?

Hello I am using a spring boot application and my requirement is to query pg_stat_statements table for some metrics. I am confused as I cant define a repository because this is not an entity.
Try to create view with this table, add row_number() as Id of this view, then create entity based on this view and use it in HQL

Update or Insert into postgres table without any primary key

I have a table which needs to be ingested from Oracle source to Greenland target using ETL tool talend. The table is huge , hence we want to load the data on daily basis incrementally. The table doesn't have any primary or unique key.
Table has date column, I am able to get both inserted/updated records from last update date but to insert that data, we need a primary key.
Any solution on how to load the data without using a primary key?
You need to define your key in talend in the schema of the component that insert into your target table, like this :
And you can use this key to update your table, in the advanced settings of the same component, activate the check box use fields optins and select your key :
This is tested and worked fine against Oracle table that does not have primary key, and it should work for you.

Spring data jpa, how to write custom query for inserting multiple records in same table?

Following is my table schema
CREATE TABLE Animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
In mysql I can directly insert multiple records in one insert sql query. Like following.
INSERT INTO animals (name) VALUES('dog'),('cat'),('penguin'),('lax'),('whale'),('ostrich');
However, how can I achieve the same thing in spring data jpa.
Right now I am using CrudRepository's Iterable save(Iterable entities); and I am eneded up with 6 insert statements
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
How do I restrict to One insert query ? Any answer will be helpful regarding spring data jpa, hql or jpql .
Assuming that you are using hibernate you need to tweak a couple of settings. You need to enable batching by putting a value in the hibernate.jdbc.batch_size property. However that might not cut it as depending on the number of different statements you also might need to order them to be able to batch them. For this you need to set the hibernate.order_inserts and hibernate.order_updates to true.
If you are using versions or timestamps to limit concurrent modification you might also need to enable hibernate.jdbc.batch_versioned_data
So all in all you probably need to add something like this to your configuration or persistence.xml
properties.put("hibernate.jdbc.batch_size", "25");
properties.put("hibernate.order_inserts", "true");
properties.put("hibernate.order_updates", "true");
properties.put("hibernate.jdbc.batch_versioned_data", "true");
A blog post explaining a bit more can be found here.
However if you are using MySQL it might not even work (yet) as MySQL requires an additional setting on the datasource to enable the usage of the addBatch for JDBC. You need to set the rewriteBatchedStatements to true to enable this feature for MySQL. Depending on your datasource you can add this to the connection string or as a seperate property.

JPA update a table

I am trying to update a table using JPA if I find that record using the primary key.During which I have a restriction should not update the record with null values.If I have a table
Employee with columns emp_id,emp_name
emp_name has a value and the new record does not have the value for emp_name in that case the old name should be retained.Is it possible?
When you find() the original employee it will have the old values. Where does your new record come from? Say it is some detached employee, then simply only overwrite the attributes that are not null into the managed employee.
JPA merge() will always merge everything. In EclipseLink you can set a FetchGroup on an entity being merged, and only the fetched attributes will be merged.

Resources