C
C
Chvalov2019-10-16 22:58:46
Hibernate
Chvalov, 2019-10-16 22:58:46

Error "No identifier specified for entity" how is it treated?

There is a database:

ER model
3MkQt91.png
Tables:
SQL DDL
CREATE TABLE language (
  "code" char(2) PRIMARY KEY, -- стандарт ISO 639-1
  "laguage" varchar(24) NOT NULL UNIQUE -- Название на локальном (родном) языке
);

-- Основные языки сайта
INSERT INTO language VALUES
  ('en', 'English'),
  ('ru', 'Русский'),
  ('uk', 'Українська');

CREATE TABLE country (
  "code"      smallint PRIMARY KEY, -- ISO 3166-1 numeric
  "locality"  varchar(64), -- название страны (на родном языке страны)
  "alpha_2"   char(2) NOT NULL UNIQUE, -- ISO 3166-1 alpha-2
  "alpha_3"   char(3) NOT NULL UNIQUE -- ISO 3166-1 alpha-3
);

CREATE TABLE country_translate (
  "language_code" char(2) REFERENCES language,
  "country_code" smallint REFERENCES country,
  "translate" varchar NOT NULL
);

Entity implementation in Spring:
language
@Data @Entity @Table
public class Language {
    @Id
    @Column(columnDefinition = "CHAR(2)")
    private String code; // Код языка по стандарту ISO 639-1

    @Column(unique = true, nullable = false, length = 24)
    private String laguage; // Локальное название языка
}
Country
@Data @Entity @Table
public class Country {
    @Id
    @Column(columnDefinition = "smallint")
    private int code; // ISO 3166-1 numeric

    @Column(nullable = false, unique = true, length = 64)
    private String locality; // Локальное название страны (на родном языке страны)

    @Column(columnDefinition = "char(2)", unique = true, nullable = false)
    private String codeAlpha2; // ISO 3166-1 alpha-2

    @Column(columnDefinition = "char(3)", unique = true, nullable = false)
    private String codeAlpha3; // ISO 3166-1 alpha-3

    @OneToMany(mappedBy = "country")
    private Set<CountryTranslate> translate = new HashSet<>();
}
countrytranslate
@Data @Entity @Table
public class CountryTranslate {

    @ManyToOne
    private Language language;

    @ManyToOne
    // @JsonIgnoreProperties("translate")
    private Country country;

    @Column(nullable = false)
    private String translate;

}

I googled the annotations @IdClass, but honestly I didn’t understand anything because there @EmbeddedIdis @MapsIdno sensible material in Russian

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yerlan Ibraev, 2019-10-17
@Chvalov

It was necessary to make a composite key.

@Data @Entity @Table
public class Country {
    @Id
    @Column(columnDefinition = "smallint")
    private int code; // ISO 3166-1 numeric
    @Column(nullable = false, unique = true, length = 64)
    private String locality; // Локальное название страны (на родном языке страны)
    @Column(columnDefinition = "char(2)", unique = true, nullable = false)
    private String codeAlpha2; // ISO 3166-1 alpha-2
    @Column(columnDefinition = "char(3)", unique = true, nullable = false)
    private String codeAlpha3; // ISO 3166-1 alpha-3
    @OneToMany(mappedBy = "countryTranslatePK.country")
    private Set<CountryTranslate> translate = new HashSet<>();
}
...
@Data @Entity @Table
public class Language {
    @Id
    @Column(columnDefinition = "CHAR(2)")
    private String code; // Код языка по стандарту ISO 639-1
    @Column(unique = true, nullable = false, length = 24)
    private String laguage; // Локальное название языка
}
...
@Data @Entity @Table
public class CountryTranslate {
    @EmbeddedId
    private CountryTranslatePK countryTranslatePK;
    @Column(nullable = false)
    private String translate;
}
...
@Data @Embeddable
public class CountryTranslatePK implements Serializable {
    @ManyToOne @JoinColumn(name="language_code")
    private Language language;
    @ManyToOne @JoinColumn(name="country_code")
    private Country country;
}

D
Dinar Zaripov, 2019-10-17
@Donquih0te

CountryTranslate is missing a field with an annotation @Id.
Everyone @Entityshould have it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question