我在我的项目中使用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(strategy = GenerationType.IDENTITY) private Long id;
@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;
我想写一个带有弹簧数据的查询,可以用他们自己的狗和汽车取得所有人。可以请任何身体帮助
答案 0 :(得分:0)
获取它们的最简单方法 - 更改当前数据模型。
人:
@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
}
然后只是查询人,你将获得所有关系。
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findAll();
}
另请注意,我是如何创建JoinTable的。如果您不自行管理,则不需要手动执行此操作(如果您在某些条件下填充它或者如果您有其他列,则需要进行管理)。
您的模型中还有一些奇怪的事情,为什么first_name
,last_name
和date_of_birth
是唯一的?许多人可能拥有这些专栏的相似数据。
答案 1 :(得分:0)
像这样更新您的实体:
<强>人强>
@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
热切地抓狗和车。否则,您可能会遇到n + 1
问题或Lazy fetch问题。