简体   繁体   English

Spring Hibernate Mysql:如何获取多对一,多对多,一对多的对象

[英]Spring Hibernate Mysql: how to get object for many-to-one, many-to-many, one-to-many

For the relationship of many-to-one, one-to-many or even many-to-many, how to get an object without the objects included on the other side? 对于多对一,一对多或什至多对多的关系,如何获得一个没有另一侧包含的对象的对象?

Say a group of address and a group of people, the relationship would be many-to-many, what if i just wanne get a "people" without the concerned address? 说一组地址和一群人,这种关系将是多对多的,如果我只是想得到一个没有相关地址的“人”,那该怎么办?

Classroom.java 课堂.java

@Entity
public class Classroom {

public Classroom(){
}

public Classroom(long id, String name) {
    this.id = id;
    this.name = name;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;

@Column(name = "name")
private String name;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "classroom_people", 
        joinColumns = {@JoinColumn(name = "classroom_id", referencedColumnName = "id") }, 
        inverseJoinColumns = {@JoinColumn(name = "people_id", referencedColumnName = "id") })
private List<People> peoples = new ArrayList<>();

... 
// getter() and setter()

} }

People.java People.java

@Entity
public class People{

public People(){
}

public People(long id, String name) {
    this.id = id;
    this.name = name;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private long id;

@Column(name = "name")
private String name;

@ManyToMany(mappedBy = "peoples", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Classroom> classrooms = new ArrayList<>();

...
// getter() and setter()

} }

By using hibernate, the table of Classroom, People and the join table classroom_people have been created automatically. 通过使用休眠,将自动创建Classroom,People表和join表classical_people表。 Until now, everything is fine. 到现在为止,一切都很好。

Then by using spring repository, I tried to fetch a people by passing the name, and return a JSON object. 然后,通过使用spring存储库,我尝试通过传递名称来获取人员,并返回JSON对象。

What I hope to get is just a JSON including people.id and people.name but what I get is a JSON including one people and all the classroom in the Set, which includes all the concerning peoples . 我希望得到的只是一个包含people.idpeople.name的JSON,但是我得到的是一个包含一个人和 Set中所有教室的JSON,其中包括所有相关的peoples

SO I guess the problem is the List and List and what I should do is to create three tables: classroom, people and classroom_people, then store the relationship manually to join table. 所以我想问题是列表和列表,我应该做的是创建三个表:教室,人和教室人,然后手动存储关系以连接表。 Then I can fetch a simple object without problem. 然后,我可以毫无问题地获取一个简单的对象。

AM I right? 我对吗?

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

I use Spring Repository and findAllByName(), so the SQL is generated by spring. 我使用Spring Repository和findAllByName(),因此SQL是由spring生成的。 Just wonder whether this would be the source of problem. 只是想知道这是否会成为问题的根源。

It's easily done with HQL if you keep the relationships bidirectional. 如果保持双向关系,使用HQL可以轻松完成。 Something like: 就像是:

String hql = "select addr.people from " + Address.class.getName() + " addr where addr.name(or some other attribute) = :addressName"
//set parameter "addressName"

EDIT: Now you took a different example, but the above query applies the same, for example getting people from a classroom. 编辑:现在您采取了一个不同的示例,但上面的查询应用相同的,例如从教室里找人。

About your last note, getting a People by name will fetch only People instance, because the @ManyToMany List referencing classrooms is LAZY which means is not populated until you call people.getClassRooms() (if you have the session open it will load the empty list with another query) You don't need to create the junction table yourselves, not for this purpose. 关于你的最后一个音符,得到一个People的名字将只获取People的实例,因为@ManyToMany List引用的教室是LAZY,这意味着未填充,直到调用people.getClassRooms()如果你有开放的会议将加载空列出另一个查询),您不需要自己创建联结表,而不是为此目的。

How do you know that List<ClassRoom> is populated when you fetch a People ? 您如何知道获取People时填充了List<ClassRoom> Because from your mappings it looks OK, unless you're calling people.getClassRooms() 因为从您的映射看起来不错,除非您要调用people.getClassRooms()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM