简体   繁体   中英

Spring MVC + Hibernate: How to handle form with relation

I have problem with adding/updating records with relations. Could please some advice how it should work?

I have two entities: Question and Category :

public class Question {
    @Id
    @GeneratedValue
    private Long questionId;

    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category")
    private Category category;

and

@Entity
@Table(name = "category")
public class Category {
    @Id
    @GeneratedValue
    private Long categoryId;
    private String name;

I have some list of categories and I would like to add new Question with selected Category. So in my QuestionController I have add method:

@RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add() {
        ModelAndView mav = new ModelAndView("question/add");
        mav.addObject("question", new Question());
        mav.addObject("categoryList", categoryService.getAll());

        return mav;
    }

and form:

<form:form modelAttribute="question" method="POST" >
    Name: <form:input path="name" value="${ques.name}" /> 
    Category: <form:select path="category" items="${categoryList}" />
    <input type="submit" value="Add" />
</form:form>

Everything looks good for now (I can fill question name and select category). But I don't know how add POST method should work

@RequestMapping(value = "/add", method = RequestMethod.POST)
    public String added(@ModelAttribute Question question, BindingResult bindingResult) {

    }

When I try to use above method I have error: Failed to convert property value of type 'java.lang.String' to required type model.Category

I've tried to look for similar problem but I coudln't find anything.. So if someone can help/advice or show link to similar issue I would be grateful!

Cheers!

You need to provide code for Spring that tells it how to convert the string value from the web page back into a Category object. This is done by either:

  1. Adding a PropertyEditor to the DataBinder.

  2. Creating a Converter.

It is a bad practice using hibernate objects to map the form items. There are two solutions

  1. Add another property private transient String categoryString; to the 'Question' class. and map the UI category to this <form:select path="categoryString" items="${categoryList}" /> That way you can avoid the error.

  2. Do not use the hibernate mapping classes for mapping the form items, use POJOs for doing this. and later somewhere in your application map this simple pojo elements onto the hibernate entity.

try changing this line:

<form:select path="category" items="${categoryList}" />

to:

<form:select path="category.categoryId" items="${categoryList}" itemLabel="name" itemValue="categoryId"/>

Then in the added method (post method), retrieve the Category object back from hibernate and set back on question object before saving:

Category selectedCategory = yourHibernateService.getCategoryById(question.getCategory().getCategoryId());
question.setCategory(selectedCategory);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM