I have a table "greetings" in my DB (Postgres 11) with columns: language, translation_string, text
and object type enum "language" ('en', 'ru')
The goal is to retrieve the text in required locale.
I have some populated data at "greetings"
+---------------+------------------------+-----------------------+
| language | translation_string | text |
|(enum language)|(varchar) (primary key) | (varchar) |
|(primary key) | | |
+---------------+------------------------+-----------------------+
| en | new year greting | Happy New Year! |
| ru | new year greting | С Новым Годом! |
+---------------+------------------------+-----------------------+
I can't map data correctly and get "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet". Also i have a composite key case, and it makes things hard.
I've changed the column type in test DB to VARCHAR and it perfectly works. But i can't do that in work database...
My language enum:
public enum Language {
en, ru;
public static Language getDefaultLocale(){
return ru;
}
}
My TranslationRepository.class
@Repository
public interface TranslationRepository extends JpaRepository<Greeting, GreetingIdentity> {
Greeting getGreetingByIdentity(GreetingIdentity identity);
}
My Greeting.class
@Data
@Entity
@Table(schema = "common", name = "greetings")
public class Greeting {
@EmbeddedId
private GreetingIdentity id;
@Column(name = "text")
private String text;
}
My GreetingIdentity.class
@Embeddable
public class GreetingIdentity implements Serializable {
@NotNull
@Enumerated(EnumType.ORDINAL)
private Language lang;
@NotNull
private String translation_string;
public GreetingIdentity(@NotNull String translation_string) {
this.lang = Language.getDefaultLocale();
this.translation_string = translation_string;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
GreetingIdentity that = (GreetingIdentity) obj;
if (!lang.equals(that.lang)) return false;
return translation_string.equals(that.translation_string);
}
@Override
public int hashCode() {
int result = lang.hashCode();
return 31 * result + translation_string.hashCode();
}
}
calling translationRepository.getGreetingByIdentity(Language.getDefaultLocale(), "new year greting") with enum type in DB fails with a mentioned above error... What am i doing wrong?