Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java persistence

Reply
Thread Tools

Java persistence

 
 
Shiladitya
Guest
Posts: n/a
 
      09-26-2010
Hi,
I am new to Java and need a simple, easy to use library to map Java
classes with MySQL tables. I came across Hibernate and Google persist.
Can you suggest pros and cons of these or suggest some other better
ORM ?

TIA
Shiladitya
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-26-2010
On 09/26/2010 12:09 AM, Shiladitya wrote:
> Hi,
> I am new to Java and need a simple, easy to use library to map Java
> classes with MySQL tables. I came across Hibernate and Google persist.
> Can you suggest pros and cons of these or suggest some other better
> ORM ?


Similar to Hibernate are the Java Persistence API (JPA) implementations
EclipseLink and Apache OpenJPA. They're all three robust and quite useful.
Make sure if you do use Hibernate that you stick with the JPA-compliant part
of it.

--
Lew
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      09-26-2010
On Sun, 26 Sep 2010, Lew wrote:

> On 09/26/2010 12:09 AM, Shiladitya wrote:
>
>> I am new to Java and need a simple, easy to use library to map Java
>> classes with MySQL tables. I came across Hibernate and Google persist.


I'd never even heard of Google Persist. Here we go:

http://code.google.com/p/persist/

It seems its goal is to be an as-simple-as-possible ORM. Of course,
eliminate the complexity of Hibernate-style ORMs means eliminating some of
their features: notably missing, from what i can see, are relationships
between objects, in that persistent objects can only have fields of simple
types (primitives, strings, and so on).

>> Can you suggest pros and cons of these or suggest some other better ORM
>> ?

>
> Similar to Hibernate are the Java Persistence API (JPA) implementations
> EclipseLink and Apache OpenJPA. They're all three robust and quite
> useful. Make sure if you do use Hibernate that you stick with the
> JPA-compliant part of it.


Good advice. I'm using OpenJPA at the moment and, once i realised what i
was doing wrong with the schema generator, it's an absolute doddle to use.
Classes looks like (and this is standard JPA; this will be identical with
Hibernate, EclipseLink, or any other JPA provider):

@Entity
public class Customer {
@Id
private int customerNumber;
private String name;
@OneToMany(mappedBy="customer")
private List<Order> orders;
// constructor, methods, etc
}

@Entity
public class Order {
@Id
private orderNumber;
@ManyToOne
private Customer customer;
// constructor, methods, etc
}

You compile that as normal.

You put a persistence.xml file in the classpath root with these contents
(again, this is standard JPA):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="example" transaction-type="RESOURCE_LOCAL" />
</persistence>

You then set up properties describing the database conenction; i do this
as an environment variable in a shell script, because i think it's runtime
configuration rather than part of the code, but you can also put this in
the persistence.xml file. Mine look like:

DB_ARGS="-Dopenjpa.ConnectionDriverName=$DB_DRIVER -Dopenjpa.ConnectionURL=$DB_URL -Dopenjpa.ConnectionUserName=$DB_USER -Dopenjpa.ConnectionPassword=$DB_PASS"

That's not standard JPA; i think JPA 2.0 defines standard property names
for the database connection details, but OpenJPA doesn't support them yet.

You then generate SQL for the necessary tables like this (having defined
the classpath variables appropriately):

java -cp "$PROJECT_CLASSPATH:$OPENJPA_CLASSPATH:$DB_CLASSPA TH" $DB_ARGS org.apache.openjpa.jdbc.meta.MappingTool -schemaAction build -sql schema.sql

You then run that schema.sql against your database. You can use the
mapping tool to set up the tables directly, but i prefer to make SQL and
then run that, because i have a record of exactly what gets done. This bit
is entirely OpenJPA-specific, i'm afraid. I think the resulting SQL files
would work with any JPA provider, though - i think the default mapping is
standardised.

You then write code to manipulate the objects like this (again, this is
standard JPA):

EntityManagerFactory emf = Persistence.createEntityManagerFactory("example"); // name matches the one in persistence.xml
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Customer c = new Customer(100, "Harry Brown");
Order o = new Order(83479);
c.addOrder(o);
em.persist(c);
tx.commit();
}
finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}

You compile that as normal, and run it with:

java -cp "$PROJECT_CLASSPATH:$OPENJPA_CLASSPATH:$DB_CLASSPA TH" $DB_ARGS MainClass

That's it.

Now, there are three things i really should have done but didn't, because
they in fact aren't truly necessary.

The first was to give my classes no-args constructors. The JPA spec
requires that entity classes have public or protected no-args
constructors. As it happens, the OpenJPA entity enhancer adds these if
they're needed. For portability and correctness, you should add them
explicitly.

The second was to list the entity classes in the persistence.xml. I'm not
sure the spec requires this, but i think it's a good idea even if it
doesn't - OpenJPA warns that i haven't done it, but deals with it. I have
a step in my build script which adds them automatically, so there's no
manual work involved in this for me.

The third is to enhance my classes manually. For OpenJPA, class files need
to be modified before they are loaded to allow transparent persistence.
You can do this with a command-line tool at build time, but if you don't,
OpenJPA can usually load an agent at runtime which will do it on the fly.
It's safer to do it at build time, though. Again, i have a line in my
script which does this, so there's no manual work.

So, even doing it properly, JPA is pretty simple.

tom

--
Only the bagel has the correct aspect ratio.
 
Reply With Quote
 
Shiladitya
Guest
Posts: n/a
 
      09-26-2010
On Sep 26, 9:48*am, Tom Anderson <(E-Mail Removed)> wrote:
> On Sun, 26 Sep 2010, Lew wrote:
> > On 09/26/2010 12:09 AM, Shiladitya wrote:

>
> >> I am new to Java and need a simple, easy to use library to map Java
> >> classes with MySQL tables. I came across Hibernate and Google persist.

>
> I'd never even heard of Google Persist. Here we go:
>
> http://code.google.com/p/persist/
>
> It seems its goal is to be an as-simple-as-possible ORM. Of course,
> eliminate the complexity of Hibernate-style ORMs means eliminating some of
> their features: notably missing, from what i can see, are relationships
> between objects, in that persistent objects can only have fields of simple
> types (primitives, strings, and so on).
>
> >> Can you suggest pros and cons of these or suggest some other better ORM
> >> ?

>
> > Similar to Hibernate are the Java Persistence API (JPA) implementations
> > EclipseLink and Apache OpenJPA. *They're all three robust and quite
> > useful. Make sure if you do use Hibernate that you stick with the
> > JPA-compliant part of it.

>
> Good advice. I'm using OpenJPA at the moment and, once i realised what i
> was doing wrong with the schema generator, it's an absolute doddle to use..
> Classes looks like (and this is standard JPA; this will be identical with
> Hibernate, EclipseLink, or any other JPA provider):
>
> @Entity
> public class Customer {
> * * * * @Id
> * * * * private int customerNumber;
> * * * * private String name;
> * * * * @OneToMany(mappedBy="customer")
> * * * * private List<Order> orders;
> * * * * // constructor, methods, etc
>
> }
>
> @Entity
> public class Order {
> * * * * @Id
> * * * * private orderNumber;
> * * * * @ManyToOne
> * * * * private Customer customer;
> * * * * // constructor, methods, etc
>
> }
>
> You compile that as normal.
>
> You put a persistence.xml file in the classpath root with these contents
> (again, this is standard JPA):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
> * * *<persistence-unit name="example" transaction-type="RESOURCE_LOCAL" />
> </persistence>
>
> You then set up properties describing the database conenction; i do this
> as an environment variable in a shell script, because i think it's runtime
> configuration rather than part of the code, but you can also put this in
> the persistence.xml file. Mine look like:
>
> DB_ARGS="-Dopenjpa.ConnectionDriverName=$DB_DRIVER -Dopenjpa.ConnectionURL=$DB_URL -Dopenjpa.ConnectionUserName=$DB_USER -Dopenjpa.ConnectionPassword=$DB_PASS"
>
> That's not standard JPA; i think JPA 2.0 defines standard property names
> for the database connection details, but OpenJPA doesn't support them yet..
>
> You then generate SQL for the necessary tables like this (having defined
> the classpath variables appropriately):
>
> java -cp "$PROJECT_CLASSPATH:$OPENJPA_CLASSPATH:$DB_CLASSPA TH" $DB_ARGS org.apache.openjpa.jdbc.meta.MappingTool -schemaAction build -sql schema.sql
>
> You then run that schema.sql against your database. You can use the
> mapping tool to set up the tables directly, but i prefer to make SQL and
> then run that, because i have a record of exactly what gets done. This bit
> is entirely OpenJPA-specific, i'm afraid. I think the resulting SQL files
> would work with any JPA provider, though - i think the default mapping is
> standardised.
>
> You then write code to manipulate the objects like this (again, this is
> standard JPA):
>
> EntityManagerFactory emf = Persistence.createEntityManagerFactory("example"); // name matches the one in persistence.xml
> EntityManager em = emf.createEntityManager();
> EntityTransaction tx = em.getTransaction();
> try {
> * * * * tx.begin();
> * * * * Customer c = new Customer(100, "Harry Brown");
> * * * * Order o = new Order(83479);
> * * * * c.addOrder(o);
> * * * * em.persist(c);
> * * * * tx.commit();}
>
> finally {
> * * * * if (tx.isActive()) {
> * * * * * * * * tx.rollback();
> * * * * }
> * * * * em.close();
>
> }
>
> You compile that as normal, and run it with:
>
> java -cp "$PROJECT_CLASSPATH:$OPENJPA_CLASSPATH:$DB_CLASSPA TH" $DB_ARGS MainClass
>
> That's it.
>
> Now, there are three things i really should have done but didn't, because
> they in fact aren't truly necessary.
>
> The first was to give my classes no-args constructors. The JPA spec
> requires that entity classes have public or protected no-args
> constructors. As it happens, the OpenJPA entity enhancer adds these if
> they're needed. For portability and correctness, you should add them
> explicitly.
>
> The second was to list the entity classes in the persistence.xml. I'm not
> sure the spec requires this, but i think it's a good idea even if it
> doesn't - OpenJPA warns that i haven't done it, but deals with it. I have
> a step in my build script which adds them automatically, so there's no
> manual work involved in this for me.
>
> The third is to enhance my classes manually. For OpenJPA, class files need
> to be modified before they are loaded to allow transparent persistence.
> You can do this with a command-line tool at build time, but if you don't,
> OpenJPA can usually load an agent at runtime which will do it on the fly.
> It's safer to do it at build time, though. Again, i have a line in my
> script which does this, so there's no manual work.
>
> So, even doing it properly, JPA is pretty simple.
>
> tom
>
> --
> Only the bagel has the correct aspect ratio.


Thanks for the suggestions Lew, and code snippets Tom. I think I will
use OpenJPA and see how it goes.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-27-2010
On Sat, 25 Sep 2010 21:09:36 -0700 (PDT), Shiladitya
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>I am new to Java and need a simple, easy to use library to map Java
>classes with MySQL tables. I came across Hibernate and Google persist.
>Can you suggest pros and cons of these or suggest some other better
>ORM ?


If you are new to Java, you might avoid an ORM for now. Then when you
use Hibernate or similar, you will have a better idea of what is going
on under the covers which should help in training Hibernate to jump
through hoops.
--
Roedy Green Canadian Mind Products
http://mindprod.com

You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      09-28-2010
On 26-09-2010 08:15, Lew wrote:
> On 09/26/2010 12:09 AM, Shiladitya wrote:
>> I am new to Java and need a simple, easy to use library to map Java
>> classes with MySQL tables. I came across Hibernate and Google persist.
>> Can you suggest pros and cons of these or suggest some other better
>> ORM ?

>
> Similar to Hibernate are the Java Persistence API (JPA) implementations
> EclipseLink and Apache OpenJPA. They're all three robust and quite
> useful. Make sure if you do use Hibernate that you stick with the
> JPA-compliant part of it.


I consider that very good advice.

Unfortunately it is not widely followed.

Arne

 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      09-28-2010
Arne Vajhj wrote:
> On 26-09-2010 08:15, Lew wrote:
>> On 09/26/2010 12:09 AM, Shiladitya wrote:
>>> I am new to Java and need a simple, easy to use library to map Java
>>> classes with MySQL tables. I came across Hibernate and Google
>>> persist. Can you suggest pros and cons of these or suggest some
>>> other better ORM ?

>>
>> Similar to Hibernate are the Java Persistence API (JPA)
>> implementations EclipseLink and Apache OpenJPA. They're all three
>> robust and quite useful. Make sure if you do use Hibernate that you
>> stick with the JPA-compliant part of it.

>
> I consider that very good advice.
>
> Unfortunately it is not widely followed.
>
> Arne


One reason it hasn't been widely followed is that with JPA 1.0 there were
important and interesting things that you couldn't do with JPA, but
Hibernate or EclipseLink (or presumably OpenJPA) did supply useful APIs to
do them. There should be considerably fewer reasons to dive into native API
land when using JPA 2.0 implementations.

AHS
--
The difference between a moral man and a man of honor is that the
latter regrets a discreditable act, even when it has worked and he has
not been caught. -- H.L. Mencken


 
Reply With Quote
 
Arne Vajhj
Guest
Posts: n/a
 
      09-28-2010
On 28-09-2010 05:28, Arved Sandstrom wrote:
> Arne Vajhj wrote:
>> On 26-09-2010 08:15, Lew wrote:
>>> On 09/26/2010 12:09 AM, Shiladitya wrote:
>>>> I am new to Java and need a simple, easy to use library to map Java
>>>> classes with MySQL tables. I came across Hibernate and Google
>>>> persist. Can you suggest pros and cons of these or suggest some
>>>> other better ORM ?
>>>
>>> Similar to Hibernate are the Java Persistence API (JPA)
>>> implementations EclipseLink and Apache OpenJPA. They're all three
>>> robust and quite useful. Make sure if you do use Hibernate that you
>>> stick with the JPA-compliant part of it.

>>
>> I consider that very good advice.
>>
>> Unfortunately it is not widely followed.

>
> One reason it hasn't been widely followed is that with JPA 1.0 there were
> important and interesting things that you couldn't do with JPA, but
> Hibernate or EclipseLink (or presumably OpenJPA) did supply useful APIs to
> do them. There should be considerably fewer reasons to dive into native API
> land when using JPA 2.0 implementations.


My guess is that even for JPA 1.0 it was more laziness/ignorance
than need for specific features.

Arne

 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      09-28-2010
Arne Vajhj wrote:
> On 28-09-2010 05:28, Arved Sandstrom wrote:
>> Arne Vajhj wrote:
>>> On 26-09-2010 08:15, Lew wrote:
>>>> On 09/26/2010 12:09 AM, Shiladitya wrote:
>>>>> I am new to Java and need a simple, easy to use library to map
>>>>> Java classes with MySQL tables. I came across Hibernate and Google
>>>>> persist. Can you suggest pros and cons of these or suggest some
>>>>> other better ORM ?
>>>>
>>>> Similar to Hibernate are the Java Persistence API (JPA)
>>>> implementations EclipseLink and Apache OpenJPA. They're all three
>>>> robust and quite useful. Make sure if you do use Hibernate that you
>>>> stick with the JPA-compliant part of it.
>>>
>>> I consider that very good advice.
>>>
>>> Unfortunately it is not widely followed.

>>
>> One reason it hasn't been widely followed is that with JPA 1.0 there
>> were important and interesting things that you couldn't do with JPA,
>> but Hibernate or EclipseLink (or presumably OpenJPA) did supply
>> useful APIs to do them. There should be considerably fewer reasons
>> to dive into native API land when using JPA 2.0 implementations.

>
> My guess is that even for JPA 1.0 it was more laziness/ignorance
> than need for specific features.
>
> Arne


In some cases ego. Based on code I've had to maintain some programmers never
saw an API but that they had to figure out a way to include a use of it.

AHS
--
The difference between a moral man and a man of honor is that the
latter regrets a discreditable act, even when it has worked and he has
not been caught. -- H.L. Mencken


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Persistence API and persistence.xml Kenneth P. Turvey Java 2 03-16-2008 12:08 AM
Java Object Persistence (JSP & Java) Snyke Java 4 11-02-2005 10:57 PM
EJB persistence versus Database persistence? javaguy44 Java 10 05-18-2004 07:08 PM
[ANN] FireStorm/DAO Database Persistence Tool for Java Programmers Andy Grove Java 0 11-04-2003 07:53 PM
[semi-OT] java objects persistence and XML id/idref rico Java 2 10-28-2003 02:12 AM



Advertisments