Eclipse Java EE for Web Developers
JBoss tools ???
Hibernate tools ???
Prepare database
create user
create database
grant schema privileges
test: login and connect to the database
Configure a data source with a JNDI name on JBoss server
start wildfly/jboss server
goto http://localhost:9990/console/App.html
configuration -> subsystems -> data sources -> non-XA (if transaction with single db) -> add
give: name, JNDI (java:/cnipdb), enabled true, driver (pre-installed, mysql-connector-java-5.1xxx.jar)
give: connection url, use JTA:true, use CCM: true (??), security: username password
connection -> test connection
record JNDI name down: "java:/cnipdb"
This, my understanding, is for the Eclipse tools
Eclipse, add driver: window -> preference -> data management -> connectivity -> driver definition, add required driver
Data Source Explorer -> Database connections -> add
After setting up a new EJB Project (or other project)
Enable JPA facet:
Project property -> Project Facets -> JPA
Select the proper version that the server supports
XML error may be thrown if configured with wrong version
How to find out?
server documentation
grep jpa under server path?
Java EE 6 uses JPA 2.0 (see JBoss development guide)
Select run time (server)
Configure project JPA aspect:
Project property -> JPA ->
Platform hibernate (JPA 2.1) (depends on server, find out from server document)
type: library provided by target runtime
connection: the created connection in data source explorer view
class management: discover (or add to persistence.xml, doesn't matter)
Canonical metamodel -> source folder ->
select one, otherwise JPA details view not work
if don't want it mess up with other code, create a separate source folder
First find out hibernate version used by server: (from jboss installation dir, grep hibernate-core)
Open view Hibernate Configuration -> add
name: correspondent with project
type: JPA
Hibernate version: (same with server)
project: select
connection: JPA project configured connection (this uses the connection configured in project property -> JPA)
persistence unit: browse and select
After saved the configuration, expand to test the hibernate connection and proper configuration
Project property -> Hibernate settings -> enable, select just created console configuration
Find out configured data sources at JBoss server:
select server view,
select Jboss server, start
explore: server details -> sub systems -> data sources -> data source -> [data source] -> attributes -> jndi-name
Open persistence.xml:
Connection
Transaction type: default (JTA)
JTA data source: set the JNDI name of the datasource (java:jboss/datasources/BingSampleApp)
Create an entity:
Things to do to the class:
add "private int id" (in later steps it will be recognised)
add @Entity annotation (Right click a class, JPA->Make persistent do the same, ignore others for now)
add properties, getter and setters etc.
source - generate hibernate / JPA annotations
enable optimistic locking - this will generate version field
preferred location of annotation
it affects how JPA access the entity
maybe getter?
review, remove unwanted change, and finish
Select the entity and open JPA details view, to view and adjust JPA properties
Generate schema:
JPA schema generation tool sucks. If anything went wrong, it does not generate schema, does not popup any message, and Eclipse starts behaving wired.
To make it work, for
Select the project, right click, JPA tools -> generate table from entities, select hibernate console configuration, generate.
Open generated schedule.ddl (if not generated, check error, and if complain <persistence> cannot find, might be JPA version incompatible with supported version).
If not connected, open data source explorer, connect to database, select schema (if not selected) -> execute all.
View generated tables in "SQL results" view.
http://relation.to/Bloggers/HibernateStaticMetamodelGeneratorAnnotationProcessor
Project property -> Annotation Processing
Enable project specific setting
Enable annotation processing
Enable processing in editor
Generated source directory - any
-> Factory path -> Add external JARs
find the path to downloaded Hibernate release, under lib/jpa-metamodel-generator, locate the jar
OK.
JPA Details view : put cursor on annotation and adjust JPA details
JPA Structure: Open persistence.xml and view JPA structure