Thufir
Thufir

Reputation: 8497

@Entity with JSF, persist "hello world" with JPA

Oracle describes very well how to make an @Entity. However, it's not strictly clear to me how to actually add/drop tables. I also like the rose india explanation, but just want to clarify the general idea.

For a JSF JEE6 app with CDI, I can basically just create an @Entity class, instantiate some instances in the @Named bean, and write (CRUD operations) to the MySQL database with an EntityManager from the bean? I can just use default JPA which comes with Glassfish?

Just want to clarify the general idea before I get started.

Upvotes: 0

Views: 598

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

In general you have at least two options:

  1. Create your database tables and references, then build your entity classes based on the database tables (modern IDEs provide tools for automatic generation of entity classes from db tables)

  2. Write your entity class manually and create the database from these classes. JPA providers normally allow this by setting a special parameter in the persistence.xml , e.g. for Eclipselink:

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

drops all existing tables and creates new ones from your entity classes (especially useful during development), while

<property name="eclipselink.ddl-generation" value="create-tables"/>

will only create a new table if there is no existing.

I can just use default JPA which comes with Glassfish?

Yes, for the functionality that is based on the specification.

Upvotes: 5

Related Questions