how to add columns to database table that are not contained in Entity - spring

If I want to have DB table with scheme
id|column1|rest of columns...
such as:
#Entity
#Table(name = "SampleEntity")
class SampleEntity(
#Id #GeneratedValue val id: Long,
)
how can I add columns to table such that the table has these columns, but my entity does not?
I would like to be able to add these columns programmatically with a for loop. But any pointers would be appreciated.

If you want to alter table programatically just use spring-jdbc (https://www.baeldung.com/spring-jdbc-jdbctemplate) which will allow you to execute SQL queries thus make changes to table programmatically.
It is completely ok if your entity doesn't have all the columns - columns that don't have matching attributes inside of the class just will be ignored by the entity in Spring JPA. Alternatively, you could just use spring-jdbc with a custom row-mapper.

Related

Spring boot with Hibernate envers: all fields in audit table

Is it possible to have an audit table with all the column of the base table, that is updated only when a specific column is modified? If I add the #Audited annotation to my Entity class I have an audit table with all the columns, but a new row is inserted every time a field is modified; otherwise, if I add the #Audited annotation only to one property, hibernate will add a new row inside the audit table only when that property is modified, but the audit table will have only one column ( the column of the property annotated). Any suggestion?

#UniqueConstraint requires alter table in MariaDB if table already existed before with no constraints?

I apologize if I repeat the question, but I did not find a similar one.
I have added a unique constraint on an already existent table. We use MariaDB.
I have used the annotation:
#Table(uniqueConstraints={#UniqueConstraint(name="autonomy_name_energyType", columnNames={"autonomy","name","energyType"})})
The unit tests pass, but in the DB I am still allowed to create duplicates.
Do I need an ALTER table too? By checking the table I can see there are no constraints added to it.
Thanks
As explained in these SO posts :
Unique constraint not created in JPA
#Column(unique=true) does not seem to work
An explicit alter table query is needed for ur constaints to take effect on the db level.
As an extra info, it would have worked if the table was being re-created via JPA. see :
Add a unique constraint over muliple reference columns

Hibernate #Id and oracle Unique constraint

Can I use an hibernate entity with #Id on a unique constraint instead of a primary key on oracle databases? Also will it be JPA compliant?
Sounds a bit strange, but seems possible:
http://www.objectdb.com/api/java/jpa/Id:
The field or property to which the Id annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger.
But be aware:
(1) E.g. in Oracle UNIQUE does not imply NOT NULL, you have to ensure this by your own.
(2) You can't use a FOREIGN KEY constraint in your DB.
(3) You have to be careful with Id generation (http://www.objectdb.com/api/java/jpa/GeneratedValue) if you want to insert new entities (not only read existing ones).
(4) If you insert a new entity, you have to generate a value for the DB id.
And that are only a few disadvantages that came quickly to my mind ...

updating data from one to table to another table which are in two different schema

I got two schema A and B. In A there is a table 'company' and in B there is a table 'newcompany'. Now I need all the data which is residing on A.company to be updated on B.newcompany whenever there is some data updation on A.company.
Please help me out with a query or some function which implements this functionality.
You could give grants to the user and simply access the table as B.newcompany inside an onupdate trigger for the A.company table.

Doctrine - Table without primary keys

I use doctrine.
It's possible to generate entities who based on a database with table (important) without primary key ?
The DB is for Chacal XXI (an application) and 1 or 2 tables have not primary keys, i can't add primary key manually.
No. From https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html#identifiers-primary-keys :
Every entity class must have an identifier/primary key. You can select the field that serves as the identifier with the #Id annotation.

Resources