Skip to content
Advertisement

Hibernate Null Attribute Value using Camel-Case

Using

Hibernate 5.3, Spring Boot 2.1, MySQL, that runs in Windows 10 OS.

What I know

I have verified on my phpmyadmin the case of the attributes. And as long the case were the same as the attributes of my Entity class you don’t have to explicitly define the column name in that Entity. And using @Column(name="tableattribute") is not required.

Problem

Once I executes the query, the number of row has been retrieved correctly. For example, my database contains 5 record, the List contains 5 employee objects, but all its attributes of the Entity always returns as null value.

I want to remove explicitly declaring the column name on each attribute and ensure that it will work in the actual server which might cause problem with the case of column name and the attribute name.

Tried

I tried to add @Column('column Name all lowercase') on each attributes and It retrieve the value.

Upon learning this, I verified the table column if it is lower case, but is not in lower case. Instead, It still follow the camel-case that is in my SQL command to create the table.

MySQL Table

   CREATE TABLE `personal` (
   `empID` int(11) NOT NULL,
   `empNumber` varchar(15) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
   `surname` varchar(50) NOT NULL DEFAULT '',
   `firstname` varchar(50) NOT NULL DEFAULT '',
   `middlename` varchar(50) NOT NULL DEFAULT '',
   `middleInitial` varchar(10) DEFAULT NULL,
   `nameExtension` varchar(10) DEFAULT '',
   `salutation` varchar(15) NOT NULL DEFAULT ''
   ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Entity

This Entity is now working fine as long as @Column and specify the column name in lowercase. But it is annoying to add each attribute with @Column and specify the name of the field in all lower case.

  @Entity
  @Table(name="personal")
  public class Employee implements Serializable {
     @Id
     @Column(name="empID")
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private long id;

     @NotNull
     @Column(name="empnumber")
     private String empNumber;

     private String surname;

     private String firstname;

     private String middlename;

     @Column(name="middleinitial")
     private String middleInitial;

     @Column(name="nameextension")
     private String nameExtension;
 }

Actual Query Snippet on DAO

     @Autowired
     private EntityManager entityManager;

     @Override
     public List<Employee> findAll() {
          Session currentSession = entityManager.unwrap(Session.class);
          Query<Employee> query = currentSession.createQuery("from Employee", Employee.class);
          return query.getResultList();
     }

Advertisement

Answer

You can change that behavior by setting

spring.jpa.hibernate.naming.physical-strategy
spring.jpa.hibernate.naming.implicit-strategy

properties in your application.properties. On how to implement the strategies yourself, you can check out official docs.

Edit 1:

spring.jpa.hibernate.naming.physical-strategy = org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 
spring.jpa.hibernate.naming.implicit-strategy = org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement