2016-09-25 111 views
0

我有一個postgres數據庫,我正在嘗試使用Spring Boot做一個簡單的REST服務。我與jpa ManytoMany的關係存在問題。Jpa ManytoMany Spring Boot的許多問題

Person實體:

@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) 
) 

加入表:

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 
) 

當我提出一個HTTP方法調用,我沒有得到居住的國家。

服務代碼:

@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默認情況下會延遲加載關聯,因此您需要啓用急切獲取才能立即運行。

@ManyToMany(fetch = FetchType.EAGER) 
+0

是,國家列表爲空。所有其他領域都有價值。設置'fetch = FetchType.EAGER'沒有解決這個問題。 (在persons.persons_countries_residence表中,我已經爲存在的人和國家設置了兩個現有的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:錯誤:關係「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.