Spring MVC-Angularjs : How do I bind object property

1.2k views Asked by At

I dont understand why I am getting this error when i try sumbit my form ,so I have an Invalid property ‘siteesTypeSite[idTypeSite]’ of bean class in my web application ,I have tried to find the solution, but still no luck.

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) {
//      TypeSites test=new TypeSites("test");
//      site.setSiteesTypeSite(typeSites);
//      Set type = new HashSet ();
//      type.add(typeSites);
        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 :

<select required 
ng-model=”sites.siteesTypeSite”
name=”siteesTypeSite”
>
<option ng-repeat=”typesites in page.source” value=”{{typesites}}” >{{typesites.typeSite}}</option>
</select>

and i have this before submit in 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

Edite 1 :

i was trying to use MAP like this , but the same error

@LazyCollection(LazyCollectionOption.FALSE)
    @JsonManagedReference("site-typeSite")
    @OneToMany(mappedBy="siteesTypeSite")
    @MapKey//(name="typeSite")
    public Map<Integer, Sites> getSitees() {
        return sitees;
    }

Edite 2 :

i have edite my siteType class like bellow , but i get the same error :

@Entity
@Table(name = "TypeSites")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="idTypeSite")
public class TypeSites implements java.io.Serializable {

    private static final long       serialVersionUID = 1L;
    private int                     idTypeSite;
    private String                  typeSite;
    //private List<Sites>           sitees = new AutoPopulatingList<Sites>(Sites.class);;
    private Map<Integer, Sites>      sitees;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="idTypeSite")
    public int getIdTypeSite() {
        return idTypeSite;
    }
    public void setIdTypeSite(int idTypeSite) {
        this.idTypeSite = idTypeSite;
    }

    @LazyCollection(LazyCollectionOption.FALSE)
    @JsonManagedReference("site-typeSite")
    @OneToMany(mappedBy = "siteesTypeSite", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @MapKey(name="id")
    public Map<Integer, Sites> getSitees() {
        return sitees;
    }
    public void setSitees(Map<Integer, Sites> sitees) {
        this.sitees = sitees;
    }


@SuppressWarnings("unchecked")
    public TypeSites() {
        this.sitees = MapUtils.lazyMap(new HashMap<Integer,Object>(), new Factory() {

              public Object create() {
                  return LazyList.decorate(new ArrayList<Sites>(), 
                                 FactoryUtils.instantiateFactory(Sites.class));
              }

          });
    }
0

There are 0 answers