'Hibernate : functionnally calling hibernate to recreate constraints
I have been deferring my hibernate DDL usage time after time, from create-drop, to update and now i'm using other tools for database migration, and, while still respecting my hibernate entities, i am now between validate and no control.
Can i still use hibernate DLL programatically ? For columns, i am using a hbm2ddl : e.g.
javax.persistence.schema-generation.scripts.action
javax.persistence.schema-generation.scripts.create-target
hibernate.hbm2ddl.delimiter
to generate my scripts..
And i'm currently developping something specifically to do constraints.. Which i think is less of a problem than adding a column, so i wanted to delegate this really as of a update in hibernate. And it would be even better if i could just call a function to check for constraints or recreate them.
Does any function exists in such a way (i did not find this in documentation)
Thanks ! :)
Solution 1:[1]
Actually i'm at that point : i found out how hibernate naming strategy seem to work and i'm wishing to twist it to get access to the constraits : Unique keys, foreign key and primary keys.. However there are many flaws : i column names are seemingly null on foreign and identifier have to respect certain length, which doesn't seem like ok.. I might go with some md5 hashing
import org.hibernate.boot.model.naming.*;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Par défaut hibernate utilise une convention de nommage implicite, sauf que les noms de clé étrangères et certaines contraintes
* sont alimentés d'un Hash
* Nous avons décidé de fixer les noms pour pouvoir y faire référence par ailleurs.
*/
public class CustomisationStrategieNommageImplicite extends ImplicitNamingStrategyJpaCompliantImpl {
private List<Identifier> declaredIdentifiers = new ArrayList<>();
public List<Identifier> getDeclaredIdentifiers() {
return declaredIdentifiers;
}
public void addIdentifier(Identifier identifier){
this.declaredIdentifiers.add(identifier);
}
@Override
public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {
Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
List<List<Identifier>> identifiers = List.of(List.of(source.getTableName()),List.of(source.getReferencedTableName()), source.getColumnNames(),source.getReferencedColumnNames());
Identifier generatedIdentifier = generateIdentifierFromIdentifiersBuildingContextAndPrefix(identifiers,source.getBuildingContext(),"fk");
Identifier definitiveIdentifier = userProvidedIdentifier != null ? userProvidedIdentifier : generatedIdentifier;
addIdentifier(definitiveIdentifier);
return definitiveIdentifier ;
}
@Override
public Identifier determineUniqueKeyName(ImplicitUniqueKeyNameSource source) {
Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
List<List<Identifier>> identifiers = List.of(List.of(source.getTableName()), source.getColumnNames());
Identifier generatedIdentifier = generateIdentifierFromIdentifiersBuildingContextAndPrefix(identifiers,source.getBuildingContext(),"uk");
Identifier definitiveIdentifier = userProvidedIdentifier != null ? userProvidedIdentifier : generatedIdentifier;
addIdentifier(definitiveIdentifier);
return definitiveIdentifier ;
}
@Override
public Identifier determineIndexName(ImplicitIndexNameSource source) {
Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
List<List<Identifier>> identifiers = List.of(List.of(source.getTableName()), source.getColumnNames());
Identifier generatedIdentifier = generateIdentifierFromIdentifiersBuildingContextAndPrefix(identifiers,source.getBuildingContext(),"uk");
Identifier definitiveIdentifier = userProvidedIdentifier != null ? userProvidedIdentifier : generatedIdentifier;
addIdentifier(definitiveIdentifier);
return definitiveIdentifier;
}
private Identifier generateIdentifierFromIdentifiersBuildingContextAndPrefix(List<List<Identifier>> identifiers, MetadataBuildingContext context, String prefix){
return toIdentifier(
prefix+"_"+ identifiersToString(identifiers),
context
);
}
private String identifiersToString(List<List<Identifier>> identifier){
return identifier.stream().map(list-> identifierToString(list)).collect(Collectors.joining("_"));
}
// identifier peut etre un element simple ou multiple
private String identifierToString(List<Identifier> identifiers){
if (identifiers.isEmpty()){
return "";
}
return identifiers.stream().map(Identifier::getText).reduce((a,b)->a+"_"+b).orElse("");
}
}
I was wondering how ok would be an implementation of a Singleton pattern or smt to be able to get those identifiers and once and for all be able to check indexes through information schema then create the ones that are not present. But I'm not really happy of the Identifier spec.
Any plan?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ragatzino |