Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7653

Re: Persistence API - magic?

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: Persistence API - magic?
References <7e3a3be9-8960-4b94-8028-2da962435fc8@u6g2000yqa.googlegroups.com>
Message-ID <f4z9q.9346$sk5.2721@newsfe03.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2011-09-06 21:40 -0300

Show all headers | View raw


On 11-09-06 07:52 PM, nroberts wrote:
> I'm a little confused about how and why the persistence API is even
> being used, let alone how it works, in this tutorial I just went
> through.
> 
> I followed the instructions in this tutorial:
> http://programming.manessinger.com/tutorials/an-eclipse-glassfish-java-ee-6-tutorial/

I won't discourage you from trying out Java EE 6, but it's a big jump up
from what you're able to do with JBoss 4. I expect you knew that.

Looking at this particular tutorial, my recommendation is, stick with
the Java EE tutorial, and then more focused third-party tutorials. This
Manessinger fellow is simply covering way too much stuff, with not
nearly enough detail for most of it. I wouldn't steer anyone to this
tutorial - my 2 cents worth. YMMV.

> What I'm trying to figure out is how the "entity" classes that eclipse
> created with the JPA tools work so I can make my own if I need to.
> 
> One tutorial on persistence that I found (
> http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html?page=3 )
> explains how to make them by hand but it uses an orm.xml or
> annotations to tell Java what table and what columns to connect an
> entity object with.

I won't swear to it but that's probably a good JPA tutorial for JBoss 4,
because it's a JPA 1.0 tutorial and I'd be surprised if JBoss 4 supports
JPA 2.0.

You won't need an orm.xml at this point, if ever - stick to
persistence.xml and annotations.

> The eclipse stuff doesn't seem to do that at
> all.  The orm.xml file exists, but it's practically empty containing
> nothing but the root element and xml tag.

Basically, as I suggested above, you can ignore the orm.xml. I've almost
never had one myself.

> Here's an example entity:
> 
> @Entity
> public class Zip extends com.manessinger.util.jpa.Entity implements
> Serializable {
> 	private static final long serialVersionUID = 1L;
> 
> 	@Id
> 	@GeneratedValue(strategy=GenerationType.IDENTITY)
> 	private Integer id;
> 
> 	private String code;
> 
> 	private String name;
> 
> 	//bi-directional many-to-one association to Country
>     @ManyToOne
> 	private Country country;
>  // ... getters and setters...
> }
> 
> I was thinking that perhaps it was order based, but the order of the
> columns in the zip table to do not match.  COUNTRY_ID comes between ID
> and CODE.  The names are close but the case is different and the
> "country" column is actually COUNTRY_ID.

Order is not important. Also (and this anticipates a comment below) JPA
uses a fair bit of convention over configuration, so there are set rules
that dictate why the default PK for the referenced table is COUNTRY_ID.
This is in the JPA APIs and specification.

If you want to change from the defaults then there are various
annotations and annotation attributes that let you do that.

> The manessinger Entity base doesn't have anything interesting but some
> id utility functions:
> 
> public abstract class Entity {
> 	public static boolean isId(Integer id)
> 	{
> 		return (id != null && id > 0);
> 	}
> 
> 	public boolean hasId()
> 	{
> 		return isId(getId());
> 	}
> 
> 	public abstract Integer getId();
> }

This is a fairly unnecessary base class for entity classes if that's all
it does. I don't even like the premise of that abstract class. Ignore
it, and keep the "id" (whatever the @Id field is called actually)
accessors in the entity classes.

There's nothing wrong with having a entity base class though.
@MappedSuperclass is designed for this purpose better than what Mr
Manessinger devised. At this stage of the game I wouldn't bother with this.

> The information doesn't seem to be at the query site either:
> 
>     	Query q = em.createQuery("select co from Country co");
>     	result = (List<Country>)q.getResultList();
> 
> What are the rules that Java is using in order to tell what I want
> here and do it?  As it stands it seems to almost be intuitively
> interpreting what I want to do, which I know is impossible.  Why does
> this code work?

See above - JPA API docs and the specification. And I strongly recommend
the JPA section of the Java EE tutorial.

AHS

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Persistence API - magic? nroberts <roberts.noah@gmail.com> - 2011-09-06 15:52 -0700
  Re: Persistence API - magic? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-09-06 21:40 -0300
    Re: Persistence API - magic? nroberts <roberts.noah@gmail.com> - 2011-09-07 07:56 -0700
      Re: Persistence API - magic? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-09-07 17:34 -0300
  Re: Persistence API - magic? markspace <-@.> - 2011-09-06 17:47 -0700
    Re: Persistence API - magic? Lew <lewbloch@gmail.com> - 2011-09-06 18:26 -0700
      Re: Persistence API - magic? markspace <-@.> - 2011-09-06 19:01 -0700
    Re: Persistence API - magic? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-09-07 06:14 -0300
    Re: Persistence API - magic? nroberts <roberts.noah@gmail.com> - 2011-09-07 08:10 -0700
      Re: Persistence API - magic? markspace <-@.> - 2011-09-07 09:15 -0700
        Re: Persistence API - magic? Lew <lewbloch@gmail.com> - 2011-09-07 10:36 -0700
          Re: Persistence API - magic? markspace <-@.> - 2011-09-07 10:57 -0700
            Re: Persistence API - magic? Lew <lewbloch@gmail.com> - 2011-09-07 11:06 -0700
  Re: Persistence API - magic? Lew <lewbloch@gmail.com> - 2011-09-06 18:09 -0700
  Re: Persistence API - magic? Roedy Green <see_website@mindprod.com.invalid> - 2011-09-08 12:32 -0700
    Re: Persistence API - magic? "John B. Matthews" <nospam@nospam.invalid> - 2011-09-08 19:23 -0400

csiph-web