Java persistence

Discussion in 'Java' started by Shiladitya, Sep 26, 2010.

  1. Shiladitya

    Shiladitya Guest

    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
    Shiladitya, Sep 26, 2010
    #1
    1. Advertising

  2. Shiladitya

    Lew Guest

    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
    Lew, Sep 26, 2010
    #2
    1. Advertising

  3. Shiladitya

    Tom Anderson Guest

    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_CLASSPATH" $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_CLASSPATH" $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.
    Tom Anderson, Sep 26, 2010
    #3
  4. Shiladitya

    Shiladitya Guest

    On Sep 26, 9:48 am, Tom Anderson <> 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_CLASSPATH" $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_CLASSPATH" $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.
    Shiladitya, Sep 26, 2010
    #4
  5. Shiladitya

    Roedy Green Guest

    On Sat, 25 Sep 2010 21:09:36 -0700 (PDT), Shiladitya
    <> 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.
    Roedy Green, Sep 27, 2010
    #5
  6. 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
    Arne Vajhøj, Sep 28, 2010
    #6
  7. Arne Vajhøj 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
    Arved Sandstrom, Sep 28, 2010
    #7
  8. Shiladitya

    Arne Vajhøj Guest

    On 28-09-2010 05:28, Arved Sandstrom wrote:
    > Arne Vajhøj 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
    Arne Vajhøj, Sep 28, 2010
    #8
  9. Arne Vajhøj wrote:
    > On 28-09-2010 05:28, Arved Sandstrom wrote:
    >> Arne Vajhøj 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
    Arved Sandstrom, Sep 28, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rico
    Replies:
    2
    Views:
    396
  2. Andy Grove
    Replies:
    0
    Views:
    424
    Andy Grove
    Nov 4, 2003
  3. javaguy44
    Replies:
    10
    Views:
    959
    Michael Berg
    May 18, 2004
  4. Snyke
    Replies:
    4
    Views:
    863
    Snyke
    Nov 2, 2005
  5. Kenneth P. Turvey

    Java Persistence API and persistence.xml

    Kenneth P. Turvey, Mar 15, 2008, in forum: Java
    Replies:
    2
    Views:
    17,252
    Kenneth P. Turvey
    Mar 16, 2008
Loading...

Share This Page