JPA and EJB - OneToMany problem
I try to build simple Java EE application that uses JPA + EJB3 and Stripes. It's a little address book. I'm using 2 JPA entities, Person and Email. Every person can have more emails, but each email can only belong to one person. My entities looks like this (with default setters and getters):
Person.java:
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade = CascadeType.REMOVE, mappedBy = "person")
private Collection<Email> emails; ... }
Email.java:
@Entity
public class Email implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String note;
private String address;
@ManyToOne
private Person person; ... }
But when I try to show the list of every person and all their emails, I can't get emails to show. This is how I'm trying to print them:
<c:forEach items="${actionBean.people}" var="person">
<tr>
<td><c:out value="${person.name}"/></td>
<td>
<c:forEach items="${person.email}" var="email">
<c:out value="${email.address}"/><c:out value="${email.note}"/>
</c:forEach>
</td>
</tr>
</c:forEach>
A开发者_如何学Pythonny idea, how to solve this?
Does your Person
class have a getEmail()
method or a getEmails()
method? Given the variable name of emails
I would expect a Person.getEmails()
given the attribute name, but your JSTL is looking for getEmail()
.
If that's not the problem, I believe you might need to add a @JoinColumn
annotation to your Email
class, and add a column to the email table that refers back to the person id. This is how I've done all my ManyToOne annotations. An example ManyToOne can be found here.
精彩评论