我是在寫一個邏輯,以在數據庫中保存JSON數據與關係多對多(休眠),爲此,我有一個控制器類,域類的過程中, dao類,pom和page.jsp文件。彈簧MVC +休眠+ JSON post方法保存到數據庫
這是我正在和我試圖在數據庫中保存的JSON數據
[{"id":1,"vendor":"6456","venedesc":"PHOA TIAN HUI","venupddat":1450320162470},{"id":2,"vendor":"6827","venedesc":"MKS(ENA O) PUTRA , CV","venupddat":145032016243433240}]
//類用戶
@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
和我的課廠商
@Entity
public class Vendor {
@[email protected]
@Column(name = "ID")
private Integer id;
@Column(name = "vendor")
private String vendor;
//getter setter
控制器
@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;}
供應商頁面
<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();
和我的聚甲醛
jackson-mapper-asl.version 1.9.13
jackson.version 2.5.4
spring.version 3.2.8.RELEASE
hibernate.version 4.2.8.Final
我不知道發生什麼事,因爲只顯示HTTP狀態400
看着你的服務器日誌:你肯定* *找到更多的信息不僅僅是「HTTP 400」。在這裏尋找一些故障排除提示:Spring MVC的 - HTTP狀態代碼400(錯誤請求)丟失的字段,它被定義爲不需要(http://stackoverflow.com/questions/17858537/spring-mvc-http-status -code-400-壞請求對缺失場 - 這此結果defin)。 ALSO:1)得到[提琴手](http://www.telerik.com/fiddler),和2)使能[Hibernate日誌](http://javarticles.com/2015/06/enabling-logging-in-hibernate .html) – paulsm4
看起來像通過GET請求發送數據,但您的控制器期望發佈請求。 – Jens
不顯示服務器日誌失敗,只顯示最後一個查詢選擇。 和我一直在使用GET請求,但仍然一樣。 – user151968