Hibernate Relationship Tutorial – @OneToMany and @ManyToOne (Part 17 to 20 )

Good to know you’ve completed Parts 1 to 16! Thumbs up to you! Otherwise, you can see the links below and follow.

Now we’ll continue with Part 17, focusing on inserting new item to the database using the repository.

 

Part 17 – Setup Advanced REST Client

Before you can make  a POST request to the application, you need to have a REST Client (you can’t use the browser!).  For me I use Advanced REST Client. Postman REST Client is ok too.  ARC  is an Add-on to google Chrome. To set it up Advanced REST Client on Google Chrome, follow the steps

Step 1: Follow the link below to setup Advanced REST Client

https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

Step 2: Install it and then launch it. The interface is a shown below

 

Advanced REST Client
Advanced REST Client GUI

 

Part 18: Add New Location

To Insert a new Location we need to make a POST request.

Step 1: Write the AddLocation method in the LocationService. Copy and paste in the LocationService

public void AddLocation(Location location) {
     locationRepository.save(location);
}

 

Step 2: Write the AddLocation method in the LocationController. The method is given below. Copy and paste in the LocationController

 

@PostMapping("/locations/addNew")
public void AddLocation(@RequestBody Location location) {
    locationService.AddLocation(location);
}

 

 

Step 3: Relaunch the application.

Step 4: Make a POST request to the http://localhost:8080/locations/addNew url using Advanced Rest Client. The body of the request is as shown below:

{
    "id": 5,
    "name": "KanysTown"
}

 

Step 5: Now access the http://localhost/locations url to ensure the location is inserted. Then add a couple more locations.

 

Part 19 – Inserting a New User

We would also do the same as we did with adding new Location, but with a little variation. We’ll make some modifications to the User entity and to our data.sql file.

Step 1: Launch the application and access the H2 Console to check  what columns are there in the User table. Notice that there is a column in the User table called location_Id.

Step 2: Open the data.sql file. In the code to insert location, change the location_id to locationid (remove the underscore)

Step 3: Open the User model class. Add a new field named locationid.

Step 4: Add the getters and setters for the new locationid field

Step 5: In the @JoinColumn annotation, change the attribute location_id to locationid.

Step 6: Add new attributes to the @JoinColumn: insertable= false, updatable= false

At the end all the changes you have made is given below:

...
...
@ManyToOne
@JoinColumn(name="locationid", insertable = false, updatable=false)
private Location location;

private Integer locationid;

public Integer getLocationid() {
    return locationid;
}

public void setLocationid(Integer locationid) {
    this.locationid = locationid;
}
...
...

 

Step 7: Open the UserService and add the following code to insert new User

public void UpdateUser(User user) {
	userRepository.save(user);
}

 

Step 8: Open the UserController and add the following code

@PostMapping("/users/addNew")
public void AddUser(@RequestBody User user) {
    userService.AddUser(user);
}

 

Step 10: Launch the application and an try to insert a new user using Advanced REST Client. If it works, thumbs up to you! Otherwise, let me know.

 

Part 20 – Inserting a New Post

Try to make the changes similar to the ones we made to insert a User. Try it and see if you get it, before following the procedure. The procedures are below:

Step 1: Open the data.sql file. Change the post_id to postid for the insert into POST queries.

Step 2: In the Post model, add a new field named postid.

Step 3: Add the getters and setters for the new postid field

Step 4: In the @JoinColumn annotation, change the post_id to postid.

Step 5: Add updatable and insertable attribute and set them to false

At the end, the changes to the  Post model would be as follows:

 

@ManyToOne
@JoinColumn(name="userid", insertable=false, updatable=false)
private User user;

private Integer userid;	

public Integer getUserid() {
    return userid;
}

public void setUserid(Integer userid) {
    this.userid = userid;
}

 

Step 6: Launch the application and make a request using the Advanced REST Client. The Json file is as shown below

{
"id": 4,
"postDate": "2019-09-02T17:42:03.878",
"userid": 2,
"details": "Kindson The Genius"
}

 

Check the H2 Console to ensure that the data inserted correctly.

At this point, you have completed this Part. Thumbs up to you! You are a genius!

Let’s now see how to perform updates on the entities.

User Avatar

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

4 thoughts on “Hibernate Relationship Tutorial – @OneToMany and @ManyToOne (Part 17 to 20 )

Leave a Reply

Your email address will not be published. Required fields are marked *