Reputation: 310
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
Upvotes: 2
Views: 1624
Reputation: 8273
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.
Upvotes: 1