keep open the Hibernate session with spring MVC application

440 views Asked by At

I am getting an invalid property exception when I am trying to bind a collection in my model object in my form object. I was trying some trick and now I think that the problem is hibernate session with spring MVC , so in the controller I edited create function so that before my form saves, I try to get session and get list of typesite's but still get the same error:

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]

Is there something wrong with my mapping?

Sites.java mapping

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 mapping :

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
}

controller class :

@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 code :

$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 : Here I a call to another angular controller to get a list of type site on the select

    <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> 

and I have this before submit in the browser :

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

The problem is : I can't submit the nested object , if I change value of select to typesites.typeSite instead of typesites like this

 <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> 

The submit work fine, but I have two inserts : an insert in the typesite table, and an insert in the site table with the new foreign key.

0

There are 0 answers