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

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:

Related

Spring JPA how to skip Foreign Key Constraint violation check

Is there a way to Skip Foreign Constraint check in Spring JPA.
In SQL ALTER TABLE TableName NOCHECK CONSTRAINT ForeignKeyName. How to achieve this programmatically using Spring JPA
I am working on a Sync tool using SpringBoot, Spring data JPA which copies data from one DB to Other and using Spring JPA, the tables in the schema have two many foreign key constraints and adding the mappings in all the entity classes is proving to be cumbersome. Any suggestion around skipping the constraint check and in general for the approach is appreciated.
I understand the consequence of data integrity, by removing the constraint check.
There is no special JPA way to do this, but you can simply use SQL.
Depending on if you want to do the change just for the sync process or always you'd put the statement either in the creation scripts of the database or you execute it using EntityManager.createNativeQueryenter

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

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.

Why is my Spring Boot entity ID generation failing?

I'm trying to auto-generate ID's for my entity, but it's not generating. Instead, it's starting from 1 when there already exists an entry with id "1" in my DB. Why is it not generating id "9" for my new entity?
Typically when creating a table with GenerationType.IDENTITY on postgres, Hibernate will setup the id column plus a database sequence to manage this id.
By convention the sequence name will be "tablename_id_seq". E.g., for the table ad_group_action there will be a corresponding sequence ad_group_action_id_seq. You can connect to the database to double-check the actual sequence name created.
The sequence just starts from 1 and increments each time a row is inserted by Hibernate.
But if there are pre-existing rows -- or if rows with existing IDs are inserted "manually" into the table -- those rows can conflict with the sequence.
One solution is to simply reset the sequence (from pgAdmin or another database client) to start at a higher number (say 100), using something like:
ALTER SEQUENCE ad_group_action_id_seq RESTART WITH 100;
Now Hibernate will not conflict with the existing rows (assuming their max id is < 100).
Alternatively, when inserting rows manually, omit the id column and let postgres automatically set them. This way the table and the sequence will always be in sync.

Re insert records in Oracle table with Auto generated identifiers using Hibernate

I have few tables in my database where the primary keys are auto generated using Hibernate seqhilo generator configuration. We need to archive these records and at a later point, should be able to restore them in case of a business scenario. My question is if I restore these tables with simple insert statements will that suffice or should I worry about the sequence generator? I would like to have the same ID and not a new generated one. To be clear these re-inserts will happen via direct SQL and not via Hibernate.

Hibernate `assigned` strategy returns 0 with sequence and trigger

I've Table uses Trigger and sequence to set its PK column.
The Hibernate mapping strategy for its Pk is assigned..
This yields in session.save(obj) returns object with id=0
How to make it returns the correct assigned PK value.
session.getIdentifier() doesn't work!
assigned means: Nobody generates the ID, the ID is set explicitely in the entity before persisting it.
What you want to do is impossible. Hibernate would have to insert an entity without knowing its ID, then the database would generate the ID, and Hibernate would have to reload the entity from the database to know its ID. But how would it reload the entity without knowing its ID?
The native generator does the same thing, and it works because the database provides a getLastGeneratedId() method which allows getting the IOD that the database has generated. But you can't do that with Oracle and a trigger.
Remove the trigger from the database, use the sequence generator, and everything will be fine.

Resources