MyBatis Generator not keeping camelcasing of field names

183 views
Skip to first unread message

Rovanion Luckey

unread,
Apr 13, 2014, 2:02:49 PM4/13/14
to mybati...@googlegroups.com
Hi,
I'm having the issue where the fields/columns in the database have names that are in CamelCase. But when MyBatis Generator generates code from those tables it does not keep the original camelcasing of the names, no matter what the table property useActualColumnNames is set to.

So for example the table field/column userName is represented as a variable username in the generated java code instead of userName.

Jeff Butler

unread,
Apr 13, 2014, 8:20:23 PM4/13/14
to mybati...@googlegroups.com
Most JDBC drivers do not return mixed case names unless the column names have been delimited on the create table statement.  What database are you using?

Jeff Butler



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rovanion Luckey

unread,
Apr 14, 2014, 3:32:25 AM4/14/14
to mybati...@googlegroups.com
I'm using postgresql.

Jeff Butler

unread,
Apr 14, 2014, 4:58:24 AM4/14/14
to mybati...@googlegroups.com
I should have known :)

PostgreSQL forces everything to lower case unless you delimit the names. For example

create table foo (userName varchar ...

Will create a table with the actual column name being username. 

To create a mixed case name you have to do this:

create table foo ("userName" varchar ...

But then you must delimit the name with every SQL statement you write (MyBatis generator has options to force delimited names in generated SQL). 

So you have to first figure out what the actual column name is. If the generated SQL is working, then then column name is probably username and not userName. If you really want it camel case in the database you will have to recreate the table using delimiters, but then you have to be careful to delimit every SQL statement you write. My advice would be DON'T DO THIS. You will end up creating a database that is very difficult to work with. 

This is why most people write names like user_name - you still get readability and you don't have to deal with delimited names.

Jeff Butler

Rovanion Luckey

unread,
Apr 14, 2014, 5:08:00 AM4/14/14
to mybatis-user
Well that is sweet of them. Will user-name work as well as user_name?

Jeff Butler

unread,
Apr 14, 2014, 5:23:23 AM4/14/14
to mybati...@googlegroups.com
I'm not sure - it still might require a delimiter. But imagine this SQL statement...

select user-name from foo

Does it mean a column called user-name or does it mean to subtract name from user?

In other words - it might work but it is confusing. I'd stick with the underscore as it is well understood and works. These types of conventions exist for a reason. 

BTW - PostgreSQL is a bit more sensitive to these types of issues than other databases, but even so most databases assume that undelimited SQL is case insensitive. Oracle and DB2 will make the column names upper case unless delimited. 

Rovanion Luckey

unread,
Apr 14, 2014, 5:26:53 AM4/14/14
to mybatis-user
Thank you for your answers!
Reply all
Reply to author
Forward
0 new messages