1

I'm newbie in Java so this question looks so simple. I have a model like:

@Entity(name="website") public class Website { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="websiteId", nullable=false, unique=true) private long websiteId; @ManyToOne @JoinColumn(name = "publisherId") private Publisher publisher; public Website() { } //... all getter and setter.... } 

You see, inside Website class, i have an object of Publisher type:

@Entity(name="publisher") public class Publisher { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="publisherId", nullable=false, unique=true) private long publisherId; private String publisherName; @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name="publisherId") private List<Website> listWebsite; public Publisher() { } //...all getter and setter... } 

OK, now i have a form to submit whole object of Website type. I just created a dropdownlist and let user select publisher: The form:

<form:form action="/LineJavaTest1/website/add" commandName="websiteForm" method="post"> <form:input path="websiteName" size="30" placeholder="Website Name"/> <form:input path="websiteUrl" size="30" placeholder="Website Url"/> <form:select path="publisher" multiple="false" size="1"> <%--<form:options itemValue="publisherId" itemLabel="publisherName"/>--%> <form:option value="NONE" label="--- Select ---" /> <form:options items="${publishers}" itemValue="publisherId" itemLabel="publisherName"/> </form:select> <form:hidden path="websiteId" size="30" placeholder="Website Id"/> <input type="submit" class="btn btn-default" value="Save" /> </form:form> 

You see, i set the path for the form:select tag as "publisher", the itemValue is set to "publisherId". When posting, it post the publisherId (a long type value) to the publisher property of posted object. The validation will be failed because it required Publisher type, not long type.

My question: How do i posting the Website.Publisher.publisherId in the right way?

UPDATE:

My add actions in controller:

@RequestMapping(value = "/add", method = RequestMethod.GET) public String showForm(ModelMap mm, @ModelAttribute("websiteForm") Website websiteForm) { mm.put("websiteForm", new Website()); mm.put("publishers", publisherService.getAll()); return "website/add"; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@ModelAttribute("websiteForm") Website websiteForm, BindingResult result, ModelMap mm) { websiteValidator.validate(websiteForm, result); if (result.hasErrors()) { return "error"; } if (websiteForm.getWebsiteId()>0) { websiteService.edit(websiteForm); }else { websiteService.add(websiteForm); } return "redirect:index"; } 

I have changed the path of select tag from publisher to publisher.publisherId as below suggest from Shantaram Tupe but the form cannot be generated to html. I changed from publisher to publisher.publisherName and everything looks well, the form can be viewed in html, the value post back to server in publisher.publisherName. How can i just post it back in publisher.publisherId? It looks something's wrong in configuration for my publisherId field?

UPDATE 2

My issue is generating HTML form only. I have tried editing the generated html form on browser from something like:

<form.....> <select id="publisher.publisherName" name="publisher.publisherName" size="1">......</select> </form> 

to something like:

<form.....> <select id="publisher.publisherId" name="publisher.publisherId" size="1">......</select> </form> 

And everything work well. Now, How can i generate the form with publisher.publisherId?

LAST UPDATE

Finally, i found it. It works with PublisherName but not PublisherId because PublisherName is string type. The dump item in select tag i used (<form:option value="NONE" label="--- Select ---" />) has the value NONE - that cannot be a long type value. Changed it to 0 and the form generated successfully.

1
  • What are the possible values for publisherId, other than NONE ?, if all are of long type, consider changing NONE, to something else to long type... Commented Jan 22, 2018 at 5:51

1 Answer 1

1

simply change path in <form:select> tag from publisher to publisher.publisherId

Some more things from my side:

  1. You don't need to use @Column(name="websiteId", nullable=false, unique=true)

    • Since You have column name in database same as field name in Entity class
    • Since it is Annotated with @Id it will never null and by default uniqe
  2. You don't need to use @JoinColumn at both side, at @OneToMany side use mappedBy attribute e.g. @OneToMany(mappedBy="publisher")

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your suggest things beside and i marked vote up but changing the path doesn't help. At first, i put the path as you said (publisher.publisherId) but the form even cannot be generated to html. I changed it to publisher and the form generated successfully. I will update my question with the controller function.
hmm.... I may provide more thing. If i change the path to 'publisher.publisherName' the form is generated successfully, the data post back to server ok with the long type value for publisherName - fantastic. However if i change it back to 'publisher.publisherId' the form still cannot be generated.
I have updated the question with a lot things to make it more clearly. Really hope you can help me out of the stuck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.