I've been reading some articles about usage of composite keys in MySql and found that a composite key can't own a auto_increment id column. However, I'm interested in using a similar feature. Let's explain it:
Using MariaDB 10 (InnoDB) and Hibernate 3.6.9
I want to do some of my application table fields translatable. I have thought an only table for translations should be enough. This table has a composite key which has an int value as a key for the translation and also the locale value for the concrete text. The same id and locale values can't place as entries.
So that's how the model should look like:
I don't want the translations to be loaded with each of the random entities as a Collection, I'm thinking about a method like String translationFor(Integer id, Locale loc)
could do it for my current locale. However, when I save some translation Set I want to assign them the same id. Let's take this case:
- Spanish: Cuchara
- English: Spoon
The table should look as:
id locale translation
1 es Cuchara
1 en Spoon
But I can't tell MySql to have a composite id with auto_increment column. So, I consider I should assign it manually, performing these steps:
- Build the
Translation
entities with the locale values - Begin a transaction in Hibernate session
- Retrieve the last
id
value in thetranslations
table - Assign it manually to the entities
- Save them
- Commit the transaction
Is it the most proper way? Am I doing it atomically?