1

I am using spring mvc with hibernate and JPA. I have a Person class which is inherited by another class called Agent. The mapping is implemented as follows:

@Entity
@Table(name = "Person")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person extends Auditable implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PersonId")
    protected Long id;

    //other variables
    ...
}


@Entity
@PrimaryKeyJoinColumn(name = "PersonId")
public class Agent extends Person implements Serializable {
    //additional agent specific variables go here 
    ...
}

Saving new data is smooth and I have no problem there. however, when I edit data, everything except the id value is bound to the controller method's model attribute. I have verified that the id has been sent along with other items from the browser using chrome's developer tools. but the id field at the controller is always null and as a result the data is not updated. This is what my controller method looks like:

@RequestMapping(value = "register", method = RequestMethod.POST)
public @ResponseBody CustomAjaxResponse saveAgent(ModelMap model, @ModelAttribute("agent") @Valid Agent agent, BindingResult result) {
    ...
}

I suspect the problem is probably with my inheritance mapping because I have other classes inheriting from the Person class and I face a similar problem there as well.

Please help!

Chappex
  • 169
  • 1
  • 2
  • 8

1 Answers1

2

you need a public setter for id.

In cases like this I commonly use a specific dto for the form, and/or implement a conversion service that retrieves the entity via hibernate based on id and then performs a merge.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • I don't see where my problem is regarding @ModelAttribute. I have a form backing object and the form contains the id as a hidden field. As I mentioned in the question, I have verified that the id has been submitted in the post request – Chappex Mar 28 '13 at 13:37
  • @Chappex you are right about modelattribute, I don't use this much actually I probably should, see update – NimChimpsky Mar 28 '13 at 13:43
  • id has a public setter. I know that using a DTO is an alternative but I didn't like the idea of creating a class which ends up to be almost the same as the Agent class – Chappex Mar 28 '13 at 14:00
  • @Chappex what does the validation do ? Is it missing out id in there ? Check your html form element names are correct ? http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args – NimChimpsky Mar 28 '13 at 14:36
  • 1
    @Chappex I use the new sping 3.2 testing features in cases like this it really simplifies and helps sort out what is going wrong. – NimChimpsky Mar 28 '13 at 14:40
  • The validation is done through JSR 303 annotations. Even after removing it, I get the same thing. I think I will give a shot to spring testing features – Chappex Mar 29 '13 at 03:12