views:

1845

answers:

6
@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.

A: 

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

davetron5000
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
A: 

Put the @Column annotation on the getter:

@Column(name="DateOfBirth")
public Date getDateOfBirth() {
...
}
Ryan Anderson
+1  A: 

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
A: 

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

lucas
Are you saying that the only change you made was in the capitalization?
Matt Solnit
Yeah. It seems that ImprovedNamingStrategy was the perpetrator, which sees camelcase column names and converts into lowercase+underscores. Bug or feature, you decide.
lucas
+1  A: 

Possible workaournd. 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 got this problem before. I worked with a legacy columns where there was no space in the column names "employeename", "employeerole", "departmentlocation" And I hate it because all my beans properties had to be without camel case.

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

OscarRyz
+3  A: 

FYI: The reason for the insertion of undrscores 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