当我尝试在我的表单对象中的模型对象中绑定一个集合时,我得到一个invalid property exception
。我正在尝试一些技巧,现在我认为问题是使用spring MVC的hibernate会话,所以在控制器中我编辑了创建函数,以便在我的表单保存之前,我尝试获取会话并获取typesite的列表但仍然得到同样的错误:
org.springframework.beans.InvalidPropertyException: Invalid property ‘siteesTypeSite[idTypeSite]’ of bean class [model.Sites]:
Property referenced in indexed property path ‘siteesTypeSite[idTypeSite]’ is neither an array nor a List nor a Map; returned value was [2]
我的地图有问题吗?
Sites.java映射
public class Sites implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@JsonBackReference("site-typeSite")
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToOne
@JoinColumn(name = “idTypeSite”)
private TypeSites siteesTypeSite;
Getter/setters
}
TypeSites.java映射:
public class TypeSites implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="idTypeSite")
private int idTypeSite;
private String typeSite;
@LazyCollection(LazyCollectionOption.FALSE)
@JsonManagedReference("site-typeSite")
@OneToMany(mappedBy = “siteesTypeSite”,fetch = FetchType.LAZY)
// private Set<Sites> sitees= new HashSet<Sites>(0);
private List<Sites> sitees = new AutoPopulatingList<Sites>(Sites.class);
Getter/setters
}
控制器类:
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> create(
@ModelAttribute("site") @Valid Sites site,
@ModelAttribute("typeSites") TypeSites typeSites,
@RequestParam(required = false) String searchFor,
@RequestParam(required = false,
defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
Locale locale) {
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = attr.getRequest().getSession();
TypeSites existingTypeSites = (TypeSites) ((Session) session).get(TypeSites.class, site.getSiteesTypeSite().getIdTypeSite());
site.setSiteesTypeSite(existingTypeSites);
siteService.save(site);
}
Angularjs代码:
$scope.createObject = function (newObjectForm) {
if (!newObjectForm.$valid) {
$scope.displayValidationError = true;
return;
}
$scope.lastAction = ‘create';
var url = $scope.url;
var config = {headers: {‘Content-Type': ‘application/x-www-form-urlencoded; charset=UTF-8′}};
$scope.addSearchParametersIfNeeded(config, false);
$scope.startDialogAjaxRequest();
$scope.sites.siteesTypeSite =JSON.parse($scope.sites.siteesTypeSite);
$http.post(url, $.param($scope.sites), config)
.success(function (data) {
$scope.finishAjaxCallOnSuccess(data, “#addObjectsModal”, false);
})
.error(function(data, status, headers, config) {
$scope.handleErrorInDialogs(status);
});
};
JSP:在这里我调用另一个角度控制器来获取选择
上的类型站点列表 <div ng-controller="siteTypesiteController">
<select required
ng-model="sites.siteesTypeSite"
name="siteesTypeSite"
ng-options="typesites as typesites.typeSite for typesites in page.source "
>
<option value="">-- Select Type site --</option>
</select><br>
我在浏览器中提交之前已经有了这个:
Object {codeOracle: "test", codeGSM: "test", area: "test", siteesTypeSite: Object}
area: "test"
codeGSM: "test"
codeOracle: "test"
siteesTypeSite: Object
idTypeSite: 2
sitees: Array[0]
typeSite: "Public"
__proto__: Object
__proto__: Object
问题是:如果我将select的值更改为typesites.typeSite
而不是typesites
,则我无法提交嵌套对象
<div ng-controller="siteTypesiteController">
<select required
ng-model="sites.siteesTypeSite"
name="siteesTypeSite"
ng-options="typesites.typeSite as typesites.typeSite for typesites in page.source "
>
<option value="">-- Select Type site --</option>
</select><br>
提交工作正常,但我有两个插入:typesite
表中的插入,以及带有新外键的site
表中的插入。