I have two object model of Muscle and Exercise. For one Muscle there will be many exercise and that is how they are mapped in the project. My Muscle object has a name property, How do I fetch it when I fetch the Exercise object? I have can somehow fetch for the first column in the database that is for the first Exercise does contain the muscle object with the name property but from the second it doesn't have any name property.
This is my Muscle Class
package com.fazla.exercise.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
public class Muscle {
@Id
// @Column(unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
// @JoinColumn(name="muscle")
// @Column(nullable = true)
// cascade is used so that for any changes in parent will change the child too
@OneToMany(mappedBy="muscle", orphanRemoval= false, fetch=FetchType.EAGER)
private List<Exercise> exercises = new ArrayList<>();
public Muscle() {
}
public Muscle(String name, List<Exercise> exercises) {
super();
this.name = name;
this.exercises = exercises;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Exercise> getExercises() {
return exercises;
}
public void setExercises(List<Exercise> exercises) {
this.exercises = exercises;
}
// @Override
// public String toString() {
// return "Muscle [id=" + id + ", name=" + name + ", exercises=" + exercises + "]";
// }
}
This is my Exercise Class
package com.fazla.exercise.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
//import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
//@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
public class Exercise {
@Id
@Column(unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
@Column
private String description;
//As there will be many exercise under one muscle that is why manytoone
//object references an unsaved transient instance - save the transient instance before flushing
//that is why need to add the cascading dependencies
// (cascade = CascadeType.ALL)
// @JoinColumn(name="muscle_id")
// @JsonIgnore
// @JoinTable(name="muscle")
@ManyToOne
@JoinColumn(name="muscle_id")
private Muscle muscle;
public Exercise() {
}
public Exercise(String name, String description, Muscle muscle) {
super();
this.name = name;
this.description = description;
this.muscle = muscle;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Muscle getMuscle() {
return muscle;
}
public void setMuscle(Muscle muscle) {
this.muscle = muscle;
}
// @Override
// public String toString() {
// return "Exercise [id=" + id + ", name=" + name + ", description=" + description + ", muscle=" + muscle + "]";
// }
}
This is how I am getting the JSON response from the API
[
{
"id": 1,
"name": "Dumbbell Curl",
"description": "Take a weight that let's you maintain the form",
"muscle": {
"id": 1,
"name": "Biceps",
"exercises": [
{
"id": 1,
"name": "Dumbbell Curl",
"description": "Take a weight that let's you maintain the form",
"muscle": 1
},
{
"id": 2,
"name": "Barbell Curl",
"description": "Take a weight that let's you maintain the form",
"muscle": 1
}
]
}
},
{
"id": 2,
"name": "Barbell Curl",
"description": "Take a weight that let's you maintain the form",
"muscle": 1
}
]
I want it to response back with the whole muscle object in the 2nd Exercise object rather than only "muscle": 1