Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » EclipseLink as alternative to Hibernate in Swing App
EclipseLink as alternative to Hibernate in Swing App [message #384245] Fri, 12 December 2008 00:55 Go to next message
David J Rericha is currently offline David J RerichaFriend
Messages: 13
Registered: July 2009
Junior Member
Dear members,

I have developed a production Swing app in the medical field using
Hibernate as the persistence layer. Even though Hibernate is geared
towards web-apps, it has been very adaptable to the 2-tier world with one
exception: lazy-loading. Since lazy-loading is unsupported outside a
transaction in Hibernate, I have implemented a framework that always
maintains an open transaction with the current session. However, these
long transactions still do not stave off seemingly random
LazyInitializationException's when performing lazy-loads.

My question is will I run into the same problem with EclipseLink? From
what I garnered from the documentation, fetching in EclipseLink does not
need to be wrapped in an EntityManager transaction(1). Also, any
recommended implementation-patterns of EclipseLink in a GUI app would be
much appreciated.

Thanks,


David

1 - Das, Aditi. "Understanding JPA, Part 1: The object-oriented paradigm
of data persistence." JavaWorld.com, 01/17/08:

"EntityManager manages entities; it is responsible for their addition,
updating, and deletion. You can find an entity without a transaction;
however, add, update, and delete operations need to be within a
transaction."
Re: EclipseLink as alternative to Hibernate in Swing App [message #384247 is a reply to message #384245] Fri, 12 December 2008 06:39 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
Eclipselink works perfectly in two tier environments. My setup currently does all changes outside a transaction in memory and when the save button is pressed I merges all entites that were changes and everything is persisted. I did have to tweak some behavior in an ExtendedEntityManager and that has to do with what happens when exceptions occur during save; when that happens you need to clear the EM in order to get rid of the entities that have already been persisted (and have their version increased), but by doing so you also lose objects that are supposed to be removed. I can give you my two classes that handle these scenario's.

Tom


David J Rericha wrote:
> Dear members,
>
> I have developed a production Swing app in the medical field using
> Hibernate as the persistence layer. Even though Hibernate is geared
> towards web-apps, it has been very adaptable to the 2-tier world with
> one exception: lazy-loading. Since lazy-loading is unsupported outside
> a transaction in Hibernate, I have implemented a framework that always
> maintains an open transaction with the current session. However, these
> long transactions still do not stave off seemingly random
> LazyInitializationException's when performing lazy-loads.
>
> My question is will I run into the same problem with EclipseLink? From
> what I garnered from the documentation, fetching in EclipseLink does not
> need to be wrapped in an EntityManager transaction(1). Also, any
> recommended implementation-patterns of EclipseLink in a GUI app would be
> much appreciated.
>
> Thanks,
>
>
> David
>
> 1 - Das, Aditi. "Understanding JPA, Part 1: The object-oriented paradigm
> of data persistence." JavaWorld.com, 01/17/08:
>
> "EntityManager manages entities; it is responsible for their addition,
> updating, and deletion. You can find an entity without a transaction;
> however, add, update, and delete operations need to be within a
> transaction."
Re: EclipseLink as alternative to Hibernate in Swing App [message #384256 is a reply to message #384247] Fri, 12 December 2008 19:07 Go to previous messageGo to next message
David J Rericha is currently offline David J RerichaFriend
Messages: 13
Registered: July 2009
Junior Member
Tom,

Thanks for the response. It is good to hear of your success with
EclipseLink in 2-tier. I created a test app in Eclipse-Ganymede using
Dali - just a Person entity associated many-to-one with an Address entity.
I wanted to test lazy-loading. However, even though I specified "Lazy"
for the address entity in the orm.xml, the entity is loaded anyway. Is
there a problem with lazy-loading or am I missing something? The
Server-Client session is used. I know I need a DatabaseSession, but I am
not sure how to configure that. Maybe that is the problem. My source is
below:

EclipseLink, version: Eclipse Persistence Services - 1.0.2 (Build 20081024)
JavaSE 1.6.0_11
Eclipse Platform 3.4.1
WST (Dali) 3.0.3

Models:
public class Address implements Serializable
{
private long id;
private String city;
private String country;
private String startOrProvince;
private String postalCode;
private String street;
private static final long serialVersionUID = 1L;

//Getters & setters...
}

public class Person implements Serializable
{
private long id;
private String firstName;
private String lastName;
private Address address;
private static final long serialVersionUID = 1L;

public String toString()
{
return firstName + " " + lastName;
}

//Getters & setters...
}

Main:
public class TryQuickStart
{

public static void main( String[] args )
{
Map properties = new HashMap();

EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"quickstart", properties );

EntityManager em = emf.createEntityManager();

em.getTransaction().begin();
Person david = (Person)em.createQuery(
"select p from Person p where p.firstName = 'David'"
).getSingleResult();
System.out.println( david );
em.getTransaction().commit();

em.close();
}
}

Here is the log:
EL Config]: 2008.12.12
12:50:04.813--ServerSession(17386824)--Connection(9613729)-- Thread(Thread[main,5,main])--Connected:
jdbc:postgresql://localhost/quickstart
User: postgres
Database: PostgreSQL Version: 8.1.8
Driver: PostgreSQL Native Driver Version: PostgreSQL 8.3 JDBC4 with SSL
(build 604)
[EL Finest]: 2008.12.12
12:50:04.816--ServerSession(17386824)--Thread(Thread[main,5, main])--sequencing
connected, state is Preallocation_NoTransaction_State
[EL Finest]: 2008.12.12
12:50:04.817--ServerSession(17386824)--Thread(Thread[main,5, main])--sequence
address_seq: preallocation size 1
[EL Finest]: 2008.12.12
12:50:04.817--ServerSession(17386824)--Thread(Thread[main,5, main])--sequence
person_seq: preallocation size 1
[EL Info]: 2008.12.12
12:50:04.858--ServerSession(17386824)--Thread(Thread[main,5, main])--file:/home/daver/workspace/quickstart/build/classes/ -quickstart
login successful
[EL Finest]: 2008.12.12
12:50:04.858--ServerSession(17386824)--Thread(Thread[main,5, main])--End
deploying Persistence Unit quickstart; state Deployed; factoryCount 1
[EL Finer]: 2008.12.12
12:50:04.877--ServerSession(17386824)--Thread(Thread[main,5, main])--client
acquired
[EL Finest]: 2008.12.12
12:50:05.091--UnitOfWork(23978087)--Thread(Thread[main,5,mai n])--Execute
query ReadAllQuery(quickstart.demo.model.Person)
[EL Fine]: 2008.12.12
12:50:05.094--ServerSession(17386824)--Connection(25218858)- -Thread(Thread[main,5,main])--SELECT
ID, FIRSTNAME, LASTNAME, address_id FROM person WHERE (FIRSTNAME = ?)
bind => [David]
[EL Finest]: 2008.12.12
12:50:05.137--ServerSession(17386824)--Thread(Thread[main,5, main])--Execute
query ReadObjectQuery(quickstart.demo.model.Address)
[EL Fine]: 2008.12.12
12:50:05.138--ServerSession(17386824)--Connection(9820728)-- Thread(Thread[main,5,main])--SELECT
address_id, province, CITY, COUNTRY, p_code, STREET FROM address WHERE
(address_id = ?)
bind => [2]
[EL Finest]: 2008.12.12
12:50:05.142--UnitOfWork(23978087)--Thread(Thread[main,5,mai n])--Register
the existing object David Rericha
[EL Finest]: 2008.12.12
12:50:05.148--UnitOfWork(23978087)--Thread(Thread[main,5,mai n])--Register
the existing object quickstart.demo.model.Address@132ae7
David Rericha
[EL Finer]: 2008.12.12
12:50:05.150--UnitOfWork(23978087)--Thread(Thread[main,5,mai n])--release
unit of work
[EL Finer]: 2008.12.12
12:50:05.150--ClientSession(21132756)--Thread(Thread[main,5, main])--client
released

orm.xml:

<?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 class="quickstart.demo.model.Address" access="PROPERTY">
<table name="address">
</table>
<attributes>
<id name="id">
<column name="address_id"/>
<generated-value generator="address_seq" strategy="SEQUENCE"/>
<sequence-generator name="address_seq" allocation-size="1"
initial-value="1"/>
</id>
<basic name="startOrProvince">
<column name="province"/>
</basic>
<basic name="city">
</basic>
<basic name="country">
</basic>
<basic name="postalCode">
<column name="p_code"/>
</basic>
<basic name="street">
</basic>
</attributes>
</entity>
<entity class="quickstart.demo.model.Person" access="PROPERTY">
<table name="person">
</table>
<attributes>
<id name="id">
<generated-value generator="person_seq" strategy="SEQUENCE"/>
<sequence-generator name="person_seq" allocation-size="1"
initial-value="1"/>
</id>
<basic name="firstName">
</basic>
<basic name="lastName">
</basic>
<many-to-one name="address" fetch="LAZY">
<join-column name="address_id" referenced-column-name="address_id"/>
<cascade>
<cascade-all />
</cascade>
</many-to-one>
</attributes>
</entity>
</entity-mappings>

persistence.xml:
<?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="quickstart" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property name="eclipselink.jdbc.url"
value="jdbc:postgresql://localhost/quickstart"/>
<property name="eclipselink.jdbc.user" value="postgres"/>
<property name="eclipselink.jdbc.password" value=""/>
<property name="eclipselink.jdbc.driver" value="org.postgresql.Driver"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.target-database" value="PostgreSQL"/>
</properties>
</persistence-unit>
</persistence>
Re: EclipseLink as alternative to Hibernate in Swing App [message #384348 is a reply to message #384256] Fri, 12 December 2008 20:46 Go to previous messageGo to next message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
When running EclipseLink JPA outside of the container you do need to use
the -javaagent:eclipselink.jar on the command line when you launch the
application. This enables the byte-code weaving necessary for 1:1 and M:1
lazy loading.

Our JPA implementation only uses the Server/Client session under the
covers. For 2-tier applications you can simply configure the internal
connection pool to use 1 read and 1 write connection.

Doug
Re: EclipseLink as alternative to Hibernate in Swing App [message #384350 is a reply to message #384348] Fri, 12 December 2008 21:45 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
Or you use compile time weaving. I'm not sure if this really makes a difference when the application starts (I build the jar, but I'm not using it), but runtime weaving is noticable there.
Re: EclipseLink as alternative to Hibernate in Swing App [message #385171 is a reply to message #384247] Fri, 02 January 2009 20:35 Go to previous messageGo to next message
David J Rericha is currently offline David J RerichaFriend
Messages: 13
Registered: July 2009
Junior Member
Tom,

It would be very helpful to me to look at the two classes you mentioned.

Thanks,

David
Re: EclipseLink as alternative to Hibernate in Swing App [message #385172 is a reply to message #385171] Fri, 02 January 2009 21:02 Go to previous message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
> It would be very helpful to me to look at the two classes you mentioned.

I created an Eclipse bug report (#259432) and attached both classes. You can find them there.

You will need to alter some minor code, like logging, but otherwise they are pretty autonomous.

Tom
Previous Topic:Converting from Hibernate composite-element mapping
Next Topic:EclipseLink vs Hibernate
Goto Forum:
  


Current Time: Thu Apr 25 02:04:58 GMT 2024

Powered by FUDForum. Page generated in 0.04127 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top