Java Reference
In-Depth Information
chapter 17, but with a few differences: a new method (
removeUser()
)on
UserDao
, the
User
object now has a one-to-many relationship with a
Telephone
object, and both of
these classes are marked with
JPA
annotations, as shown in listing 18.1.
Listing 18.1
User
and
Telephone
class definitions
@Entity
@Table(name="users")
public class
User {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long
id;
private
String username;
@Column(name="first_name")
private
String firstName;
@Column(name="last_name")
private
String lastName;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="user_id")
@ForeignKey(name="fk_telephones_users")
private
List<Telephone> telephones =
new
ArrayList<Telephone>();
// getters and setters omitted
}
@Entity
@Table(name="phones")
public class
Telephone {
public static enum
Type {
HOME, OFFICE, MOBILE;
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long
id;
private
String number;
private
Type type;
// getters and setters omitted
}
This chapter's sample application also has a business layer interface (
UserFacade
,
defined in listing 18.2), which in turn deals with
DTOs
(data transfer objects), not
the persistent objects directly. Therefore, we need a
UserDto
class (also defined in
listing 18.2).
Listing 18.2
Business layer interface (
UserFacade
) and transfer object (
UserDto
)
public interface
UserFacade {
UserDto getUserById(
long
id);
}