2

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?

Avi Meltser
  • 409
  • 4
  • 11
darth jemico
  • 605
  • 2
  • 9
  • 18
  • Can you try removing `@Enumerated(EnumType.ORDINAL)` in your GreetingIdentity class – Guillaume May 31 '19 at 10:21
  • The same error:( Resolved [org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] – darth jemico May 31 '19 at 11:27
  • What is the error message of the underlying exception? Can you please edit your question with the stacktrace? – Guillaume May 31 '19 at 12:17
  • Solved my issue! Thanks to https://stackoverflow.com/questions/27804069/hibernate-mapping-between-postgresql-enum-and-java-enum I've just added a @TypeDef(name = "psql_enum", typeClass = PostgreSQLEnumType.class) annotation to my class and @Type(type = "psql_enum") and @Enumerated(EnumType.STRING) for my property and that's it! – darth jemico May 31 '19 at 12:36

0 Answers0