Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to work with dependent fields in Spring 3.0?
    primarykey
    data
    text
    <p>I tried to create my first spring mvc application. I'm using architecture: Spring MVC-Service-DAO-Persistence Architecture. I've get this error:</p> <pre><code>Field error in object 'rooms' on field 'roomTypeId': rejected value [7]; codes [typeMismatch.rooms.roomTypeId,typeMismatch.roomTypeId,typeMismatch.mypackage.domain.RoomType,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [rooms.roomTypeId,roomTypeId]; arguments []; default message [roomTypeId]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'mypackage.domain.RoomType' for property 'roomTypeId'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [mypackage.domain.RoomType] for property 'roomTypeId': no matching editors or conversion strategy found] </code></pre> <p>Domain:</p> <pre class="lang-java prettyprint-override"><code>@Entity @Table(name = "rooms") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Rooms.findAll", query = "SELECT r FROM Rooms r")}) public class Rooms implements Serializable { ... @JoinColumn(name = "RoomTypeId", referencedColumnName = "RoomTypeId") @ManyToOne private RoomType roomTypeId; ... public RoomType getRoomTypeId() { return roomTypeId; } public void setRoomTypeId(RoomType roomTypeId) { this.roomTypeId = roomTypeId; } } </code></pre> <p>Controller:</p> <pre class="lang-java prettyprint-override"><code>@RequestMapping(value = "/room/add", method = RequestMethod.POST) public String addRoom(@ModelAttribute("rooms") Rooms room, BindingResult result) { roomService.addRoom(room); return "redirect:/rooms"; } </code></pre> <p>I tried:</p> <pre class="lang-java prettyprint-override"><code>@RequestMapping(value = "/room/add", method = RequestMethod.POST) public String addRoom(@ModelAttribute("rooms") Rooms room, @RequestParam Long roomTypeId, BindingResult result) { room.setRoomTypeId(typeService.getType(roomTypeId.intValue())); roomService.addRoom(room); return "redirect:/rooms"; } </code></pre> <p>jsp:</p> <pre class="lang-xml prettyprint-override"><code>&lt;form:form method="post" action="room/add" commandName="room"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="name"&gt; &lt;spring:message code="label.roomname" /&gt; &lt;/form:label&gt;&lt;/td&gt; &lt;td&gt;&lt;form:input path="name" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="roomTypeId"&gt; &lt;spring:message code="label.typeroom" /&gt; &lt;/form:label&gt; &lt;/td&gt; &lt;td&gt; &lt;form:select path="roomTypeId" name="roomTypeId"&gt; &lt;c:forEach items="${typeList}" var="type"&gt; &lt;form:option value="${type.roomTypeId}" label="${type.name}"/&gt; &lt;/c:forEach&gt; &lt;/form:select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;input type="submit" value="&lt;spring:message code="label.addroom"/&gt;" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form:form&gt; </code></pre> <p>I understand why this shows me. But I do not know how to fix it. Help me fix it or tell me what I'm doing wrong.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload