简体   繁体   中英

JPA One-To-Many Relationship Mapping

I am new in Spring Boot and I have a class Student.java and another Project.java. Each student can have multiple projects (the relationship is 1:M). I want to map them, and I have the following:

In Student.java :

@OneToMany(mappedBy="student")
private List of projects;

In Project.java :

@ManyToOne <br>
@JoinColumn(name=student_id)
private Student student; 

I know that @JoinColumn adds a column in the project table in the database, but I don't understand what does mappedBy do? I found this: " mappedBy indicates the entity is the inverse of the relationship. " What does the inverse of the relationship mean?

I tried the code in the database, and when I don't have the mappedBy I get 3 tables in the database (a different table that contains projectId and studentId), but I don't understand how that works. Thanks for the help :)

You can take a look at https://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html#mappedBy() for more details.

The field that owns the relationship. Required unless the relationship is unidirectional.

It tells JPA to go around and find if is there any bean property with name student and use this configuration. In your code, it will find a configuration in Project entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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