Thursday, July 30, 2009

Creating JPA project in Eclipse

Introduction to Oracle Enterprise Pack for Eclipse 11g JPA Workbench


http://www.oracle.com/technetwork/articles/cioroianu-eclipse-jpa-084626.html


Dali JPA Tools
An Eclipse Web Tools Platform Sub-Project
http://eclipse.org/webtools/dali/main.php

Generate custom entities



Custom individual entity




Generated entity class

@Entity()
public class Book implements Serializable {
private static final long serialVersionUID = 1L;

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

private String title;

@Temporal( TemporalType.DATE)
@Column(name="PUBLISH_DATE")
private java.util.Date publishDate;

//bi-directional many-to-one association to Author
@ManyToOne(fetch=FetchType.EAGER)
@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() {
return this.author;
}

public void setAuthor(Author author) {
this.author = author;
}

}


@Entity()
public class Author implements Serializable {
private static final long serialVersionUID = 1L;

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

@Column(name="FIRST_NAME")
private String firstName;

@Column(name="LAST_NAME")
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() {
return this.email;
}

public void setEmail(String email) {
this.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

    @PersistenceContext
private javax.persistence.EntityManager em;

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


Add session bean call to servlet
 @EJB
TestSessionBeanRemote remoteBusinessIntf;

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

response.getWriter().write(author.getLastName());

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.

    Regards,

    ReplyDelete