Skip to content

Archiving JPA entity causes unnecessary need to set up archive table #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cmetzlerhg opened this issue Nov 25, 2024 · 6 comments
Closed
Assignees
Labels
in: event publication registry Event publication registry type: bug Something isn't working
Milestone

Comments

@cmetzlerhg
Copy link

With version 1.3 there is a new completion-mode value, which should be UPDATE by default. If the property is not set at all, the completion mode seems to be ARCHIVE which would require to introduce a new table and eventually updating the logic to delete archived events.

Quick workaround is setting spring.modulith.events.completion-mode to the desired mode in the application.properties.

@odrotbohm
Copy link
Member

Again: can you please provide more details on what erroneous behavior you see and what you expect? “…seems to be ARCHIVE” is not something we can reasonably act upon. How do you deduce that the archive mode is active?

Generally speaking, the CompletionMode is detected based on CompletionMode.from(Environment) and defaults to CompletionMode.UPDATE.

/**
* Looks up the {@link CompletionMode} from the given environment or uses {@link #UPDATE} as default.
*
* @param environment must not be {@literal null}.
* @return will never be {@literal null}.
*/
public static CompletionMode from(Environment environment) {
Assert.notNull(environment, "Environment must not be null!");
var result = environment.getProperty(PROPERTY, CompletionMode.class);
return result == null ? CompletionMode.UPDATE : result;
}

@cmetzlerhg
Copy link
Author

@odrotbohm when I update the dependency to version 1.3.0 I get the following exception:

09:23:17.949 [main] ERROR o.s.boot.SpringApplication - [] Application run failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [event_publication_archive]

But you're correct, even if I set the property spring.modulith.events.completion-mode to update Hibernate still complains about the missing table.

@cmetzlerhg
Copy link
Author

I will close the ticket, because my assumption was wrong.

@cmetzlerhg
Copy link
Author

@odrotbohm let me know, if you need a new ticket. I think this is related to org.springframework.modulith:spring-modulith-starter-jpa when setting spring.jpa.hibernate.ddl-auto=validate and using Flyway or similar tools to manually create the required tables. Adding a new migration to create the new table event_publication_archive fixes the issue.

@odrotbohm
Copy link
Member

odrotbohm commented Nov 25, 2024

Not so fast. Your high-level observation was incomplete, but you identified an actual problem. :) Here's what I think is happening:

Spring Modulith 1.3 introduced a new JPA entity ArchivedJpaEventPublication mapped to the event_publication_archive table by default. It's located in the same package as DefaultJpaEventPublication, thus found when Spring Boot is scanning for entity types and registered with the EntityManagerFactory. A Hibernate, set up to verify the database schema on startup (spring.jpa.hibernate.ddl-auto=validate), will verify that all tables for all entities registered exist and thus report the archive table missing, independent of which completion mode is configured.

We should try to find a way to only make the archive entity known to Spring Boot if the completion mode is indeed set to archive.

@odrotbohm odrotbohm reopened this Nov 25, 2024
@odrotbohm odrotbohm self-assigned this Nov 25, 2024
@odrotbohm odrotbohm added in: event publication registry Event publication registry type: bug Something isn't working labels Nov 25, 2024
@odrotbohm odrotbohm added this to the 1.4 M1 milestone Nov 25, 2024
@odrotbohm odrotbohm changed the title Update to 1.3 breaks when not setting completion mode Archiving JPA entity causes unnecessary need to set up archive table Nov 25, 2024
odrotbohm added a commit that referenced this issue Nov 25, 2024
…iving setups.

We now keep the JPA entity classes to store current and archived event publications in separate packages so that the archiving one can be included only when the archiving mode is selected.
@odrotbohm
Copy link
Member

This should be fixed in the latest snapshots (also 1.3.1 ones).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: event publication registry Event publication registry type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants