Craig,
The reason I use the SessionBean is exactly for what you mention. That
is, I don't want to write the transaction begin/commit within
try/catch/finally as I like to think that session beans are used to
encapsulate my business logic within transaction boundaries. Is this
approach correct?
Please note that I get the "openjpa.jdbc.Schema - Existing column "CODE"
on table "APP.PROMOTION" is incompatible with the same column in the
given schema definition" error message for pretty much every field in my
tables (entities). I just pasted one of them in my previous note to keep
the message short.
My orm.xml file is pretty much empty:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">
</entity-mappings>
My persistence.xml file is as follows (I've tried switching from
transaction-type={JTA | RESOURCE_LOCAL} but from reading some articles,
it sounds like I should be using the latter option, is this correct?)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="MyDataAppJPA" transaction-type="RESOURCE_LOCAL">
<jta-data-source>MyDataAppSource</jta-data-source>
<non-jta-data-source>MyDataAppNoTxSource</non-jta-data-source>
<class>com.data.jpa.Account</class>
<class>com.data.jpa.AccountCustomer</class>
<class>com.data.jpa.Address</class>
<class>com.data.jpa.Promotion</class>
<!-- exclude-unlisted-classes>true</exclude-unlisted-classes -->
<properties>
<property name="openjpa.ConnectionDriverName"
value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="openjpa.ConnectionURL"
value="jdbc:derby://localhost:1527/testdb"/>
<!-- property name="openjpa.TransactionMode" value="managed"/ -->
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
<property name="openjpa.jdbc.DBDictionary" value="derby"/>
<property name="openjpa.jdbc.DBDictionary"
value="org.apache.openjpa.jdbc.sql.DerbyDictionary"/>
<property name="openjpa.jdbc.Schema" value="APP"/>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(SchemaAction='add',ForeignKeys=true)"/>
<!-- property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=false)"/ -->
<property name="openjpa.Sequence" value="table(Table=OPENJPASEQ,
Increment=100)"/>
<!-- property name="openjpa.Log" value="DefaultLevel=TRACE"/ -->
</properties>
</persistence-unit>
</persistence>
Here is my SQL table definition:
CREATE TABLE Promotion (
product_id INTEGER NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
code CHAR(8),
discount SMALLINT NOT NULL,
message VARCHAR(1024),
broadcast CHAR(5)
);
Here is the Promotion class:
package com.data.jpa;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
@Entity
@IdClass(PromotionPK.class)
public class Promotion implements Serializable {
@Id
@Column(name="PRODUCT_ID", insertable=false, updatable=false)
private int productId;
@Id
@Column(name="START_DATE")
private Date startDate;
@Id
@Column(name="END_DATE")
private Date endDate;
private String code;
private short discount;
private String message;
private String broadcast;
private static final long serialVersionUID = 1L;
public Promotion() {
super();
}
// getters and setters follow
}
Benjamin S. Vera-Tudela
SWG Middleware Strategy
Ph: (512)-286-9073 T/L 363-9073
E-Mail: [email protected]
Inactive hide details for Craig L Russell ---06/12/2009 05:11:56 PM---Hi
Benjamin, If you access OpenJPA from your servlet direCraig L Russell
---06/12/2009 05:11:56 PM---Hi Benjamin, If you access OpenJPA from your
servlet directly, then you use the EntityTransaction to begin and commit
transacti
From:
Craig L Russell <[email protected]>
To:
[email protected]
Date:
06/12/2009 05:11 PM
Subject:
Re: Questions about OpenJPA issues
------------------------------------------------------------------------
Hi Benjamin,
If you access OpenJPA from your servlet directly, then you use the
EntityTransaction to begin and commit transactions. But if you delegate
to a SessionBean, the bean's transaction properties begin and commit
transactions without your writing any code.
With regard to the schema mismatch, without seeing the orm metadata and
the table definition it's hard to figure out where the problem is. The
apache servers remove attachments from emails. Can you try pasting the
text of the orm definition and the class definition for the Promotion
class into the message?
Regards,
Craig
On Jun 11, 2009, at 3:06 PM, Benjamin S Vera-Tudela wrote:
I am new to JPA and am having a problem with WAS CE 2.1.1.2
(based on Geronimo) and after two days of researching, I am
unable to identify the issue. Perhaps someone may know what
the problem is by the info provided below. I have included
my persistence.xml and deployment plan files in case someone
wants to take a peek at these.
For background, my app is a simple J2EE app with accounts,
products, and promotions among other related objects. After
successful deployment on WAS CE 2.1.1.2, I can access the
main JSPs and servlet, but upon posting data that is used to
update the JPA entities via a Stateless Session Bean (SSB)
invoked from the servlet, I get the several instances of the
following message for various entity fields:
672 MyDataAppJPA WARN [http-0.0.0.0-8080-1]
openjpa.jdbc.Schema - Existing column "CODE" on table
"APP.PROMOTION" is incompatible with the same column in the
given schema definition. Existing column:
Full Name: PROMOTION.CODE
Type: char
Size: 8
Default: null
Not Null: false
Given column:
Full Name: Promotion.code
Type: varchar
Size: 255
Default: null
Not Null: false
These are followed by the exception:
<openjpa-1.2.1-r2180:4612 nonfatal user error>
org.apache.openjpa.persistence.InvalidStateException: You
cannot access the EntityTransaction when using managed
transactions.
Any idea what may be causing these errors? Any hints are
appreciated.
========================================================================================================================================
For some further background on the app and how the pieces
interact, the app contains:
1) A Web component with a Servlet and JSPs acting as the web
front that interface with the end user
2) An EJB component with a Stateless Session Bean (SSB) used
to handle the transaction boundaries for "complex" business
logic using the JPA entitites
3) A JPA component containing all the Entity Beans for my
application and "Entity Managers" generated with RAD (see
below for an example)
My servlet instantiates the SSB and Entity Manager as follows:
<graycol.gif>Servlet Code:
public class MyDataAppServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
private MyDataAppBeanLocal bean; // Stateless Session Bean
reference
private AccountManager accountMgr = new AccountManager(); //
Entity Manager for the Account JPA entity
...
}
A piece of my SSB code follows:
<graycol.gif>SSB Code:
@Stateless
public class MyDataAppBeanImpl implements MyDataAppBeanLocal {
static java.util.Random rnd = new
java.util.Random(System.currentTimeMillis());
@PersistenceContext(unitName="MyDataAppJPA")
protected EntityManager em;
@Resource SessionContext ctx;
public void createAccount(String id, String password, String
name, String type String address1, String address2, String
city, String state, String zipCode)
throws Exception {
System.out.println(">>>> Creating account transaction");
Account account = new Account();
account.setLoginId(id);
account.setPassword(password);
account.setName(name);
account.setType(type);
Address address = new Address();
address.setAddress1(address1);
address.setAddress2(address2);
address.setCity(city);
address.setState(state);
address.setZipCode(zipCode);
account.setAddress(address);
em.persist(address);
em.persist(account);
}
My servlet may also perform simple updates on JPA entity
fields using the object's "entity manager" (which I actually
created using RAD tooling as I was learning JPA). For
instance, from the sample servlet code above, the accountMgr
can be used on an Account JPA entity to persist a simple
field update such as the account's last login timestamp:
<graycol.gif>Updating JPA with Entity Manager:
try {
account.setLastLogin(new Timestamp(System.currentTimeMillis()));
accountMgr.updateAccount(account);
} ....
The AccountManager looks like this (I have commented out
annotations generated from RAD that are tied to WAS and not
available in WAS CE such as @JPAManager and @Action):
<graycol.gif>Account Manager created with RAD:
//XXX:@JPAManager(targetEntity=com.data.jpa.Account.class)
@SuppressWarnings("unchecked")
public class AccountManager {
private EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("MyDataAppJPA");
return emf.createEntityManager();
}
//XXX:@Action(Action.ACTION_TYPE.UPDATE)
public String updateAccount(Account account) throws Exception {
EntityManager em = getEntityManager();
try {
em.getTransaction().begin();
account = em.merge(account);
em.getTransaction().commit();
} catch (Exception ex) {
try {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} catch (Exception e) {
ex.printStackTrace();
throw e;
}
throw ex;
} finally {
em.close();
}
return "";
}
}
And finally here is persistence.xml and deployment plan for
my JPA and app.
/
(See attached file: persistence.xml)(See attached file:
MyDataApp.xml)/
Benjamin S. Vera-Tudela
SWG Middleware Strategy
Ph: (512)-286-9073 T/L 363-9073
E-Mail: [email protected]_ <mailto:[email protected]>
Craig L Russell
Architect, Sun Java Enterprise System _http://db.apache.org/jdo_
408 276-5638 _mailto:[email protected]_
P.S. A good JDO? O, Gasp!