Environment:
Spring Boot version: 3.2.1 with Hibernate-Core 6.4.1.Final
Also tested with 3.3.1 with Hibernate-Core 6.5.2.Final
Database: MS SQL Server with multiple accessible databases
Issue:
I have a setup where I need to map a sequence to the @Id field of an entity using @GeneratedValue and @SequenceGenerator annotations. The entity itself is mapped to Database A (it’s a View), but the sequence it relies on is located in Database B (which also store the Table). I specify the “catalog” and “schema” parameters in the @SequenceGenerator annotation to point to the correct location of the sequence in Database B.
When I configure my Spring Boot API with spring.jpa.hibernate.ddl-auto set to validate, Hibernate throws an error at startup (from class AbstractSchemaValidator), indicating that it cannot find the sequence. It seems to only query the INFORMATION_SCHEMA.SEQUENCES of the database that the connection is opened for, rather than checking across all specified catalogs in the entity mappings.
Switching ddl-auto to none avoids this error at startup, and the application runs fine with the sequence being correctly utilized at runtime.
Expected Behavior:
Hibernate should correctly locate the sequence in Database B when the catalog and schema are properly specified in the @SequenceGenerator annotation, even when ddl-auto is set to validate.
Actual Behavior:
Hibernate fails to find the sequence unless ddl-auto is set to none, suggesting it does not fully honor the catalog settings in multi-database environments during schema validation. This throws the error message :
Schema-validation: missing sequence [“B.schema_from_B.my_sequence”]
Code Example :
@Getter
@Setter
@Entity
@Table(name = "my_view", schema = "schema_from_A", catalog = "A")
public class ViewEntity {
@Id
@GeneratedValue(strategy = SEQUENCE, generator = "my_seq_generator")
@SequenceGenerator(name = "my_seq_generator", catalog = "B", schema = "schema_from_B", sequenceName = "my_sequence")
@Column(name = "id")
private Integer id;
}
Configuration YML :
Here’s an extract of my configuration
spring:
datasource:
url: jdbc:sqlserver://<ip_of_server>;databaseName=A;encrypt=true;trustServerCertificate=true
jpa:
properties:
hibernate:
default_catalog: A
default_schema: my_schema_A
hibernate:
ddl-auto: validate
1