BAEL-1876: Hibernate 5 Naming Strategy Configuration (#5158)
This commit is contained in:
parent
1a4fe225af
commit
e92e71df1c
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.hibernate.namingstrategy;
|
||||||
|
|
||||||
|
import org.hibernate.boot.model.naming.Identifier;
|
||||||
|
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
|
||||||
|
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||||
|
|
||||||
|
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||||
|
return convertToSnakeCase(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Identifier toPhysicalColumnName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||||
|
return convertToSnakeCase(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Identifier toPhysicalSchemaName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||||
|
return convertToSnakeCase(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||||
|
return convertToSnakeCase(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
|
||||||
|
return convertToSnakeCase(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Identifier convertToSnakeCase(final Identifier identifier) {
|
||||||
|
if (identifier == null) {
|
||||||
|
return identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String regex = "([a-z])([A-Z])";
|
||||||
|
final String replacement = "$1_$2";
|
||||||
|
final String newName = identifier.getText()
|
||||||
|
.replaceAll(regex, replacement)
|
||||||
|
.toLowerCase();
|
||||||
|
return Identifier.toIdentifier(newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.hibernate.namingstrategy;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "Customers")
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
@Column(name = "email")
|
||||||
|
private String emailAddress;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmailAddress() {
|
||||||
|
return emailAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmailAddress(String emailAddress) {
|
||||||
|
this.emailAddress = emailAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
package com.baeldung.hibernate.namingstrategy;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.boot.MetadataSources;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class NamingStrategyLiveTest {
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
try {
|
||||||
|
Configuration configuration = new Configuration();
|
||||||
|
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.load(Thread.currentThread()
|
||||||
|
.getContextClassLoader()
|
||||||
|
.getResourceAsStream("hibernate-namingstrategy.properties"));
|
||||||
|
|
||||||
|
configuration.setProperties(properties);
|
||||||
|
|
||||||
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||||
|
.build();
|
||||||
|
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||||
|
metadataSources.addAnnotatedClass(Customer.class);
|
||||||
|
|
||||||
|
SessionFactory factory = metadataSources.buildMetadata()
|
||||||
|
.buildSessionFactory();
|
||||||
|
|
||||||
|
session = factory.openSession();
|
||||||
|
} catch (HibernateException | IOException e) {
|
||||||
|
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void close() {
|
||||||
|
if (session != null)
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCustomPhysicalNamingStrategy() {
|
||||||
|
|
||||||
|
Customer customer = new Customer();
|
||||||
|
customer.setFirstName("first name");
|
||||||
|
customer.setLastName("last name");
|
||||||
|
customer.setEmailAddress("customer@example.com");
|
||||||
|
|
||||||
|
session.beginTransaction();
|
||||||
|
|
||||||
|
Long id = (Long) session.save(customer);
|
||||||
|
|
||||||
|
session.flush();
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
Object[] result = (Object[]) session.createNativeQuery("select c.first_name, c.last_name, c.email from customers c where c.id = :id")
|
||||||
|
.setParameter("id", id)
|
||||||
|
.getSingleResult();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
hibernate.connection.driver_class=org.h2.Driver
|
||||||
|
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
|
||||||
|
hibernate.connection.username=sa
|
||||||
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
|
||||||
|
hibernate.show_sql=true
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
|
||||||
|
hibernate.physical_naming_strategy=com.baeldung.hibernate.namingstrategy.CustomPhysicalNamingStrategy
|
||||||
|
hibernate.implicit_naming_strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
|
Loading…
x
Reference in New Issue
Block a user