Javers fails after changing the primary key from long to UUID - javers

I have just changed the primary key of an entity from long to uuid.
Now, when I try to change an object, javers fails with an Exception.
java.lang.IllegalArgumentException: Invalid UUID string: 7
at java.base/java.util.UUID.fromString(UUID.java:215)
at org.javers.core.json.typeadapter.util.UUIDTypeAdapter.deserialize(UUIDTypeAdapter.java:19)
at org.javers.core.json.typeadapter.util.UUIDTypeAdapter.deserialize(UUIDTypeAdapter.java:10)
at org.javers.core.json.BasicStringTypeAdapter.fromJson(BasicStringTypeAdapter.java:45)
at ........
at org.javers.spring.jpa.JaversTransactionalJpaDecorator.commit(JaversTransactionalJpaDecorator.java:50)
Javers seems to try to deserialize the old long-id as a UUID, which fails.
I could not find any recommended way of handling this situation.

Related

Spring Boot Apache Derby Duplicate Key Value Issue

Seeing this Exception:
This is my POST Request.
I tried with different key value pairs. Still the same error.
My Entity Class.
Error
The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL200516190100880' defined on 'DATA_STORE'.
Anything I could be missing?

DatastoreException: The given key doesn't have a String name value but a conversion to String was attempted

Changed #Id type from Long to String in GCP datastore using spring java Repository.
DatastoreDataException
org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException: The given key doesn't have a String name value but a conversion to String was attempted
So Keys in datastore can either have the property id which is a number or the property name which is a string.
I included 2 screenshots of an example of each
Numeric id:
String name:
So when you say this:
Changed #Id type from Long to String in GCP datastore using spring java Repository.
What did you actually do?
It sounds like you just changed a model definition in your ORM. This doesn't actually change anything already stored in the datastore, it only impacts new entities going forward. So it sounds like, you're fetching entities with ids but your model definition is expecting them to have names.
You would have to have some kind of data migration job convert them all over. Convert isnt even the right word since changing the key to use name instead would just create a new entity. You would have to delete the old entities that use id in this process.
You would also have to update all other entities that have key properties to this kind too.
So we changed the Id from Long to String. And datastore table was already created with Long Id. so when we changed it we saw the above exception. By creating new table with String Id we resolved the issue.

Hibernate looking for column which I've marked as #Transient

I have a JPA entity with a few fields which is running well. I now am adding a new field and have marked it #Transient as I donot want it to be persisted. However on running tests or deploying the app I get an error stating: Caused by: org.hibernate.HibernateException: Missing column: columnName in dbname.tableName
Why is Hibernate looking for the column in the database schema even though I've marked it to be Transient?
Was a mistake on my part. Was using org.springframework.data.annotation.Transient whereas should've been using javax.persistence.Transient.

when more foreign keys and need to insert is it good to use native #Query instead of JPA managed way

There is a table with 3 foreign keys to three tables.
To do an insert using JPA, is it suggested/performant to
query individual tables(having foreign key relations) and create
respecitve objects and do a .save()
?
or
use native #Query(), with #Transactional and #Modifying?
for making an insert, i am making 3 calls to DB to get respective objects/details and use them for insertion. so total 4 calls.
If i use native Query, i have the id's required(getting from client) i can do it in one query.
so, is it good to do in JPA way or use native query? which is good in view of performance?
If you have the primary keys of the dependencies you can call EntityManager.getReference().
This will return a placeholder that you can use to set as the dependencies in your entity. That way no SQL statement is executed for the dependencies.
From the API Doc:
<T> T getReference(Class<T> entityClass,
Object primaryKey)
Get an instance, whose state may be lazily fetched.
If the requested instance does not exist in the database, the EntityNotFoundException
is thrown when the instance state is first accessed.
(The persistence provider runtime is permitted to throw the EntityNotFoundException when getReference is called.)
The application should not expect that the instance state will be available upon detachment,
unless it was accessed by the application while the entity manager was open.
Parameters:
entityClass - entity class
primaryKey - primary key
Returns:
the found entity instance
Throws:
IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that entity's primary key or is null
EntityNotFoundException - if the entity state cannot be accessed
https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#getReference-java.lang.Class-java.lang.Object-

entity framework without primary key

I have created an entity class for my mvc application. But, my table doesn't have a primary key and the VS throws back an error:
"The table/view does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view."
Is there a way to still use the model even if it has no primary key??
or Do I have to add a pk? or should I just use execute command?
Any suggestion/comment is highly appreciated. Thanks in advance. :)

Resources