I want add multiple languages to my database with diferents columns:
id
description
description_es
description_fr
description_ge
price
And return only a translation
class Product(val id: Long, val description: String, val price: Float)
How could I tell Spring Data which description column to use so that it returns the object to me?
The only thing that has happened to me is having classes with inheritance and using @Column for the description in each one, but that forces me to have 4 methods, each one with a type, and using a switch to choose which one to call, which is not elegant not scalable.
Is there any way to pass a column mapping to the repository?
CodePudding user response:
A better table design would be something along the lines of this:
desc_id | description | lang
1 | water | eng
1 | agua | sp
1 | eau | fr
...
This is a more normalized design in which each description/price tuple appears separately for each language. Now if you want a price for a given language and description, you may simply query the table.
CodePudding user response:
Maybe what you want to use either Class-based or Dynamic projection by Spring Data JPA. here is some example
public interface ProductRepository extends JpaRepository<Product, Long> {
// ...
ProductEsDto findById(Long Id);
ProductFrDto findById(Long Id);
ProductGeDto findById(Long Id);
<T> T findById(Long Id, Class<T> type);
}
And here is the requirement for this functionality able to work, I quoted from this reference link for you to look further details.
For a projection class to work in tandem with a repository interface, the parameter names of its constructor must match the properties of the root entity class.