32
@Column(name="DateOfBirth")
private Date dateOfBirth;

I specifically need the above code to create a column named "DateOfBirth," instead Hibernate gives me a column named date_of_birth. How can I change this? Is there a web.xml property? I came across DefaultNamingStrategy and ImprovedNamingStrategy, but not sure how to specify one or the other.

lucas
  • 6,951
  • 4
  • 33
  • 34

10 Answers10

41

Try putting this in

application.properties

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
34

FYI: The reason for the insertion of underscores is probably because you're using an ImprovedNamingStrategy. It's set on your Configuration object. See here for an example...

If you don't want the underscores you can just not set the naming strategy, or set it to the DefaultNamingStrategy you discovered earlier.

Dan Vinton
  • 26,401
  • 9
  • 37
  • 79
12

Here is a possible workaround: if you name it dateofbirth the column in the DB would be named like that, but the attribute name should be the same.

Hibernate takes the camel case format to create/read database columns.

I've had this problem before. I worked with a legacy columns where there was no space in the column names "employeename", "employeerole", "departmentlocation". I hate it because all my beans properties had to be without camel case.

Database columns separated by "_" will be used to properly camelCase as you have just seen.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
10

add below property in the case of spring boot.

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

ErSyyedS
  • 230
  • 5
  • 9
  • 1
    This used to work but since spring-boot 2 (2.0.4) and hibernate 5.2 (5.2.17) it does not work anymore. I debugged through entire hibernate and the naming strategies actually do not interfere but some other hibernate magic afterwards does the transformation to add underscores to camlCase properties. This really sucks especially that an explicit @Column(name="nameWithCamlCase") does not help and hibernate still thinks to be smarter than what the developer wanted. I assume some latest JPA compliance issue is causing this madness. – Jörg Oct 30 '18 at 11:08
7

Put the @Column annotation on the getter:

@Column(name="DateOfBirth")
public Date getDateOfBirth() {
...
}
Ryan Anderson
  • 502
  • 5
  • 16
5

ImprovedNamingStrategy has method addUnderscores() which is called from tableName() and columnName() you can implement your own naming strategy class and override these as per your choice

    public class MyOwnNamingStrategy extends ImprovedNamingStrategy {
        @Override
        public String tableName(String tableName) {
        //return addUnderscores(columnName); // skip this
        return columnName; // if you want column name variable name same
        //return changeAsYouWant(columnName); // as name sames
       }
   }
jonsca
  • 10,218
  • 26
  • 54
  • 62
syedmuneer
  • 51
  • 1
  • 1
  • I would suggest also calling "super(tableName)" on any tablenames you do not need your special convention... maybe – mschmoock Oct 15 '12 at 16:41
5

The workaround proposed was to use @Column(name="dateofbirth"), which worked for my purposes.

lucas
  • 6,951
  • 4
  • 33
  • 34
  • Are you saying that the only change you made was in the capitalization? – Matt Solnit Dec 17 '08 at 22:24
  • Yeah. It seems that ImprovedNamingStrategy was the perpetrator, which sees camelcase column names and converts into lowercase+underscores. Bug or feature, you decide. – lucas Dec 18 '08 at 16:54
1

You can annotate either fields or getter methods, it doesn't make a difference. Can you post your full hibernate.cfg.xml or persistence.xml file?

cliff.meyers
  • 17,666
  • 5
  • 51
  • 66
0

I'm not 100% sure, but don't you need to annotate the get method and not the private variable?

davetron5000
  • 24,123
  • 11
  • 70
  • 98
  • I don't think so? At the very least, I can specify things like length at the instance variable level and they will alter the columns accordingly.. – lucas Dec 17 '08 at 21:38
  • You can annotate either one, but you must annotate them consistently. If you mix them and annotate some headers and some getters, it will find one and ignore the other. – Nathan Dec 05 '15 at 11:04
0

I had a similar problem and adding the following two properties to my application.properties solved my issue: spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl .