7

I'm kinda new to Spring (REST) and I'm building a simple REST webservice. I use a RestController to map HTTP requests. I use this simple method for accepting a POST request:

@RequestMapping(value = "/grade/create", method = RequestMethod.POST)
public ResponseEntity<Grade> createGrade(@RequestBody Grade grade)
{
    dao.createGrade(grade);

    return new ResponseEntity<Grade>(grade, HttpStatus.OK);
}

dao in this case is a class with a @Repository annotation. A request shou;d look like this:

{
    "gradeType": "REGULAR",
    "grade": "10",
    "passed": 1,
    "userId": 1,
    "exam_id": 1,
    "user_id": 3
}

The problem is, that Grade has 2 foreign keys, for user and exam. I want to be able to just pass the ID's of the foreign entities and let Hibernate take care of the rest. However, currently I'm getting this as a response:

{
  "timestamp": 1484758525821,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.dao.InvalidDataAccessResourceUsageException",
  "message": "could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement",
  "path": "/grade/create"
}

How can I solve this? I've heard something about a JpaRepository, can I use that to accomplish this?

My Grade modal is as follows:

@Entity
@Table(name = "grades")
@NamedQueries(value = {
        @NamedQuery(name = "Grade.get", query = "SELECT c FROM Grade c WHERE id = :id"),
        @NamedQuery(name = "Grade.getAll", query = "SELECT c FROM Grade c"),
        @NamedQuery(name = "Grade.getAllByUser", query = "SELECT g FROM Grade g INNER JOIN Exam e ON g.exam.id = e.id INNER JOIN Course c ON e.course.id = c.id WHERE g.user.id = :id"),

})
public class Grade {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

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

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

    @Column(name = "passed")
    private int passed;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "exam_id")
    private Exam exam;

    private int examId;

    private int userId;

    public Grade(int id, String gradeType, String grade, int passed, User user, Exam exam)
    {
        setId(id);
        setGradeType(gradeType);
        setGrade(grade);
        setPassed(passed);
        setUser(user);
        setExam(exam);
    }

    public Grade() {
    }

And my repository...

@Repository
public class GradeDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public List<Grade> getallGrades() {
        return em.createNamedQuery("Grade.getAll", Grade.class)
                .getResultList();
    }

    public List<Grade> getLimitedGradesByUser(int limit, int user_id) {
        return em.createNamedQuery("Grade.getAllByUser", Grade.class)
                .setParameter("id", user_id)
                .setMaxResults(limit)
                .getResultList();
    }

    @Transactional
    public Grade getGrade(int id) {
        return em.createNamedQuery("Grade.get", Grade.class).setParameter("id", id).getSingleResult();
    }

    @Transactional
    public Grade createGrade(Grade grade) {
        grade = em.merge(grade);
        em.persist(grade);

        return grade;
    }
}

Thanks

2
  • Can you please add your repository class as well ? Commented Jan 18, 2017 at 17:02
  • @SrikanthA Of course! Silly of me. I've added it
    – Roemer
    Commented Jan 18, 2017 at 17:07

1 Answer 1

4

How can I solve this? I've heard something about a JpaRepository, can I use that to accomplish this?

Yes, you can do this with JpaRepository. You can start adding an Interface
public interface GradeRepository extends PagingAndSortingRepository<Grade, Long>, same for User and Exam
Then when you POST your Grade in your controller(I would suggest a Service for that)
1. Create a new Grade()
2. Use setters to link your User and Exam by @Autowire-ing your UserRepository and ExamRepository and calling findById
3. Then fetch your other fields from your POST using setters
4. Call .save(grade) from your @Autowired GradeRepository

Please note that User and Exam should already exist in the DB to be able link them

Also I would highly suggest you to never pass direct Entities via HTTP instead use DTOs and in your Grade json never pass the exam id. Try finding the exam by other fields, not the table id (ex: findByName)

I implemented a fully functioning SpringBoot App with JPA and Security, you might have a look https://github.com/hodispk/internship

8
  • 1
    Thanks, this is a massive help already. I tried to Autowire the repo's intro my controller but I'm this issue: Description: Field gradeRepository in HvA.Controllers.GradeController required a bean of type 'HvA.Repositories.GradeRepository' that could not be found. Action: Consider defining a bean of type 'HvA.Repositories.GradeRepository' in your configuration.
    – Roemer
    Commented Jan 19, 2017 at 9:28
  • Make sure you are @ComponentScan("com.pkg.repos")-ing your repos. You might have to @EnableJpaRepository("com.pkg.repos") too. Commented Jan 19, 2017 at 11:57
  • In which file? My main Application.java?
    – Roemer
    Commented Jan 19, 2017 at 12:50
  • Yes on top of your Application class Commented Jan 19, 2017 at 13:21
  • @AlexandruHodis why would you recommend against passing the exam id? If the client wants to POST a new grade, wouldn't they want to do so by passing in the id of the parent exam? (ignore the possibility of having the exam ID be part of the URL)
    – AKrush95
    Commented Aug 20, 2018 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.