RJP RJP - 22 days ago 3
Java Question

Accessing form data from POST in JPA embedded table

Im running a Spring MVC application using JPA for persistence. I have a form which has an update button. When this button is clicked its supposed to update a users record in the database. The User table has an embedded table Address in it. I am able to access all the fields in the User table but not the embedded table. Here is my Request mapping

@RequestMapping(value="/user/{userid}",method=RequestMethod.POST)
public String Update(@ModelAttribute("user")User user,@PathVariable("userid") String userid,Model model){


Here is my User.java

@Entity
@Table(name="User")
public class User {
@Id
@Column(name = "userid")
private String id;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
@Column(name="title")
private String title;
@Embedded
private Address address;
@ManyToMany
@JoinTable(name="phone_user", joinColumns={@JoinColumn(name="userid")},
inverseJoinColumns={@JoinColumn(name="phoneid")})
private List<Phone> phones;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}

public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}

public List<Phone> getPhones() {
return phones;
}
public void setPhones(List<Phone> phones) {
this.phones = phones;
}


}
Note the @Embedded tag of Address field.
Here is my Address.java

@Embeddable
public class Address {
String street;
String city;
String state;
String zip;
@Column(name="street")
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Column(name="city")
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name="state")
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Column(name="zip")
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}


}


Here is my JSP which sends the form data

<html>
<head>
<title>Home</title>

</head>
<body>
<h1>
Hello world!
</h1>



<form method="POST" action="/cmpe275/user/${userid}">
<table>
<tr>
<td>Name</td>
<td><input type="text" readonly="readonly" name="userid" value="${id}"/></td>
</tr>
<tr><tr><tr><tr><tr><tr><tr><tr><tr>
<td>First Name</td>
<td><input type="text" name="firstname" value="${firstname}"/></td>
<td>Last Name</td>
<td><input type="text" name="lastname" value="${lastname}"/></td>
</tr>
<tr>
<tr><tr><tr><tr>
<td>Street</td>
<td><input type="text" name="street" value="${street}"/></td>
<td>City</td>
<td><input type="text" name="city" value="${city}"/></td>
<td>State</td>
<td><input type="text" name="state" value="${state}"/></td>
<td>Zip</td>
<td><input type="text" name="zip" value="${zip}"/></td>
</tr>
<tr><tr><tr><tr>
<td><input type="submit" onclick="updateUser();" name="Update" value="Update"/></td>

<td><input type="button" name="Delete" value="Delete"/></td>



</table>
</form>
</body>
<script>
function updateUser(){
console.log("hi");
}

</script>
</html>


I am unable to access the address fields using the getter/setters.
For example - In my handler mapping - To get the first name of the updated profile I can access it using :

user.getFirstName()


But If i want to access the updated Address I do a

user.getAddress.getCity()


I get a null value.
Any idea why?

Answer

I think this

<td><input type="text"  name="street" value="${street}"/></td>
<td>City</td>
<td><input type="text"  name="city" value="${city}"/></td>
<td>State</td>
<td><input type="text"  name="state" value="${state}"/></td>
<td>Zip</td>
<td><input type="text"  name="zip" value="${zip}"/></td>

should be

<td><input type="text"  name="address.street" value="${address.street}"/></td>
<td>City</td>
<td><input type="text"  name="address.city" value="${address.city}"/></td>
<td>State</td>
<td><input type="text"  name="address.state" value="${address.state}"/></td>
<td>Zip</td>
<td><input type="text"  name="address.zip" value="${address.zip}"/></td>

you want the json you can see in the browser console to be structured like your POJOs.

Comments