InventoryItem.java
...
@ManyToOne
@JoinColumn(name="CAT_ID", nullable=false)
public InventoryCategory getCategory()
{
return category;
}
...
* The annotation @ManyToOne identifies that:
* This object has a many-to-one relationship with the InventoryCategory
object. Meaning that there many InventoryItems can reference the same
InventoryCategory
* The annotation
@JoinColumn identifies that:
* The column in the database table that holds the InventoryCategory
reference by the attribute:
name=
"CAT_ID".
| Remember
These annotations contribute to the creation of the tables and the
relationships. It is important to put as much thought into how these
objects relate to each other as you would if you were designing the
database tables because you are designing the database tables.
You can see more about the relationship annotations at the Apache OpenJPA
site. The documentation is here
.
|
# Summary
This was a very basic example of how to use OpenJPA with Geronimo and
Derby. However, many applications that require database persistence do not
use much more than the basic functionality demonstrated in this tutorial.
The purpose of this was to be a primer. Aside from the setup of the server
and database, we went through the creation of a persistence.xml file, the
basics of the OpenJPA Entity, and EntityManager and some of the
functionality.
**Exercises**
Using this sample code as a base, try to do some of the following:
* Add additional fields to the InventoryItem Entity.
* Create a Customer Entity and establish a one-to-many relationship with
the InventoryItems as one customer having purchased many items.
* Since several customers could have purchased the same items and an item
could have been purchased by many customers, use the documentation to
create a many-to-many relationship using @ManyToMany*.
\*To make these changes you may have to delete the existing database
tables so that they can recreated with the new relationship fields.
# References
* **Java J2SE 1.6**
* [Download](https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jdk-6u16-oth-JPR@CDS-CDS_Developer)
* *
* **Apache Sources**
* **Geronimo**
* [Geronimo Homepage](http://geronimo.apache.org/)
* [Gernimo V2.2 Server Documentation](http://cwiki.apache.org/GMOxDOC22/documentation.html)
* [Geronimo V2.2 Server download](http://www.apache.org/dyn/closer.cgi/geronimo/2.2/geronimo-tomcat6-javaee5-2.2-bin.zip)
* **JPA**
* [Apache OpenJPA home page](http://openjpa.apache.org/)
* [Apache OpenJPA download](http://openjpa.apache.org/downloads.html)
* [Apache OpenJPA documentation](http://openjpa.apache.org/documentation.html)
* **Annotations**
* [Documentation for Java Annotations](http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html)
* **JPQL**
* [A reference for JPQL](http://java.sun.com/javaee/5/docs/tutorial/backup/update3/doc/QueryLanguage.html)
* **Blogs**
* [Discussion on The Server Side](http://www.theserverside.com/news/thread.tss?thread_id=58343)
* [Websphere & OpenJPA blog on blogspot](http://bit.ly/WASJPAblog)
* [JPA Blog on developerWorks](http://bit.ly/JPATutApachedwBlog)
* **Implementation Sites**
* [IBM WebSphere Application Server V7 Java Persistence API (JPA) 2.0 Open Beta](http://bit.ly/BetaSiteApache)