Hibernate Field Naming with Spring Boot
Issue: BAEL-2098
This commit is contained in:
committed by
Josh Cummings
parent
18d7252bdf
commit
184d60ca6e
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.naming;
|
||||
|
||||
import org.hibernate.jpa.boot.spi.IntegratorProvider;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Custom implementation of the {@link HibernatePropertiesCustomizer HibernatePropertiesCustomizer}.
|
||||
* We can use it to set custom hibernate properties.
|
||||
*/
|
||||
public class HibernateConfig implements HibernatePropertiesCustomizer {
|
||||
|
||||
@Override
|
||||
public void customize(Map<String, Object> hibernateProperties) {
|
||||
hibernateProperties.put("hibernate.integrator_provider", (IntegratorProvider) () -> Collections.singletonList(MetadataExtractorIntegrator.INSTANCE));
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.naming;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.model.relational.Database;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.integrator.spi.Integrator;
|
||||
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
|
||||
|
||||
/**
|
||||
* Custom implementation of the {@link Integrator Integrator} interface.
|
||||
* We can use it to getting access to the binding metadata between entity mappings and database tables.
|
||||
*/
|
||||
public class MetadataExtractorIntegrator implements Integrator {
|
||||
|
||||
public static final MetadataExtractorIntegrator INSTANCE = new MetadataExtractorIntegrator();
|
||||
|
||||
private Database database;
|
||||
|
||||
private Metadata metadata;
|
||||
|
||||
public Database getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public Metadata getMetadata() {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
|
||||
|
||||
this.database = metadata.getDatabase();
|
||||
this.metadata = metadata;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
|
||||
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.naming.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Account {
|
||||
|
||||
@Id private Long id;
|
||||
|
||||
private String defaultEmail;
|
||||
|
||||
@OneToMany List<Preference> preferences;
|
||||
|
||||
@Column(name = "\"Secondary_Email\"") private String secondaryEmail;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.naming.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Preference {
|
||||
|
||||
@Id private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne private Account account;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user