A round trip working with objects
Hibernate may be regarded as a persistence provider to JPA:
Figure 930. JPA persistence provider
Todo: Ref/Jdbc/persistProvider.fig
The following class hibintro.v1.model.User
will be used as a starting example to be mapped to a database. Notice
the javax.persistence.Entity
annotation
❶:
Figure 931. Mapping
hibintro.v1.model.User
instances
to a database. package hibintro.v1;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* A simple class intended to be mapped to a database server
*
*/
@Entity ❶
public class User {
String uid;
/**
* @return The user's unique login name e.g. "goik"
*/
@Id
public String getUid() {
return uid;
}
/**
* @param uid See {@link #getUid()}.
*/
public void setUid(String uid) {
this.uid = uid;
}
String cname;
/**
* @return The user's common name e.g. "Martin Goik"
*/
public String getCname() {
return cname;
}
/**
* @param cname See {@link #getCname()}.
*/
public void setCname(String cname) {
this.cname = cname;
}
/**
* Hibernate/JPA require a default constructor. It has has to be
* implemented if any non-default constructor has been defined
*/
public User() {}
/**
* @param uid See {@link #getUid()}.
* @param cname See {@link #getCname()}.
*/
public User(String uid, String cname) {
this.uid = uid;
this.cname = cname;
}
}
With respect to Figure 929, “A basic persistence.xml
JPA configuration file. ” we
notice our class hibintro.v1.model.User
being
referenced:
<persistence ... >
<persistence-unit name="hibintroV1PU">
<class>hibintro.v1.User
</class>
...
</persistence-unit>
...
This line tells Hibernate to actually map
hibintro.v1.model.User
to the database.