Jpa Manyto很多Spring Boot问题

时间:2016-09-25 12:16:56

标签: spring postgresql hibernate jpa

我有一个postgres数据库,我正在尝试使用Spring Boot创建一个简单的REST服务。我对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",
        joinColumns=@JoinColumn(name="person_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(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();
 }

任何帮助表示感谢。

4 个答案:

答案 0 :(得分:1)

也许,您需要在连接表名称中指定模式名称:

@JoinTable(
        name="persons_countries_residence", schema="persons",
        joinColumns=@JoinColumn(name="person_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(name="country_id", referencedColumnName="id"))

答案 1 :(得分:1)

更新您的国家/地区类代码,如:

ObjectInputStream
  

虽然ManyToMany关系总是双向的   对象模型可以选择是否在两者中进行映射   方向,以及它将在哪个方向上映射。如果您选择   要在两个方向上映射关系,那么必须有一个方向   定义为所有者,另一个必须使用mappedBy属性   定义它的映射。这也避免了必须复制JoinTable   这两个地方的信息。

答案 2 :(得分:0)

您的意思是国家/地区列表为空吗?默认情况下,@ManyToMany关联会被延迟加载,您需要启用eager-fetching才能立即开始工作。

@ManyToMany(fetch = FetchType.EAGER)

答案 3 :(得分:0)

解决方案是:

  @ManyToMany
  @JoinTable(
        name="persons_countries_residence", schema = "persons",
        joinColumns=@JoinColumn(name="person_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(name="country_id", referencedColumnName="id"))
private List<Country> countriesOfResidence;

还必须在@JoinTable注释中指定架构。