使用 spring 数据加入三个实体
Join three entities using spring data
我在我的项目中使用 spring 数据,以下是实体:
rr:
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
private String model;
@ManyToOne
@JoinColumn( name = "owner" )
private ffperson;
private String constructor;
//getters and setters
zaa:
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Column( name = "name" )
private String Name;
//getters and setters
gff
@ID
@GeneratedValue( 策略 = GenerationType.IDENTITY )
私人长号;
@Column( name = "lastname", unique = true )
private String lastName;
@Column( name = "firstname", unique = true )
private String firstName;
@Column( name = "birth_date", unique = true )
private Date dateOfBirth;
gfff:
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@ManyToOne
@JoinColumn( name = "dog_id" )
private ffdog;
@ManyToOne
@JoinColumn( name = "person_id" )
private ffperson;
我想用 spring 数据编写一个查询,它可以获取所有拥有自己的狗和汽车的人..任何人都可以帮忙
获取它们的最简单方法 - 更改当前数据模型。
人:
@Entity
public class Person {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Column(name = "lastname")
private String lastName;
@Column(name = "firstname")
private String firstName;
@Column(name = "birth_date")
private Date dateOfBirth;
@MayToMany(fetch = FetchType.EAGER)
@JoinTable(name = "PERSON_DOG",
joinColumns = {@JoinColumn(name = "PERSON_ID")},
inverseJoinColumns = {@JoinColumn(name = "DOG_ID")})
private List<Dog> dogs = new ArrayList<>();
@OneToMany(mappedBy = "person")
private List<Car> cars = new ArrayList<>();
}
狗:
@Entity
public class Dog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private Long id;
@Column(name = "name" )
private String Name;
//getters and setters
}
汽车:
@Entity
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private Long id;
private String model;
@ManyToOne
@JoinColumn(name = "owner")
@JsonIgnore // this will prevent the loop on response
private Person person;
private String constructor;
//getters and setters
}
然后只需查询 Person 即可获得所有关系。
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findAll();
}
另请注意,我是如何创建 JoinTable 的。如果您不会自己管理它,则不需要手动执行(如果您要根据特定条件填充它或者如果您有额外的列,有时需要手动操作)。
你的模型也有一些奇怪的地方,为什么first_name
、last_name
和date_of_birth
是唯一的?许多人可能对这些列有相似的数据。
像这样更新您的实体:
人
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
Set<Dog> dogs = new HashSet<>();
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
Set<Car> cars = new HashSet<>();
...
}
狗
@Entity
@Table(name="dog")
public class Dog {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Column( name = "name" )
private String Name;
@ManyToOne
private Person person;
...
}
汽车
@Entity
@Table(name = "car")
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Person person;
...
}
现在创建一个 PersonRepository
并获取所有有狗和车的人
public interface PersonRepository extends CrudRepository<Person, Long> {
@Query("select p from Person p join fetch p.dogs join fetch p.cars")
List<Person> findAll();
}
注意:使用Join Fetch
to fetch dogs and cars eagerly. Otherwise you may get into n + 1
问题或延迟获取问题。
我在我的项目中使用 spring 数据,以下是实体:
rr:
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
private String model;
@ManyToOne
@JoinColumn( name = "owner" )
private ffperson;
private String constructor;
//getters and setters
zaa:
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Column( name = "name" )
private String Name;
//getters and setters
gff @ID @GeneratedValue( 策略 = GenerationType.IDENTITY ) 私人长号;
@Column( name = "lastname", unique = true )
private String lastName;
@Column( name = "firstname", unique = true )
private String firstName;
@Column( name = "birth_date", unique = true )
private Date dateOfBirth;
gfff:
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@ManyToOne
@JoinColumn( name = "dog_id" )
private ffdog;
@ManyToOne
@JoinColumn( name = "person_id" )
private ffperson;
我想用 spring 数据编写一个查询,它可以获取所有拥有自己的狗和汽车的人..任何人都可以帮忙
获取它们的最简单方法 - 更改当前数据模型。
人:
@Entity
public class Person {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Column(name = "lastname")
private String lastName;
@Column(name = "firstname")
private String firstName;
@Column(name = "birth_date")
private Date dateOfBirth;
@MayToMany(fetch = FetchType.EAGER)
@JoinTable(name = "PERSON_DOG",
joinColumns = {@JoinColumn(name = "PERSON_ID")},
inverseJoinColumns = {@JoinColumn(name = "DOG_ID")})
private List<Dog> dogs = new ArrayList<>();
@OneToMany(mappedBy = "person")
private List<Car> cars = new ArrayList<>();
}
狗:
@Entity
public class Dog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private Long id;
@Column(name = "name" )
private String Name;
//getters and setters
}
汽车:
@Entity
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private Long id;
private String model;
@ManyToOne
@JoinColumn(name = "owner")
@JsonIgnore // this will prevent the loop on response
private Person person;
private String constructor;
//getters and setters
}
然后只需查询 Person 即可获得所有关系。
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findAll();
}
另请注意,我是如何创建 JoinTable 的。如果您不会自己管理它,则不需要手动执行(如果您要根据特定条件填充它或者如果您有额外的列,有时需要手动操作)。
你的模型也有一些奇怪的地方,为什么first_name
、last_name
和date_of_birth
是唯一的?许多人可能对这些列有相似的数据。
像这样更新您的实体:
人
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
Set<Dog> dogs = new HashSet<>();
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
Set<Car> cars = new HashSet<>();
...
}
狗
@Entity
@Table(name="dog")
public class Dog {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Column( name = "name" )
private String Name;
@ManyToOne
private Person person;
...
}
汽车
@Entity
@Table(name = "car")
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Person person;
...
}
现在创建一个 PersonRepository
并获取所有有狗和车的人
public interface PersonRepository extends CrudRepository<Person, Long> {
@Query("select p from Person p join fetch p.dogs join fetch p.cars")
List<Person> findAll();
}
注意:使用Join Fetch
to fetch dogs and cars eagerly. Otherwise you may get into n + 1
问题或延迟获取问题。