Chappex
Chappex

Reputation: 169

ID field is null in controller

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!

Upvotes: 1

Views: 883

Answers (1)

NimChimpsky
NimChimpsky

Reputation: 47290

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.

Upvotes: 2

Related Questions