Spring Boot的Jpa ManytoMany问题 [英] Jpa ManytoMany issue with Spring Boot

查看:120
本文介绍了Spring Boot的Jpa ManytoMany问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个postgres数据库,我试图用Spring Boot做一个简单的REST服务。



个人实体:

  @Entity 
@Table(name =person,schema =persons)
public class Person实现Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
私人整数ID;

@Column(nullable = false)
私有字符串名称;

@Column(nullable = false)
private String email;

@Column
私人整数年龄;

@ManyToOne
@JoinColumn(name =country_id,referencedColumnName =id)
private国家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和setters和String方法重载
}

国家实体:

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

@Column(name =country_name)
私人字符串名称;

@Column(name =country_code)
私人字符串代码;

// getters和setters和String方法重载
}



< Postgres架构如下:



Person Table:

  CREATE TABLE persons.person 

id序列NOT NULL,
名称字符变化(50)NOT NULL,
电子邮件字符变化(40)NOT NULL,
年龄整数,
country_id序列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序列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,产生= {MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
public List< Person> getAllPersons(){
retutn jpaPersonRepository.findAll();
}

任何帮助表示赞赏。

  @ManyToMany 
@JoinTable解决方案


name =persons_countries_residence,schema =persons,
joinColumns = @ JoinColumn(name =person_id,referencedColumnName =id),
inverseJoinColumns = @ JoinColumn(name = country_id,referencedColumnName =id))
私人列表< Country> countriesOfResidence;

架构必须在 @JoinTable 注释。


I have a postgres database and I am trying to make a simple REST service with Spring Boot. I have a problem with jpa ManytoMany relationship.

Person Entity:

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

Country Entity:

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

The postgres schema is the following:

Person Table:

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
 )

Country table:

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

Join table:

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
 )

When i make an HTTP method call, I don't get the Countries of residence.

Service code:

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

Any help appreciated.

解决方案

The solution is this:

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

The schema had to be specified at the @JoinTable annotation as well.

这篇关于Spring Boot的Jpa ManytoMany问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆