Thursday, July 30, 2009

Creating JPA project in Eclipse

Introduction to Oracle Enterprise Pack for Eclipse 11g JPA Workbench

Dali JPA Tools
An Eclipse Web Tools Platform Sub-Project

Generate custom entities

Custom individual entity

Generated entity class

public class Book implements Serializable {
private static final long serialVersionUID = 1L;

@SequenceGenerator(name="BOOK_BOOKID_GENERATOR", sequenceName="BOOK_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BOOK_BOOKID_GENERATOR")
private long bookId;

private String title;

@Temporal( TemporalType.DATE)
private java.util.Date publishDate;

//bi-directional many-to-one association to Author
@JoinColumn(name="AUTHOR_ID", referencedColumnName="AUTHOR_ID")
private Author author;

public Book() {
public long getBookId() {
return this.bookId;

public void setBookId(long bookId) {
this.bookId = bookId;

public String getTitle() {
return this.title;

public void setTitle(String title) {
this.title = title;

public java.util.Date getPublishDate() {
return this.publishDate;

public void setPublishDate(java.util.Date publishDate) {
this.publishDate = publishDate;

public Author getAuthor() {

public void setAuthor(Author author) { = author;


public class Author implements Serializable {
private static final long serialVersionUID = 1L;

@SequenceGenerator(name="AUTHOR_AUTHORID_GENERATOR", sequenceName="BOOK_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AUTHOR_AUTHORID_GENERATOR")
private long authorId;

private String firstName;

private String lastName;

private String email;

//bi-directional many-to-one association to Book
@OneToMany(mappedBy="author", fetch=FetchType.EAGER)
private java.util.Set<Book> books;

public Author() {
public long getAuthorId() {
return this.authorId;

public void setAuthorId(long authorId) {
this.authorId = authorId;

public String getFirstName() {
return this.firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getLastName() {
return this.lastName;

public void setLastName(String lastName) {
this.lastName = lastName;

public String getEmail() {

public void setEmail(String email) { = email;

public java.util.Set<Book> getBooks() {
return this.books;

public void setBooks(java.util.Set<Book> books) {
this.books = books;


JPA Field detail

Add JPA project to EJB project

Add finder call to Session Bean

private javax.persistence.EntityManager em;

public Author findAuthor(long authorId) {
return em.find(Author.class, authorId);

Add session bean call to servlet
TestSessionBeanRemote remoteBusinessIntf;

Author author = remoteBusinessIntf.findAuthor(Long.parseLong(name));


1 comment:

  1. Hi,

    Thanks for your tip for adding JPA Project in build path of EJB Project. I searched lot but your blog save me.