Home > Enterprise >  How can I fix Spring boot POST API's 500 error?
How can I fix Spring boot POST API's 500 error?

Time:01-30

I created POST API in Spring Boot, but 500 error occurs.

"timestamp": "2023-01-27T16:27:32.609 00:00", "status": 500, "error": "Internal Server Error", "trace": "org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["PRIMARY KEY ON PUBLIC.CATEGORY(CATEGORY_ID) ( /* key:1 */ 1, U&'\\c1fc\\d551\\bab0', 1)"; SQL statement:\ninsert into category (category_id, category_name, site_user_id) values (default, ?, ?)

I want to put data in the 'category' table with 'categoryId', 'category_name', and 'site_user_id' as columns through POST API. It seems to be caused by putting 'siteUser' entity instead of 'site_user_id', but I don't know how to modify the code.

Below is the code I wrote.

Category.java

package com.kakaotrack.choco.linkupapi.category;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.kakaotrack.choco.linkupapi.linkcollection.LinkCollection;
import com.kakaotrack.choco.linkupapi.user.SiteUser;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Entity
@Data
@Table(name = "category")
@NoArgsConstructor
public class Category {

    public Category(String category_name, SiteUser siteUser){
        this.category_name = category_name;
        this.siteUser = siteUser;
    }

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

    private String category_name;

    @OneToMany(mappedBy = "category", cascade = CascadeType.REMOVE)
    @JsonIgnoreProperties({"category"})
    private List<LinkCollection> link_collection_list;

    @ManyToOne
    private SiteUser siteUser;

}

SiteUser.java

package com.kakaotrack.choco.linkupapi.user;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "users")
@NoArgsConstructor
public class SiteUser {

    public SiteUser(String username, String email){
        this.username=username;
        this.email=email;
    }
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(unique = true)
    private String username;

    private String password;

    @Column(unique = true)
    private String email;
}

CategoryService.java

package com.kakaotrack.choco.linkupapi.category;

import com.kakaotrack.choco.linkupapi.linkcollection.LinkCollection;
import com.kakaotrack.choco.linkupapi.user.SiteUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class CategoryService {

    private final CategoryRepository categoryRepository;

    public List<Category> getAll() {return categoryRepository.findAll();}

    public List<Category> getBySiteUser(int id){
        return categoryRepository.findBySiteUserId(id);
    }

    public Category createCategory(String categoryName, SiteUser siteUser){
        Category category = new Category(categoryName, siteUser);
        return categoryRepository.save(category);
    }

    public void deleteByCategoryId(int category_id){categoryRepository.deleteById(category_id);}

}

CategoryController.java

package com.kakaotrack.choco.linkupapi.category;

import com.kakaotrack.choco.linkupapi.linkcollection.LinkCollection;
import com.kakaotrack.choco.linkupapi.linkcollection.LinkCollectionRepository;
import com.kakaotrack.choco.linkupapi.user.SiteUser;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping(value = "/categories")
    public List<Category> getAll() {return categoryService.getAll();}

    @GetMapping(value = "/categories/{id}")
    public List<Category> getBySiteUser(@PathVariable int id) {return categoryService.getBySiteUser(id);}

    @PostMapping(value = "/categories")
    public Category createCategory(String categoryName, SiteUser siteUser){
        Category category = categoryService.createCategory(categoryName, siteUser);
        return category;
    }

    @DeleteMapping(value = "/categories/{category_id}")
    public void deleteCategory(@PathVariable int category_id){ categoryService.deleteByCategoryId(category_id);}

}

DELETE and GET APIs work well.

CodePudding user response:

Try to update SiteUser fields as shown below:

@ManyToOne(optional = true, fetch = FetchType.LAZY)
@JoinColumn(name = "site_user_id", referencedColumnName = "id")
private SiteUser siteUser;

CodePudding user response:

I think the issue is with the category_name. It is not following the standard naming convention. Underscore is used to separate property names in JPA custom methods.

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

NB: Also you have to implement the changes mentioned by Murat. Use optional = false if it is Not Null in DB

  • Related