how to alter table using JPQL in spring Boot application - spring-data-jpa

I want to alter a table using JPQL but it is not possible to alter the
table how can I alter the table using JPQL,I want to remove studentId
form Teacher table .
Here is my query to alter the Teacher Table but it is not Working
/**Drop studentId column from the teacher table*/
#Transactional
#Modifying
#Query("update Teacher t drop t.studentId")
fun drpStudentID():Long

You can use below application property and from Entity(model) class you have to remove corresponding column of Entity.
spring.jpa.properties.hibernate.ddl-auto=update

Related

How to create an (FDW) foreign table and map it to a different schema?

This is how I defined the foreign table:
CREATE FOREIGN TABLE ftbl_employee (
id UUID,
name VARCHAR,
)
SERVER company_registry_dbserver
OPTIONS (schema_name 'company', table_name 'company_employee');
It created the foreign table successfully. However, when I list the foreign table, It has defaulted to public schema. See foreign_table_schema column:
> select * from information_schema.foreign_tables;
foreign_table_catalog
foreign_table_schema
foreign_table_name
foreign_server_catalog
foreign_server_name
sandbox
public
ftbl_employee
sandbox
company_registry_dbserver
I would like to map it into the company schema in our sandbox database server instead of the public schema.
The column information_schema.foreign_tables holds the schema where the foreign table is stored locally, not the schema of the table in the target database. So, there is no way you can create this foreign table in the schema company if it does not exist locally! You need to either locally run ..
CREATE SCHEMA company;
.. or live with the foreign table in the public schema. Keep in mind that a foreign table is nothing more than a "gateway" to a table that resides in a different database / server. If you wanna know more details on the foreign table, e.g. name or schema, check the view pg_foreign_table:
SELECT relname, ftoptions
FROM pg_foreign_table
JOIN pg_class ON ftrelid = oid;

updating table with Postgresql Foreign data wrapper

I created a foreign data wrapper table named t_user into mySchema.
IMPORT FOREIGN SCHEMA public LIMIT TO (t_user)
FROM SERVER myServer INTO mySchema;
The myServer side t_user added some column, but the foreign table didn't update accordingly.
I tried to delete the foreign table t_user, but it was used by my view and materialized view t_user, so the deletion failed.
Any ideas on how to update this table?
As you have seen, the foreign table definition does not change when the underlying table changes.
If all you did is add a column, you can use ALTER FOREIGN TABLE to add a corresponding column to the foreign table. That should work even if views depend on the foreign table.
For example, if the column is of type text, you can do:
ALTER FOREIGN TABLE t_user ADD COLUMN my_column text;

Insert into postgres table with select and autoincrement

I want to move data from a table T1 to another table T2. T1 has an autoincrement id, which is consequente of the JPA annotations #Id #GeneratedValue(strategy = GenerationType.SEQUENCE) with Spring Data implementation.
insert into T1(id, dataColumn)
(select NULL, dataToCopy
from T2)
This doesn't work, as it reports an error on the violation of the non-null constraint on the id field. How can I insert my data in this case?
The reason I cannot make the insert that way is that autoincrement is not set on the id column. The reason for not having autoincrement is that setting the a generator for the id leads to the need of making an additional select statements to get the next value from a database sequence.

eclipselink jpa joined table inheritance with mappedsuperclass

I have 2 tables. Employee and EmployeeDetails. Employee table has the basic details like Employee Id, Department and some audit fields like Created By, Created Timestamp. EmployeeDetails table has all the personal details about the employee and same audit fields (Created By, Created Timestamp) like Employee table. Now the audit fields and Version column are part of a MappedSuperclass ModelBaseFields.
I am using JOINED Inheritance in Employee which is my base class. It extends ModelBaseFields which is a MappedSuperclass. EmployeeDetails extends Employee.
Now the problem is, whenever I try to persist the data, Employee table INSERT query is formed properly however, EmployeeDetails INSERT query is missing audit fields (Created By, Created Timestamp) and version column.
I have tried using SINGLE TABLE inheritance with Secondary table. I am getting same issue in that scenario as well.
How do I add common columns in child table?

Can I reference an existing row in base table from inherited one?(postgres)

I'm trying postgres, version 9.2
Can I reference an existing row in base table from inherited one?
Example:
i have a "person" table and a "student" table which inherits from person.
As i understand, if i insert new student, automatically new person is added.
But i need to insert first the person data, and then insert a reference to it and student related data.
As i understand, if i insert new student, automatically new person is added
No. Although you can select students by querying the person table, only the student table will be populated after an insert to the student table.
The relationship you want is foreign key not inheritance. Create the person id column in the student table and make it dependent on the person id on the person table.