Save object with uppercase vs lowercase letter

Asked

Viewed 1,111 times

1

I think I had posted a question previously equal to this, but I did not find.

I have a Manufacturer entity where I want to persist, to avoid differentiating between upper and lower case letters, can I save an Object with all upper or lower case? This is good practice?

  • What is DBMS? Mysql, SQL Server, Sqlite?

  • I am using Mysql

  • A priori, let the bank take care when making comparisons. This you guarantee in the creation of the table. I will try to describe better in a reply

  • in the case I’m using JPA and Hibernate, I don’t know if there are any notes or anything like that that can facilitate.

1 answer

1

Here we are dealing with the persistence of objects coming from the browser. We need to ensure that, in the database, there is no difference between the name of the Manufacturer all in High Cash or all in Low Cash.

Assuming that the information entered by the user is the right one most of the time, we should not (most of the time) touch the data and coerce it. Not even coercion to force high/low cash.

Ideally, DBMS should treat this column as insensitive to the case/case insensitive. You can specify how the column will be created using the property columnDefinition annotation @Column. For example, in the international SO, they did an example of changing the collation one-column for latin1_general_cs.

In the case of Mysql, there are several leotard and ways to place yourself in a column. If the value of leotard is omitted, it will use the default for the table; if this is also omitted, it will use what is configured in the database.

In our case, we want to make sure you’re insensitive to the case, so we can put it in the column:

// baseado no exemplo do link do SO internacional
@Column(name = "NAME_COL", columnDefinition = "VARCHAR(250) COLLATE latin1_general_ci")
private String name;

Recommended reading:

  • thanks for the help. I didn’t understand this collate latin1_general_ci ? so it stores in high box?

  • Not at all. If it was useful to you, I ask you to give an upvote ;-]

  • No. The leotard does not indicate storage, but indicates how the value will be treated in operations. In reply, I justify why I want to store the way that was informed by the user.

  • 1

    @daysonrodrigues It would also be good to find a way to leave the bank as a case insensitive by default, so as not to need to specify this in each column. Leaving the case sensitive and forcing a case at the time of recording (either upper or lower case), will force you to force the case also when searching, giving much more work and if not done very carefully may even cause performance problems.

  • My answer was in the column, so I guarantee search too. I haven’t touched Mysql in a while, I can’t remember how to change the leotard standard. However, I believe the references I have put in help in researching this point

Browser other questions tagged

You are not signed in. Login or sign up in order to post.