2

I was in the process of writing a logic to save JSON data in a database with relation many to many (hibernate), for which I have a controller class, domain class, dao class, pom and page.jsp file.

This is the JSON data which I am getting and which I am trying to save in the database

[{"id":1,"vendor":"6456","venedesc":"PHOA TIAN HUI","venupddat":1450320162470},{"id":2,"vendor":"6827","venedesc":"MKS(ENA O) PUTRA , CV","venupddat":145032016243433240}]

//class user

 @Entity
    @Table(name = "users")
    @JsonIgnoreProperties(value = { "role", "enabled", "password" }, ignoreUnknown = true)
    public class User {
        @Id
        @GeneratedValue
        private Integer idUser;     
        private String firstname;
        private String lastname;
        @ManyToMany(fetch = FetchType.EAGER)
        @JoinTable(name = "USER_VENDOR", joinColumns = { @JoinColumn(name = "idUser") }, inverseJoinColumns = {@JoinColumn(name = "ID") })
        private Set<Vendor> vendors = new HashSet<Vendor>();//getter setter

and my class vendor

 @Entity
        public class Vendor {
            @Id@GeneratedValue
            @Column(name = "ID")
            private Integer id;
            @Column(name = "vendor")
            private String vendor;
         //getter setter

controller

@RequestMapping("/tambah") //to get JSON
    public @ResponseBody List<Vendor> getVendor(){
        List<Vendor> list = vendorService.getall();
        return list;
    }

@RequestMapping(value = "/save", method = RequestMethod.POST)//to save
public ModelAndView addUser(@ModelAttribute("user") User user, RedirectAttributes redirectAttributes,Model model) {userService.addUser(user);
    ModelAndView modelAndView = new ModelAndView("redirect:/admin/role/user");return modelAndView;}

Vendor page

<form:form commandName="user" action="${pageContext.request.contextPath }/admin/role/user/save" method="post" cssClass="form-horizontal addModalForm">
        <div class="modal fade" id="addModalForm" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">    
<div><div>
    <label class="col-sm-2 control-label">Vendor:</label>
<form:select id ="vendorJSON"  path="vendors" />

   </div></div>
<button type="submit" ><span class="glyphicon glyphicon-ok-circle"></span> Simpan</button>

    <script type="text/javascript">
        var $select = $('#vendorJSON');
                $.getJSON( "${pageContext.request.contextPath }/admin/role/user/tambah", function( data ) {
                      $select.html('');
                      $.each(data, function(key, val){
                     $select.append('<option id="' + val.id + '">' + val.venedesc + '</option>');})
                    $('#detailVendor').html(data.id);});
        $('#addModalForm').modal();

and my pom

    jackson-mapper-asl.version 1.9.13 
    jackson.version 2.5.4
    spring.version 3.2.8.RELEASE
    hibernate.version 4.2.8.Final

i dont know what happen, because just show HTTP Status 400

danangindra
  • 190
  • 2
  • 9
  • 1
    Look in your server logs: you'll *certainly* find more information than just "HTTP 400". Look here for some troubleshooting tips: [Spring MVC - HTTP status code 400 (Bad Request) for missing field which is defined as being not required](http://stackoverflow.com/questions/17858537/spring-mvc-http-status-code-400-bad-request-for-missing-field-which-is-defin). ALSO: 1) get [Fiddler](http://www.telerik.com/fiddler), and 2) Enable [Hibernate logging](http://javarticles.com/2015/06/enabling-logging-in-hibernate.html) – paulsm4 Feb 25 '16 at 06:52
  • 1
    Looks like you sending your data via GET request, but your conroller expect post request. – Jens Feb 25 '16 at 06:57
  • Not show failure on server logs, just show last query select. and I've been using the GET request but still the same. – danangindra Feb 25 '16 at 07:48
  • 1
    How do you send JSON string? If you send it with jquery, can you apply script? Json from you example does not send `User` data, but two `Vendor`. – Ken Bekov Feb 25 '16 at 09:20

1 Answers1

1

If you get error code 400 it means the HTTP request sent is wrong. No need to look into hibernate or the database, the problem occurs before that.

Check the content of the HTTP POST that is sent by the browser, check also the URL it is sent to, and make sure those are compliant to what is configured in the controller.

kgautron
  • 7,915
  • 9
  • 39
  • 60