Sunday, February 20, 2011

Create JPA2 Weblogic 10.3.4 project in Eclipse 3.6 (OEPE)

Oracle Enterprise Pack for Eclipse User Guide
http://download.oracle.com/docs/cd/E15315_07/help/oracle.eclipse.tools.common.doc/html/index.html
Object-Relational Mappings: Configuring the Persistence Provider for JPA Projects
http://download.oracle.com/docs/cd/E15315_07/help/oracle.eclipse.tools.orm.doc/html/files/libAndFacetForJPA.html

Introduction to Oracle Enterprise Pack for Eclipse 11g JPA Workbench
By Andrei Cioroianu
http://www.oracle.com/technetwork/articles/cioroianu-eclipse-jpa-084626.html

It is necessary to change PRE_CLASSPATH in WLS10.3.4 to enable JPA2.

1.configure Server Runtime for WLS 10.3.4
http://www.oracle.com/technetwork/developer-tools/eclipse/downloads/index.html

2.create JPA project




3.create test entity tables
CREATE TABLE  "AUTHOR" 
( "ID" NUMBER,
"NAME" VARCHAR2(255),
"INSERT_TIMESTAMP" TIMESTAMP (6) DEFAULT sysdate,
CONSTRAINT "AUTHOR_PK" PRIMARY KEY ("ID") ENABLE
)
/


CREATE OR REPLACE TRIGGER "BI_AUTHOR"
before insert on "AUTHOR"
for each row
begin
select "ID_GEN".nextval into :NEW.ID from dual;
end;

/
ALTER TRIGGER "BI_AUTHOR" ENABLE
/





CREATE TABLE  "BOOK" 
( "AUTHOR_ID" NUMBER NOT NULL ENABLE,
"TITLE" VARCHAR2(256),
"INSERT_TIMESTAMP" TIMESTAMP (6) DEFAULT sysdate,
"COUNTER" NUMBER(*,0),
"ID" NUMBER(*,0) NOT NULL ENABLE,
CONSTRAINT "ID" PRIMARY KEY ("ID") ENABLE,
CONSTRAINT "BOOK_AUTHOR" FOREIGN KEY ("AUTHOR_ID")
REFERENCES "AUTHOR" ("ID") ENABLE
)
/


CREATE OR REPLACE TRIGGER "BOOK_ID"
BEFORE
insert on "BOOK"
for each row
begin3
select "ID_GEN".nextval into :NEW.ID from dual;

end;
/
ALTER TRIGGER "BOOK_ID" ENABLE
/






4.generate JPA entities from tables










package dave;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Set;


/**
* The persistent class for the AUTHOR database table.
*
*/
@Entity
public class Author implements Serializable {

private static final long serialVersionUID = 4224149688123855811L;

@Id
@SequenceGenerator(name="AUTHOR_ID_GENERATOR", sequenceName="ID_GEN")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AUTHOR_ID_GENERATOR")
private long id;

@Column(name="INSERT_TIMESTAMP")
private Timestamp insertTimestamp;

private String name;

//bi-directional many-to-one association to Book
@OneToMany(mappedBy="author")
private Set<Book> books;

public Author() {
}

public long getId() {
return this.id;
}

public void setId(long id) {
this.id = id;
}

public Timestamp getInsertTimestamp() {
return this.insertTimestamp;
}

public void setInsertTimestamp(Timestamp insertTimestamp) {
this.insertTimestamp = insertTimestamp;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

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

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

}

package dave;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
import java.math.BigDecimal;


/**
* The persistent class for the BOOK database table.
*
*/
@Entity
public class Book implements Serializable {

private static final long serialVersionUID = -9138882871442399233L;

@Id
@SequenceGenerator(name="BOOK_ID_GENERATOR", sequenceName="ID_GEN")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BOOK_ID_GENERATOR")
private long id;

private BigDecimal counter;

@Column(name="INSERT_TIMESTAMP")
private Timestamp insertTimestamp;

private String title;

//bi-directional many-to-one association to Author
@ManyToOne
private Author author;

public Book() {
}

public long getId() {
return this.id;
}

public void setId(long id) {
this.id = id;
}

public BigDecimal getCounter() {
return this.counter;
}

public void setCounter(BigDecimal counter) {
this.counter = counter;
}

public Timestamp getInsertTimestamp() {
return this.insertTimestamp;
}

public void setInsertTimestamp(Timestamp insertTimestamp) {
this.insertTimestamp = insertTimestamp;
}

public String getTitle() {
return this.title;
}

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

public Author getAuthor() {
return this.author;
}

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

}




JPA tools Entity editor


edit persistence.xml file
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="books" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/XE</jta-data-source>
<class>dave.Book</class>
<class>dave.Author</class>
<shared-cache-mode>NONE</shared-cache-mode><!-- shared-cache-mode must
come after any class definitions (usually SE only) - the JPA schema is ordered -->
<properties>
<property name="eclipselink.target-server" value="WebLogic_10" />
<property name="eclipselink.target-database" value="Oracle" />
<property name="eclipselink.logging.level" value="FINEST" />
<!-- new for 10.3.4.0 http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging#Server_Logging -->
<property name="eclipselink.logging.logger" value="DefaultLogger" />
<!-- turn off DDL generation after the model is stable -->
<!-- property name="eclipselink.ddl-generation" value="drop-and-create-tables"/ -->
<!-- property name="eclipselink.ddl-generation.output-mode" value="database"/ -->
</properties>
</persistence-unit>
</persistence>




5. create Session EJB to access JPA entities

package dave;

import java.util.Set;

import javax.ejb.Stateless;
import javax.persistence.PersistenceContext;

import weblogic.javaee.CallByReference;

/**
* Session Bean implementation class SessionJPA2EJB
*/
@Stateless(mappedName = "SessionJPA2EJB")
@CallByReference
public class SessionJPA2EJB implements SessionJPA2EJBRemote, SessionJPA2EJBLocal {

/**
* Default constructor.
*/
public SessionJPA2EJB() {
// TODO Auto-generated constructor stub
}

@PersistenceContext
private javax.persistence.EntityManager books;

/* (non-Javadoc)
* @see dave.SessionJPA2Remote#findAuthor(long)
*/
@Override
public Author findAuthor(long authorId) {

Author author = books.find(Author.class, authorId);

System.out.println("author " + author);

//Books
Set<Book> books = author.getBooks();

for(Book book : books){
System.out.println("book " + book);
}

return author;
}

/* (non-Javadoc)
* @see dave.SessionJPA2Remote#findBook(long)
*/
@Override
public Book findBook(long bookId) {

Book book = books.find(Book.class, bookId);

System.out.println("book " + book);

if (book != null) {
Author author = book.getAuthor();

System.out.println("author " + author);
}

return book;
}


}




Initial loading of Persistence Unit in Weblogic
[EL Finest]: 2011-02-20 15:15:06.076--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--Begin predeploying Persistence Unit books; session file:/home/dave/workspace/wls1033/testJPA2/build/classes/_books; state Initial; factoryCount 0
[EL Finest]: 2011-02-20 15:15:06.076--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--property=eclipselink.orm.throw.exceptions; default value=true
[EL Finest]: 2011-02-20 15:15:06.076--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--property=eclipselink.weaving.changetracking; default value=true
[EL Finest]: 2011-02-20 15:15:06.077--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--property=eclipselink.weaving.lazy; default value=true
[EL Finest]: 2011-02-20 15:15:06.077--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--property=eclipselink.weaving.eager; default value=false
[EL Finest]: 2011-02-20 15:15:06.077--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--property=eclipselink.weaving.fetchgroups; default value=true
[EL Finest]: 2011-02-20 15:15:06.077--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--property=eclipselink.weaving.internal; default value=true
[EL Finest]: 2011-02-20 15:15:06.078--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--property=eclipselink.jpa.uppercase-column-names; default value=false
[EL Finer]: 2011-02-20 15:15:06.078--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--Searching for default mapping file in file:/home/dave/workspace/wls1033/testJPA2/build/classes/
[EL Finer]: 2011-02-20 15:15:06.079--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--Searching for default mapping file in file:/home/dave/workspace/wls1033/testJPA2/build/classes/
[EL Config]: 2011-02-20 15:15:06.084--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The access type for the persistent class [class dave.Author] is set to [FIELD].
[EL Config]: 2011-02-20 15:15:06.085--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The target entity (reference) class for the one to many mapping element [field books] is being defaulted to: class dave.Book.
[EL Config]: 2011-02-20 15:15:06.086--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The access type for the persistent class [class dave.Book] is set to [FIELD].
[EL Config]: 2011-02-20 15:15:06.086--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The target entity (reference) class for the many to one mapping element [field author] is being defaulted to: class dave.Author.
[EL Config]: 2011-02-20 15:15:06.087--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The alias name for the entity class [class dave.Author] is being defaulted to: Author.
[EL Config]: 2011-02-20 15:15:06.088--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The table name for entity [class dave.Author] is being defaulted to: AUTHOR.
[EL Config]: 2011-02-20 15:15:06.088--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The column name for element [field id] is being defaulted to: ID.
[EL Config]: 2011-02-20 15:15:06.088--ServerSession(10406216)--Thread(Thread[[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--The column name for element [field name] is being defaulted to: NAME.


Execution of JPA query
[EL Fine]: 2011-02-20 11:25:09.515--ServerSession(14527944)--Connection(16230875)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--SELECT ID, TITLE, COUNTER, INSERT_TIMESTAMP, AUTHOR_ID FROM BOOK WHERE (AUTHOR_ID = ?)
bind => [1]
[EL Finest]: 2011-02-20 11:25:09.516--UnitOfWork(20720277)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--Execute query ReadObjectQuery(name="author" referenceClass=Author )
[EL Finest]: 2011-02-20 11:25:09.517--UnitOfWork(20720277)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--Register the existing object dave.Book@1b09a99
book dave.Book@1b09a99
[EL Finer]: 2011-02-20 11:25:09.517--UnitOfWork(20720277)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--TX beforeCompletion callback, status=STATUS_ACTIVE
[EL Finer]: 2011-02-20 11:25:09.517--UnitOfWork(20720277)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--begin unit of work commit
[EL Finer]: 2011-02-20 11:25:09.518--UnitOfWork(20720277)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--TX afterCompletion callback, status=COMMITTED
[EL Finer]: 2011-02-20 11:25:09.519--UnitOfWork(20720277)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--end unit of work commit
[EL Finer]: 2011-02-20 11:25:09.519--UnitOfWork(20720277)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--release unit of work
[EL Finer]: 2011-02-20 11:25:09.519--ClientSession(24495389)--Thread(Thread[[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--client released
[EL Finer]: 2011-02-20 11:52:37.358--ServerSession(14527944)--Thread(Thread[[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--client acquired
[EL Finer]: 2011-02-20 11:52:37.358--UnitOfWork(953536)--Thread(Thread[[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--TX binding to tx mgr, status=STATUS_ACTIVE
[EL Finest]: 2011-02-20 11:52:37.359--UnitOfWork(953536)--Thread(Thread[[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--Execute query ReadObjectQuery(referenceClass=Author sql="SELECT ID, NAME, INSERT_TIMESTAMP FROM AUTHOR WHERE (ID = ?)")
[EL Finest]: 2011-02-20 11:52:37.359--ServerSession(14527944)--Thread(Thread[[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--reconnecting to external connection pool
[EL Fine]: 2011-02-20 11:52:37.361--ServerSession(14527944)--Connection(20630989)--Thread(Thread[[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--SELECT ID, NAME, INSERT_TIMESTAMP FROM AUTHOR WHERE (ID = ?)
bind => [1]
author dave.Author@19d6908

No comments:

Post a Comment