When using discriminator value for single table strategy, the first inserted entity’s discriminator value is null but the value is there in database

You are not setting the ‘type’ field within your entities, and JPA doesn’t set it for you – not in the java object anyway. If it isn’t set when you persist an entity, it will remain unset for as long as that entity is cached (locally or the shared EMF level cache). Restarting the app works because it clears the cache, forcing any fetches of existing entities to load from the database, where the type was set based on the discriminator column value.

You can set the type when creating the class, or force the data to be reloaded from the database by calling em.refresh on the instance. In this case though, it seems strange to even bother mapping the type column as a basic mapping – the getType method should just return the static discriminator value for the class, and you cannot change the type string anyway.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top