Persistence API - magic?
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;
@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");
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?