2016-09-25 9 views
0

私はpostgresデータベースを持っており、Spring Bootで簡単なRESTサービスを作ろうとしています。私はJPNのManytoMany関係に問題があります。Spring BootでJpa ManytoManyの問題

パーソンエンティティ:

@Entity 
@Table(name = "person", schema = "persons") 
public class Person implements Serializable { 
@Id 
@GeneratedValue(strategy= GenerationType.IDENTITY) 
private Integer id; 

@Column(nullable = false) 
private String name; 

@Column(nullable = false) 
private String email; 

@Column 
private Integer age; 

@ManyToOne 
@JoinColumn(name = "country_id", referencedColumnName = "id") 
private Country countryOfBirth; 

@ManyToMany 
@JoinTable(
     name="persons_countries_residence", 
     [email protected](name="person_id", referencedColumnName="id"), 
     [email protected](name="country_id", referencedColumnName="id")) 
private List<Country> countriesOfResidence; 

// getters and setters and to String method overriden 
} 

国エンティティ:

@Entity 
@Table(name = "country", schema = "persons") 
public class Country implements Serializable { 
@Id 
@GeneratedValue(strategy= GenerationType.IDENTITY) 
private Integer id; 

@Column(name = "country_name") 
private String name; 

@Column(name = "country_code") 
private String code; 

// getters and setters and to String method overriden 
} 

Postgresのスキーマは以下の通りです:

パーソン表:

CREATE TABLE persons.person 
(
    id serial NOT NULL, 
    name character varying(50) NOT NULL, 
    email character varying(40) NOT NULL, 
    age integer, 
    country_id serial NOT NULL, 
    CONSTRAINT id PRIMARY KEY (id), 
    CONSTRAINT country_id FOREIGN KEY (id) 
    REFERENCES persons.country (id) MATCH SIMPLE 
    ON UPDATE NO ACTION ON DELETE NO ACTION 
) 

国テーブル:

CREATE TABLE persons.country 
(
    id serial NOT NULL, 
    country_name character varying(45) NOT NULL, 
    country_code character varying(10) NOT NULL, 
    CONSTRAINT country_id PRIMARY KEY (id) 
) 

テーブルに参加:私は、HTTPメソッドの呼び出しを行うと

CREATE TABLE persons.persons_countries_residence 
(
    person_id integer NOT NULL, 
    country_id integer NOT NULL, 
    CONSTRAINT person_country_id PRIMARY KEY (person_id, country_id), 
    CONSTRAINT persons_countries_residence_country_id_fkey FOREIGN KEY (country_id) 
    REFERENCES persons.country (id) MATCH SIMPLE 
    ON UPDATE CASCADE ON DELETE NO ACTION, 
    CONSTRAINT persons_countries_residence_person_id_fkey FOREIGN KEY (person_id) 
    REFERENCES persons.person (id) MATCH SIMPLE 
    ON UPDATE CASCADE ON DELETE CASCADE 
) 

、私は居住地の国を得ることはありません。

サービスコード:

@RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}) 
public List<Person> getAllPersons() { 
    retutn jpaPersonRepository.findAll(); 
} 

すべてのヘルプ高く評価しました。

答えて

0

ソリューションはこれです:

@ManyToMany 
    @JoinTable(
     name="persons_countries_residence", schema = "persons", 
     [email protected](name="person_id", referencedColumnName="id"), 
     [email protected](name="country_id", referencedColumnName="id")) 
private List<Country> countriesOfResidence; 

スキーマは@JoinTableで指定する必要がありました注釈も同様です。

0

国リストがヌルであることを意味しますか? @ManyToManyアソシエーションはデフォルトで遅延ロードされているため、すぐに機能するには、eager-fetchingを有効にする必要があります。

@ManyToMany(fetch = FetchType.EAGER) 
+0

はい、国リストがヌルです。他のすべてのフィールドには値があります。 'fetch = FetchType.EAGER'を設定してもそれを解決できませんでした。 (persons.persons_countries_residenceテーブルには、存在する人と国の2つの既存IDが設定されています)。 – user3590899

0

はたぶん、あなたが参加するテーブル名にスキーマ名を指定する必要があります。

@JoinTable(
     name="persons_countries_residence", schema="persons", 
     [email protected](name="person_id", referencedColumnName="id"), 
     [email protected](name="country_id", referencedColumnName="id")) 
+0

もし私がそれを行うと例外が発生します: 'org.postgresql.util.PSQLException:ERROR:relation" persons_persons_countries_residence "が存在しません。 ' – user3590899

+0

@ user3590899' schema'で答えを更新します。 –

1

更新あなたの国クラスのコードは次のように:

@Entity 
@Table(name = "country", schema = "persons") 
public class Country implements Serializable { 
@Id 
@GeneratedValue(strategy= GenerationType.IDENTITY) 
private Integer id; 

@Column(name = "country_name") 
private String name; 

@Column(name = "country_code") 
private String code; 

@ManyToMany(mappedBy = "countriesOfResidence") 
private List<Person> persons; 

// getters and setters and to String method overriden 
} 

Although a ManyToMany relationship is always bi-directional on the database, the object model can choose if it will be mapped in both directions, and in which direction it will be mapped in. If you choose to map the relationship in both directions, then one direction must be defined as the owner and the other must use the mappedBy attribute to define its mapping. This also avoids having to duplicate the JoinTable information in both places.

関連する問題