Hello,
I've got some unknown problem with binding value from Spring form select to object referencing this value. Object is Relation class and it has reference to Pet class object in it. When I try to add relation according to created form, Java returns following error:
I'll show some code fragments:Code:org.hibernate.PropertyValueException: not-null property references a null or transient value: org.springframework.samples.petclinic.Relation.pet2
relationForm.jsp
addRelationForm.javaCode:<form:form modelAttribute="relation"> <form:select path="pet2" items="${pets}" multiple="false" /> <form:checkbox path="positive" /> <input type="hidden" name="pet" value="${relation.pet.id}"/> <p class="submit"><input type="submit" value="Add Relation" /></p> </form:form>
Hibernate mapping fileCode:package org.springframework.samples.petclinic.web; // imports @Controller @RequestMapping("/owners/*/pets/{petId}/relations/new") @SessionAttributes("relation") public class AddRelationForm { private final Clinic clinic; @Autowired public AddRelationForm(Clinic clinic) { this.clinic = clinic; } @InitBinder public void setAllowedFields(WebDataBinder dataBinder) { dataBinder.setDisallowedFields("id"); } @RequestMapping(method = RequestMethod.GET) public String setupForm(@PathVariable("petId") int petId, Model model) { Pet pet = this.clinic.loadPet(petId); List<Relation> relations=pet.getRelations(); Collection<Pet> pets=this.clinic.getPets(); pets.remove(pet); for(int i=0; i<relations.size(); i++){ pets.remove(relations.get(i).getPet2()); } Relation relation = new Relation(); relation.setPet(pet); pet.addRelation(relation); model.addAttribute("relation", relation); model.addAttribute("pets", pets); return "pets/relationForm"; } @RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("relation") Relation relation, BindingResult result, SessionStatus status) { new RelationValidator().validate(relation, result); this.clinic.storeRelation(relation); return "redirect:/owners/" + relation.getPet().getOwner().getId(); } }
I don't see any errors here, what is wrong?Code:<class name="Pet" table="pets"> <id name="id" column="id"> <generator class="identity" /> </id> <property name="name" column="name" /> <property name="birthDate" column="birth_date" type="date" /> <many-to-one name="owner" column="owner_id" class="org.springframework.samples.petclinic.Owner" /> <many-to-one name="type" column="type_id" class="org.springframework.samples.petclinic.PetType" /> <set name="visitsInternal" inverse="true" cascade="all" lazy="false"> <key column="pet_id" /> <one-to-many class="org.springframework.samples.petclinic.Visit" /> </set> <set name="relationsInternal" inverse="false" cascade="all" lazy="false"> <key column="pet_id"/> <one-to-many class="org.springframework.samples.petclinic.Relation"/> </set> </class> <class name="Relation" table="relations"> <id name="id" column="id"> <generator class="identity" /> </id> <property name="positive" column="positive" type="boolean" /> <many-to-one name="pet" column="pet_id" class="org.springframework.samples.petclinic.Pet" /> <many-to-one name="pet2" class="org.springframework.samples.petclinic.Pet" column="pet2_id" not-null="true" cascade="all" unique="true"/> </class>


Reply With Quote