0

I am still working on my very first solo spring boot project. It is suppose to be a Rest API using the MariaDB example database Nation. There is the country_languages table which receives two foreign keys that also are the primary keys and has another regular field. First foreign key is the id from countries table and the second one is the id from languages table. When I use the save() method in order to create a new tuple I get this error:

org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class me.givo.nationdbapiproject.model.CountryLanguages. Expected: class me.givo.nationdbapiproject.model.CountryLanguagesId, got class java.lang.Integer; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class me.givo.nationdbapiproject.model.CountryLanguages. Expected: class me.givo.nationdbapiproject.model.CountryLanguagesId, got class java.lang.Integer 

This is the country_languages table from the MariaDB example:

create table country_languages( country_id int, language_id int, official boolean not null, primary key (country_id, language_id), foreign key(country_id) references countries(country_id), foreign key(language_id) references languages(language_id) ); 

I am using an @Embeddable class CountryLanguagesId in order to make a composite key as I found in this reference.

@Embeddable public class CountryLanguagesId implements Serializable { @Column(name = "country_id") private Integer countryId; @Column(name = "language_id") private Integer languageId; public CountryLanguagesId() { } public CountryLanguagesId(Integer countryId, Integer languageId) { this.countryId = countryId; this.languageId = languageId; } // + getters and setters 

After that I created the entity for the country_languages table and its repository:

@Entity @Table(name = "country_languages") public class CountryLanguages { @EmbeddedId CountryLanguagesId countryLanguagesId = new CountryLanguagesId(); @ManyToOne(fetch = FetchType.LAZY, optional = false) @MapsId("countryId") @JoinColumn(name = "country_id") private Countries countries; @ManyToOne(fetch = FetchType.LAZY, optional = false) @MapsId("languageId") @JoinColumn(name = "language_id") private Languages languages; @Column(name = "official", length = 1, nullable = false) private Integer official; public CountryLanguages() { } public CountryLanguages(Countries country, Languages language, Integer official) { this.countries = country; this.languages = language; this.official = official; } // + getters and setters 
@Repository public interface ICountryLanguagesJpaRepository extends JpaRepository<CountryLanguages, Integer> { } 

There are the countries and languages entities:

@Entity @Table(name = "countries") public class Countries { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "country_id", length = 11, nullable = false) private Integer countryId; @Column(name = "name", length = 50, nullable = true) private String name; @Column(name = "area", nullable = false) private BigDecimal area; @Column(name = "national_day", nullable = true) private java.sql.Date nationalDay; @Column(name = "country_code2", length = 2, nullable = false) private String countryCode2; @Column(name = "country_code3", length = 3, nullable = false) private String countryCode3; @OneToMany(mappedBy = "countries", cascade = CascadeType.ALL) private Set<CountryLanguages> countryLanguages; public Countries() { } public Countries(String name, BigDecimal area, Date nationalDay, String countryCode2, String countryCode3) { this.name = name; this.area = area; this.nationalDay = nationalDay; this.countryCode2 = countryCode2; this.countryCode3 = countryCode3; } 
@Entity @Table(name = "languages") public class Languages { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "language_id", length = 11, nullable = false) private Integer languageId; @Column(name = "language", length = 50, nullable = false) private String language; @OneToMany(mappedBy = "languages", cascade = CascadeType.ALL) private Set<CountryLanguages> countryLanguages; public Languages() { } public Languages(String language) { this.language = language; } public Integer getLanguageId() { return languageId; } 

These are the entries I do when get the error:

@DataJpaTest @AutoConfigureTestDatabase(replace = Replace.NONE) public class ICountryLanguagesJpaRepositoryTest { @Autowired private ICountriesJpaRepository countries; @Autowired private ILanguagesJpaRepository languages; @Autowired private ICountryLanguagesJpaRepository repository; @Test public void shouldSaveAndRemoveContinents() { Countries patu = new Countries("Patu", new BigDecimal(67822.34), new Date(12321233232L), "PU", "PTU"); countries.save(patu); Languages patuano = new Languages("Patuano"); languages.save(patuano); CountryLanguages pLanguages = new CountryLanguages(patu, patuano, 0); repository.save(pLanguages); assertEquals(1, repository.findAll().size()); System.out.println(repository.findAll()); repository.deleteById(1); assertEquals(0, repository.findAll().size()); } 

I am doing this using a H2 database. Here is the complete debug console output. Sorry but cant paste it here due characters limitation.

Thanks!

1 Answer 1

1

Your repository definition is wrong. You should specify the embeddable type as primary key type:

@Repository public interface ICountryLanguagesJpaRepository extends JpaRepository<CountryLanguages, CountryLanguagesId> { } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.