No Exception when Spring batch tables are not created - spring-batch

If the BATCH_JOB_EXECUTION_CONTEXT was not created all I get is :
Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO BATCH_JOB_EXECUTION_CONTEXT (SHORT_CONTEXT, SERIALIZED_CONTEXT, JOB_EXECUTION_ID) VALUES(?, ?, ?)]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
the error above is displayed while inserting into the table, but at the moment of the creation of the table (done automatically by spring batch )no error was displayed.
I would like to know the reason way the table was not created so I could know what is the issue (database permissions, problems of tablespace...) is it possible to log it in traces?
for information I'm using oracle database 12C and ojdbc8 version 12.2.0.1

Spring Batch does not automatically create meta-data tables in your datasource. You need to run the tables creation script against your database manually.
However, if you use Spring Boot, those tables can be created automatically using the spring.batch.initialize-schema property. More details here: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-initialize-a-spring-batch-database
There are similar/related questions to this one, I'm adding them for reference here:
Spring Batch Framework - Auto create Batch Table
Spring Batch doesn't use custom datasource to create tables

Check your Spring Boot starter class or configuration class and make sure you are not excluding this auto configuration:
(exclude={BatchAutoConfiguration.class})
Also make sure this property is not set to never:
spring.batch.initialize-schema

Related

How to implement audit on a table using Hibernate?

I'm new to Hibernate and I want to audit all changes made to a table, to get logged into its Audit table.
In other words, any INSERT, UPDATE, DELETE on TEST table must be inserted into the TEST_AUD table.
I read a few articles on using dynamic audit tables that are created, but I cannot use it. The audit table must not be automatically generated.
I did not get a satisfactory article or solution to implement auditing using entities and Hibernate in SpringBoot?
Can someone please suggest a good article or provide an example?
Thanks!
You should use hibernate envers: https://hibernate.org/orm/envers/
set hibernate.hbm2ddl.auto to create, create-drop or update
or use org.hibernate.tool.EnversSchemaGenerator
You can audit entity or properties. If you entity name is TEST, the TEST_AUD will be automatically created.
You can also follow this article https://www.baeldung.com/database-auditing-jpa for more informations.

I had make composite key in my spring boot services of two column (floor id & ticketid)

I have to make a composite key in my spring boot service, using two columns (floorid & ticketid) from an existing table to prevent duplicate records.
But suddenly I get the following error when the save call is made:
Error is identifier of an instance of com.ge.current.digitaltwin.devicemap.entity.DeviceMapCompEntity was altered from com.ge.current.digitaltwin.devicemap.entity.DeviceMapId#3f3b65f9 to com.ge.current.digitaltwin.devicemap.entity.DeviceMapId#91bc6f2; nested exception is org.hibernate.HibernateException: identifier of an instance of com.ge.current.digitaltwin.devicemap.entity.DeviceMapCompEntity was altered from com.ge.current.digitaltwin.devicemap.entity.DeviceMapId#3f3b65f9 to com.ge.current.digitaltwin.devicemap.entity.DeviceMapId#.
Is there another way to prevent duplicate records without changing the code and bymaking some changes in backend?
I don't know what is happening. Can anybody help me on this?

JPA table capitalization inconsistent

We're using a very basic JPA implementation that should create tables consistently from our models.
I believe we're using EclipseLink or TopLink (whichever one is default with the latest Netbeans/Glassfish). The problem is, the tables are created with inconsistent capitaliztion and with the columns out of order. For me, It creates the "User" table as "user", and for other members of my team it creates "USER".
I've tried using the #Table annotation (#Table(name="USer")), but it doesn't work.
How do we get EclipseLink to generate consistent table names? Frankly this seems like a rather amateurish mistake for a framework like this.
Sub-question : the reason this is a problem is because EclipseLink by default has no default way of managing schema/data migrations, as far as I know of. The way we're handling it is by writing a bunch of INSERT INTO's to bootstrap the objects we need in our database, and drop-and-recreating the tables every time the schema changes. I know this is not the best practice for propagating schema changes -- does anyone know how this is typically handled in a standard JPA implementation?
Thanks.
By default EclipseLink uses all upper case for the table name, the class User would be USER.
If you specify an #Table annotation with name="USer", then the table will be created as "USer".
Perhaps you are using your own scripts to create the tables, or you database is changing the case based on the OS or its own settings. What database are you using?
If you enable logging in EclipseLink, it will show the exact DDL that it is executing (if it is executing DDL).
In EclipseLink 2.4 there is also a "create-or-extend-tables" DDL generation option to alter existing tables.
We never found any good answer for this. Luckily, we found a workaround for the ways we were using to update the table, which didn't care about capitalization.

Where the CREATE DATABASE statement is generated in Entity Framework Code First with Migrations?

I need to configure the database created by Entity Framework Code First MigrateDatabaseToLatestVersion class.
Is it possible to influence the database files parameters like size or maxsize? What interests me in particular, is there a way to add database files?
I assume I need to find the moment where the Entity Framework generates the CREATE DATABASE statement and influence it a bit. As far as I understand, the SqlServerMigrationSqlGenerator class is too late because the database already exists at this point.
Edit: According to comment this doesn't work:
You can just add code based migration using Add-Migration command and modify its Up method to execute Sql("ALTER DATABASE ...") and do what ever you want with your database.
Database is created through DbProviderServices - that is a bridging component between EF and specific database server. ObjectContext exposes operations for creating database and generating database creation script. DbMigrator use database creation operation when you execute Update. It checks that if Database exists and if not it uses ObjectContext.CreateDatabase. So if you want to change the process of creating the database you need to implement your own implementation of the migrator to change the way how Update method generates the database (maybe you just need a decorator which will create a database prior to executing DbMigrator.Update operation).

JPA persist same entity twice

I just noticed that a part of code I worked on saves the same entity twice.
I tried several tricks, like flush() and Transaction REQUIRES_NEW but nothing does it.
But I can see while debugging that after calling persist(entity), I get an ID (IDENTITY) for that entity... but just a bit later in the code if I look at the variables I'll see the same entity with a different ID and two itenties with those IDs in the Table.
This is using Java EE 6 with Glassfish 3.0.1
What JPA provider are you using? and what version?
Enable logging (on finest) and include the log of what occurs.
If using EclipseLink to enable logging see,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging
What value is assigned to your object's id, check to ensure it is > 0.
Also ensure that you table in the database was generated with an IDENTITY column.
What database are you using?
Try using TABLE sequencing to see if it has to do with IDENTITY sequencing.
Also include the source code for you class.