Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7657
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Persistence API - magic? |
| Date | 2011-09-06 18:09 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <98c06b0a-1ece-40fd-996f-2dac21c26feb@glegroupsg2000goo.googlegroups.com> (permalink) |
| References | <7e3a3be9-8960-4b94-8028-2da962435fc8@u6g2000yqa.googlegroups.com> |
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/
>
> 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. 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. Here's an example entity:
>
> @Entity
> public class Zip extends com.manessinger.util.jpa.Entity implements
> Serializable {
> private static final long serialVersionUID = 1L;
Please, please, please do not use TAB characters to indent code on Usenet!
Use spaces, up to four per indent level.
> @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.
>
> 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();
> }
>
> The information doesn't seem to be at the query site either:
>
> Query q = em.createQuery("select co from Country co");
This looks like SQL but is most definitely not SQL. It is a request to obtain a collection (an object-oriented concept) of Country objects. It is not a set of table rows.
> result = (List<Country>)q.getResultList();
As you see in this simple example, the sensibility of JPA is object-oriented. This differs significantly from the relational algebra at the heart of the data-oriented sensibility of RDBMSes.
> 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?
It is like magic, which is both a strength and a source of potential confusion.
By default the JPA mechanism matches based on the class and attribute names. An entity type like 'Zip' maps to a similarly-named table, "zip". You can set up the persistence.xml to have the system actually create the tables for you as it executes the code.
The attributes of a class, such as 'id', map to similarly-named columns in the table by default, e.g., "id".
As Arved pointed out, you can override these defaults with annotations. See the Java EE tutorial for details.
<http://java.sun.com/javaee/6/docs/tutorial/doc/bnbpy.html>
The persistence.xml interacts with the application server's configuration files (such as server.xml and web.xml in Tomcat) to link to a database system.
Relations are mapped to collections of objects. If you do JPA right, you almost never have to refer to an id field (in the SQL sense, an id column)_explicitly. You do have to involve the "natural" keys of your object types in the 'equals()' and 'hashCode()' (and as Josh Bloch and others suggest, the 'toString()') methods. (Auto-generated integer surrogate keys are anathema!)
JPA works best when the entity types are simple.
--
Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
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