tags:

views:

63

answers:

2

How can I order JPA to set a MySQL database column with text content as case sensitive by default upon creation?

A: 

MySQL varchar type is not case-sensitive. What you want is varbinary.

CREATE TABLE IF NOT EXISTS `stuff` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varbinary(255) NOT NULL            -- varbinary to be case sensitive
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Chadwick
It's not strictly true that `varchar` is case insensitive - it depends on the table collation. For example, `utf8_unicode_cs` is case sensitive, whereas `utf8_unicode_cs` is not.
Mike
But how can I have JPA do set this automatically upon automatic schema creation? I don't want to manipulate the generated schema by hand every time I use my program on a different computer or database instance.
ali
I don't know anything about JPA, but I have just found [Character Encoding UTF-8 with JPA/Hibernate, MySql and Tomcat](http://mathiasrichter.blogspot.com/2009/10/character-encoding-utf-8-with.html). It may be of some use.
Mike
+1  A: 

The @Column annotation on your field can specify a columnDefinition attribute which may allow you to specify a case-sensitive collation for the column.

public abstract String columnDefinition

    (Optional) The SQL fragment that is used when generating the DDL for the column.
    Defaults to the generated SQL to create a column of the inferred type.
    Default:
        ""

In your case, for example, using the @Column annotation, you would use

@Column(name = "NAME_COL", columnDefinition = "VARCHAR(250) COLLATE latin1_general_cs") private String name;

Fly
Does not work unfortunately. If I do as advised it seems that JPA has problems mapping the String datatype to the column and throws an exception.
ali
Can you provide the exception and its message? What JPA implementation are you using?
Fly