KEMBAR78
Hibernate java and_oracle | PPT
copyright 2006, rhinosystemsinc.com all rights re
What we'll Cover
• Introduction of HibernateV3
Configuration & Setup of Hibernate/Java
• Code
• Pros and Cons
• RESOURCES
• Q & A.
copyright 2006, rhinosystemsinc.com all rights re
Intro:About Hibernate
• www.hibernate.org - Open Source since, and
now with JBoss (since late 2003)
w/ commercial support & training.
Founder: Gavin King
• Provides a layer for java to interact with the
database.
– Is technically a set of java-class libraries that you use to
gain access to your database
• Hibernate caches database objects
– & Hibernate services your Java program
1/3
copyright 2006, rhinosystemsinc.com all rights re
Intro:About Hibernate
• Hibernate layer resides in your JVM.
• Improves performance
– objects are cached in JVM and mapped to your
object model.
• Used in commercial applications
– JBoss for one (yes, another open source…)
– Many more opensource and commercial listed at:
http://www.hibernate.org/27.html
2/3
copyright 2006, rhinosystemsinc.com all rights re
Intro:About Hibernate
• Hibernate is highly customizable
– Caching (2nd
level cache).
– Database dialects
– Transaction
– Connection pooling
– Custom Types
• Or use out of box (recommend changing: connection pooling).
3/3
copyright 2006, rhinosystemsinc.com all rights re
Intro:Importance of DAO
• DAO: Data Access Objects –
design/pattern
• Purpose is to abstract your calls to the
database.
– Don't put SQL directly into your Java/JSP.
• You can change our database with minimal
affect on your code
• Identify bottle necks and bugs easier.
1/2
copyright 2006, rhinosystemsinc.com all rights re
Intro:Importance of DAO
• Better design translates into easier to
understand code.
• Hibernate DAO - 3rd
party technology
– Benefits of common knowledge
– Fixes to technology
– Specialized for each database and optimized for
database access (caveats).
2/2
copyright 2006, rhinosystemsinc.com all rights re
Intro:Comparison to SQL
• Problem with SQL
– never lived up to promise of standardization amongst
database vendors
– Uses Jdbc to access database – (no forced design)
– Is Relational
• With Hibernate:
– Caching
– Easier to code
– Standard access
– Is Object Oriented and maps to Relational.
1/2
copyright 2006, rhinosystemsinc.com all rights re
Intro:Comparison to SQL
• Speed/performance
– Prepared Statements and caching (hib)
– Can batch process many DML statements. (hib)
– Better performance from PL/SQL (sql/hib)
• Maintenance/Updates (hib)
• Legacy system (depends)
– Using hibernate with legacy database systems
& Legacy java code retrofit with Hibernate
2/2
copyright 2006, rhinosystemsinc.com all rights re
Configuration Hibernate/Java
• Download and install JDK 1.4 or 1.5
• Download version3 from
www.hibernate.org
• Setup Hibernate's Jars into your project's
classpath.
– The hibernate3.jar into your project's classpath;
and if need be all the <hib>/lib (if you don't
already have them).
1/5
copyright 2006, rhinosystemsinc.com all rights re
Config:hibernate.properties
• Make sure that Oracle's client jdbc lib is in
your CLASSPATH
• Hibernate.properties needs to be in your
project's & runtime classpath.
HIBERNATE.PROPERTIES
#for OCI (local instance of Oracle, not using tnsnames.ora).
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.username=joel
hibernate.connection.password=xyz
hibernate.connection.pool_size=5
hibernate.connection.url=jdbc:oracle:oci:@
hibernate.show_sql=true
2/5
copyright 2006, rhinosystemsinc.com all rights re
Config: Download & Config
Xdoclet
[OPTIONAL]
• In order to run the "generation" of hibernate
XML you need to setup your classpath to include
Xdoclet libraries
• Download latest (v1.2.3) from
http://xdoclet.sourceforge.net/xdoclet/index.html
(www.xdoclet.org?)
– Download direct from
http://sourceforge.net/project/showfiles.php?group_id=31602
– Tag References
http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html
3/5
copyright 2006, rhinosystemsinc.com all rights re
config: Download & Config ANT
[OPTIONAL]
• In order to use ANT you'll need to setup your
PATH to include ANT. Check – run "ant"
• Download latest (v1.6.5) from http://ant.apache.org/
(www.ant.org?)
– Download direct from
http://www.axint.net/apache/ant/binaries/apache-ant-1.6.5-bin.zip
– Unzip and make sure "ant" is in your path. (right-click my computer ->
properties ->Advanced->Environmental Variables -> update PATH with directory
<ant_install>/bin)
4/5
copyright 2006, rhinosystemsinc.com all rights re
config: Download & Config ANT
• SUMMARY:
– JDK [required]: PATH,CLASSPATH
– HIBERNATE [required]: CLASSPATH
• ORACLE or DBMS client [required]:
CLASSPATH (connection/lib info w/ hibernate.properties)
– XDOCLET [optional]:CLASSPATH
– ANT [optional]:CLASSPATH
5/5
copyright 2006, rhinosystemsinc.com all rights re
Code & Fragments
• What we'll cover in the CODE section. For
each of the items below you'll see the XML,
the DDL SQL, Java code and an
explanation.
–Single Entity
–Primary Keys
–Many-to-One
–Many-to-Many
–DML (Query, Insert, Update, Delete)
–Extra topics
copyright 2006, rhinosystemsinc.com all rights re
Code: Overview
• Hibernate layer is
independent of your
code
• Entities map via
hibernate XML
• Hibernate & you:
DML, caching,
isolation levels
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Java POJO Entity
public class Person {
Long PERSON_ID=null; //manufactured surrogate key
String FIRST_NAME=null;
String LAST_NAME=null;
public Long getPERSON_ID(){…}
public void setPERSON_ID(String) {…}
public String getFIRST_NAME(){…}
public void setFIRST_NAME(String) {…}
public String getLAST_NAME(){…}
public void setLAST_NAME (String) {…}
}
See SAMPLE 0
1/4
Standard get/set &
need empty
constructor ~
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: XML for Entity
<!--Must be named: Person.hbm.xml and reside in CLASSPATH-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.rhinosystemsinc.hibernate.samples_0.Person" table="SAMPLE0_Person" >
<id name="PERSON_ID" column="PERSON_ID" type="java.lang.Long" unsaved-value="null">
<generator class="hilo">
<param name="table">SAMPLE0_PERSON_SEQ</param>
<param name="column">NEXT</param>
</generator>
</id>
<property
name="FIRST_NAME"
type="java.lang.String"
update="true"
insert="true"
column="FIRST_NAME"
not-null="true"
unique="false"
length="100"
/>
<property name="LAST_NAME"/>
</class>
</hibernate-mapping>
2/4
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<id name="PERSON_ID" column="PERSON_ID" type="java.lang.Long" unsaved-value="null">
<generator class="hilo">
<param name="table">SAMPLE0_PERSON_SEQ</param>
<param name="column">NEXT</param>
</generator>
</id>
Define the manufactured
surrogate primary key,
based on SEQUENCE.
Also natural composite
primary keys:
<composite-id>
<key-property
name="name"/>
<key-property
name="ssn"/>
</composite-id>
<property
name="FIRST_NAME"
type="java.lang.String"
update="true"
insert="true"
column="FIRST_NAME"
not-null="true"
unique="false"
length="100"
/>
<property name="LAST_NAME"/>
copyright 2006, rhinosystemsinc.com all rights re
Code: Primary Keys
• All hibernate persisted Entity mappings
must have a primary key.
• Suggestion: Use surrogate generated keys.
– Hib supports natural keys ( & multi-column).
– Reason:
• Sometimes (rarely) keys will change values
• Easier to manage
3/4
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Java Main Class
//SAVING A NEW OBJECT
org.hibernate.Session sess = sessFact.openSession(); //more on SessionFactory in a minute.
Person p = new Person();
p.setFIRST_NAME("John");
p.setLAST_NAME("Smith");
Transaction tx = sess.beginTransaction();
sess.saveOrUpdate(p);
tx.commit();
sess.close();
See SAMPLE 0
At this point, what if we do an
update, via SQLPLUS?
4/4
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Java 1-to-Many
public class Person {
Long PERSON_ID=null; //manufactured surrogate key
String FIRST_NAME=null;
String LAST_NAME=null;
Set ADDRESSES=null;
public Set getADDRESSES()
{
return ADDRESSES;
}
public void setADDRESSES(Set ADDRESSES)
{
this.ADDRESSES = ADDRESSES;
}
…//rest of get/set methods
}
NEW
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Person XML 1-to-Many
…
<hibernate-mapping>
<class name="com.rhinosystemsinc.hibernate.samples_0.Person" table="SAMPLE0_Person" >
…
<set
name="ADDRESSES"
lazy="false"
inverse="true"
cascade="save-update"
sort="unsorted"
outer-join="true">
<key column="PERSON_ID"/>
<one-to-many class="com.rhinosystemsinc.hibernate.samples_1.Address"/>
</set>
…
</class>
</hibernate-mapping>
NEW
Any records that don't have address will be shown too.
all or save-update-delete
Notice one-to-many ~
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Java 1-to-Many
public class Address {
Long ADDRESS_ID=null;
String STREET=null;
String APT_NO=null;
String CITY=null;
String STATE=null;
String ZIP=null;
Person person=null;// the person this address belongs to.
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
…//rest of get/set methods
}
New Address class
Person reference ~
See Sample 1
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Address XML 1-to-Many
…usual header
<class name="com.rhinosystemsinc.hibernate.samples_1.Address"
table="SAMPLE1_Address" dynamic-update="true">
<id name="ADDRESS_ID" column="ADDRESS_ID"
type="java.lang.Long" unsaved-value="null">
<generator class="sequence">
<param name="sequence">SAMPLE1_ADDRESS_SEQ</param>
<param name="parameters">INCREMENT BY 1 START WITH 1</param>
</generator>
</id>
<many-to-one name="person" class="com.rhinosystemsinc.hibernate.samples_1.Person"
cascade="save-update"
fetch="join"
lazy="false"
update="true"
insert="true"
column="PERSON_ID"
not-null="false"/>
…rest of properties defined
Address class
NOTICE different
definition sample of
the sequence
will always be
eagerly fetched.
~
For outer-join fetching or use 'select'
sequential select fetching. N+1 select prob.
Notice variable name in Address class
See Sample 1
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: main 1-to-Many
public static void main(String args[])
….
Address addr = new Address();
Person p = new Person();
p.setFIRST_NAME("John");
p.setLAST_NAME("Thompson");
p.setADDRESSES(new HashSet());
p.getADDRESSES().add(addr);
p.setCreated(new Date());
addr.setSTREET("12345 Easy");
addr.setCITY("Sacramento");
addr.setSTATE("CA");
addr.setORDER_POS(new Long(1));
addr.setCreated(new Date());
addr.setPerson(p);
…
sess.save(p);
tx.commit();
…
…
See Sample 1
assign person reference to address ~
Create an empty "set" and assign to Person
Add the address to the "set"
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Java Many-to-Many
public class Address {
…same as before
Set persons=null;// the person this address belongs to.
public Set getPersons()
{
return persons;
}
public void setPersons(Set persons)
{
this.persons = persons;
}
…//rest of get/set methods
}
NEW ~
See Sample 2
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: XML Many-to-Many
…usual header
<class name=...Address>
<set name="persons"
table="SAMPLE2_PERSON_ADDRESS">
<key column="ADDRESS_ID"/>
<many-to-many
class="com.rhinosystemsinc.hibernate.samples_2.Person"
column="PERSON_ID"/>
</set>
…rest of properties defined
</class>
</hibernate-mapping>
"Set persons=null" in java
See Sample 1
Intermediate M-M table
M-M definition,
referencing the Person
class by PERSON_ID
~
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Main Many-to-Many
Address addr = new Address();
Person p = new Person();
p.setFIRST_NAME("John");
p.setLAST_NAME("Thompson");
p.setADDRESSES(new HashSet());
p.getADDRESSES().add(addr);
p.setCreated(new Date());
addr.setSTREET("12345 Easy");
…
//revised! for many-to-many
addr.setPersons(new HashSet());
addr.getPersons().add(p);
//ADD NEW PERSON TO ADDRESS
Person p2=new Person();
p2.setFIRST_NAME("Martha");
p2.setLAST_NAME("Thompson");
p2.setADDRESSES(new HashSet());
p2.getADDRESSES().add(addr);
addr.getPersons().add(p2);
See Sample 1
Create empty list of Addresses
Martha also lives at the SAME address,
here we add the address to the p2 object ~
Setup empty Address and Person object
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Reference Data
• Data that doesn't change usually loaded at system
installation time. (Use 2nd
level Cache).
– Examples: STATES, CATEGORY, ZIP..etc.
• Strategy
– Setup XML class with element cache as read-only
– Setup cache for relation as read-only
– Call a method to load All read-only objects at
initialization (actually even everytime).
(for example…)
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Reference Data
…
<class name="com.rhinosystemsinc.sample.Event">
…
<many-to-one
name="CATEGORY"
class="com.rhinosystemsinc.sample.Category"
cascade="none"
update="false"
insert="false"
column="CATEGORY_ID"
not-null="true" />
</class>
For the EVENT class
which operations should be cascaded from
the parent object to the associated object
Variable name for java class
Category CATEGORY=null;
We will not update or insert from this relationship. ~
copyright 2006, rhinosystemsinc.com all rights re
Code[0]: Reference Data
…
<class name="com.rhinosystemsinc.sample.Category" mutable="false">
<cache usage="read-only" />
…
</class>
----------------------------------------------------------------------------------------------------------
In your Java code, load the objects at least once…
…
org.hibernate.Query q = sess.createQuery("from Category");
q.setCacheMode(org.hibernate.CacheMode.NORMAL);
q.setCacheable(true);
java.util.List result = q.list();
…
For the CATEGORY class
Set cache to be read-only
Also - notice no need for a reference back to Event class
Make sure your Query sets up the CACHING! (2nd
level) ~
copyright 2006, rhinosystemsinc.com all rights re
Code: Components
• Components
"Fine-grained object Model" - more Classes than Tables
– Value Type – not Entity reference
• Meaning, you want separate Java class (from the main entity)
for this property, and yet it is stored in DB with main entity
• Lets Consider ZIP_CODE as example.
• Address w/ ZipCode attribute
• ZipCode defines ZIP_CODE and ZIP_4DIGIT_CODE as fields
• Address Table has two fields in table (w/ same field names).
copyright 2006, rhinosystemsinc.com all rights re
Code: Component (example)
//JAVA ADDRESS CLASS:
public class Address {
…
ZipCode ZIP=null;
public ZipCode getZIP(){…}
public void setZIP(ZipCode ZIP)
}
//Address.hbm.xml ------------------------------------------------------------------------------------------
…usual header
<class name="com.rhinosystemsinc.hibernate.samples_3.Address"
table="SAMPLE3_Address" dynamic-update="true">
…
<component name="ZIP" class="com.rhinosystemsinc.hibernate.samples_3.ZipCode">
…
</class>
ZipCode variable, name is same as
mapping below.
Notice use of "component", with class
name ~
<component name="ZIP" class="com.rhinosystemsinc.hibernate.samples_3.ZipCode">
copyright 2006, rhinosystemsinc.com all rights re
Code: Component (example)
//Java ZipCode Class
public class ZipCode {
// Components have NO ID...they are just entirely dependent on
// containing Entity - in this case Address - in otherwords,
// we want a Java Class representation of ZipCode, but want it
// mapped into the Address table with ZIP_CODE and ZIP_4DIGIT_CODE
// as attributes of Address table.
String ZIP_CODE = "";
String ZIP_4DIGIT_CODE = "";
public String getZIP_4DIGIT_CODE(){…}
public void setZIP_4DIGIT_CODE(String ZIP_4DIGIT_CODE){…}
public String getZIP_CODE(){…}
public void setZIP_CODE(String ZIP_CODE){…}
}
NO ID
In this example: Attribute names map directly to database column names ~
Two attributes
w/
getters/setters
copyright 2006, rhinosystemsinc.com all rights re
Code: Component (example)
//SQL for Address table:
create table SAMPLE3_Address
(
ADDRESS_ID number primary key,
STREET varchar2(512),
APT_NO varchar2(64),
CITY varchar2(512),
STATE varchar2(2),
ZIP_CODE varchar2(5),
ZIP_4DIGIT_CODE varchar2(4)
);
Notice same name as
ZipCode variables. ~
copyright 2006, rhinosystemsinc.com all rights re
Code: Component (example)
//Java Main Code
Address addr = new Address();
Person p = new Person();
p.setFIRST_NAME("John");
p.setADDRESSES(new HashSet());
p.getADDRESSES().add(addr);
addr.setSTREET("12345 Easy");
…
ZipCode zip=new ZipCode();
zip.setZIP_CODE("95603");
zip.setZIP_4DIGIT_CODE("4456");
addr.setZIP(zip);
addr.setPersons(new HashSet());
addr.getPersons().add(p);
…
sess.saveOrUpdate(p);
Usual initialization here.
Create a ZipCode
object, initialize it, and
set to the Address
Note: query returns an Address object w/
ZipCode filled in automatically. ~
copyright 2006, rhinosystemsinc.com all rights re
Code: Querying
• Hibernate offers a variety of ways to query
the objects:
– SQL – straight SQL (parsed SQL 92 standard)
– HQL – hibernate's query language (SQL based)
– QBE – query by example
– Criteria Queries – Object oriented
copyright 2006, rhinosystemsinc.com all rights re
Code: Querying
• HQL simple example:
Query q = sess.createQuery("from Person");
java.util.List result = q.list();
// ALSO
Query q = sess.createQuery("from Person p where " +
"p.FIRST_NAME = :fname and " +
"p.LAST_NAME =:lname");
q.setString("fname", First); //can also use positional parameters
q.setString("lname", Last);
java.util.List result = q.list();
See all examples for quering
Can also use ? As positional param
Then use:
q.setString(0, First);
q.setString(1, Last);
Use the session to create the Query
object.
Returns a list, that you can iterate
through and cast the "objects" to
Person. ~
copyright 2006, rhinosystemsinc.com all rights re
Code: Querying
• HQL scalar example (from reference.pdf):
Iterator results = sess.createQuery(
"select cat.color, min(cat.birthdate),
count(cat) from Cat cat " +
"group by cat.color").list().iterator();
while ( results.hasNext() )
{
Object[] column = (Object[]) results.next();
Color type = (Color) column[0];
Date oldest = (Date) column[1];
Integer count = (Integer) column[2];
.....
}
See all examples for quering
Notice alias "cat" lowercase
Notice cast – same as attribute
declared in Cat class ~
copyright 2006, rhinosystemsinc.com all rights re
Code: SQL Querying
• SQL example:
org.hibernate.SQLQuery q = sess.createSQLQuery("select *
" +
"
from SAMPLE0_PERSON" +
"
where " +
"
FIRST_NAME = :fname " +
"
and LAST_NAME = :lname");
q.addEntity(Person.class);
q.setString("fname", First); //can also use
positional parameters
q.setString("lname", Last);
java.util.List result = q.list();
…
See all examples for quering
createSQLQuery
Tell hibernate about actual
class
Bind parameters ~
copyright 2006, rhinosystemsinc.com all rights re
Code: SQL Querying
• SQL example #2:
org.hibernate.SQLQuery q = sess.createSQLQuery("select
{Person.*} " +
"
from SAMPLE0_PERSON Person where " +
"
Person.FIRST_NAME = :fname " +
"
and Person.LAST_NAME =:lname");
q.addEntity("Person",Person.class);
q.setString("fname", First); //can also use
positional parameters
q.setString("lname", Last);
java.util.List result = q.list();
…See all examples for quering
See also:
q.addScalar(String columnAlias, Type type).
q.addJoin(String alias, String path). Where path is the
path to the java collection member variable name of the
parent class. In our example it would be
"Person.ADDRESSES" ~
With AliasWith AliasWith Alias
copyright 2006, rhinosystemsinc.com all rights re
Pros/Cons
• Cons
– Lots of idiosyncrasies. – caching, flushing,
inverse…etc. (But can configure/disable them.)
– Learning curve is fairly steep.
(learn a way and stick with it).
– Not good for bulk-inserts (batch processing)
(forced to use PL/SQL within hibernate or jdbc)
– Java-XML-table all defining entity (good and
bad) – (xdoclet and other tools – setup system of generating base/core
code elements – use patterns and conventions)
copyright 2006, rhinosystemsinc.com all rights re
Pros/Cons
• Pros
– Simple to understand concepts.
– Stick with tools and frameworks, then can speed up
development time.
– Caching for you – so better performance.
(if you don’t use cache, then similar to straight SQL w/ preparedstatement
performance).
– Highly extensible and customizable to suite your needs
for new development and conversion.
– Defacto standard, via OpenSource LGPL license, non
proprietary, and known by developer community
(cost, maintenance, bugs..etc.)
copyright 2006, rhinosystemsinc.com all rights re
Resources
• Hibernate.org – is the best most up-to-date
– FAQs : http://www.hibernate.org/5.html
• Advanced Problems, Tips and Tricks, Performance and more…
• Evaluation- http://www.hibernate.org/263.html
– Doc link: http://www.hibernate.org/5.html
– Migration (A MUST!) – http://www.hibernate.org/250.html
• Package change: net.sf -> org.hibernate
• SessionFactory
• Criteria Query
copyright 2006, rhinosystemsinc.com all rights re
Resources
• Hibernate installation
– <installdir>/doc/
{api,other,reference}/en/pdf/hibernate_reference.pdf (great for
reference, yet not sufficient for beginners to "learn").
– <installdir>/eg – samples
copyright 2006, rhinosystemsinc.com all rights re
Resources
• Miscellaneous
– Book: Hibernate In Action http://www.manning.com/bauer
– Performance #'s:
http://www.sourcelabs.com/?sidemenu=3&page=software&sub=sash_hibernateperftest
• Hibernate user FORUM - http://forum.hibernate.org/
(+200 msg/day)
• CaveatEmptor Sample from Hibernate:
http://caveatemptor.hibernate.org/
copyright 2006, rhinosystemsinc.com all rights re
End 0f Presentation
Thank you!
• Extra topics session follow, if time.
~ Or ~
• Q & A
• Future Questions:
email: joel@rhinosystemsinc.com
copyright 2006, rhinosystemsinc.com all rights re
XDoclet
• XDoclet is used to generate hibernate XML
files.
– You specify annotation tags in your Java code
– Run an xdoclet-ANT task on the java code
– Make sure xml is in your classpath (the
xdoclet-ant will put in src directory)
• Hibernate Annotations, next step.
See all examples of Xdoclet in the java code
copyright 2006, rhinosystemsinc.com all rights re
XDoclet example
•Person.java
Long PERSON_ID=null;
String FNAME=null;
/** @hibernate.id
* unsaved-value="null"
* generator-
class="sequence"*/
public Long getPERSON_ID(){}
public void
setPERSON_ID(Long){}
/** @hibernate.property */
public String getFNAME()
{}
public void
setFNAME(String){}
See all examples for xdoclet
• Person.hbm.xml
<id name="PERSON_ID"
column="PERSON_ID"
type="java.lang.Long"
unsaved-value="null">
<generator class="sequence">
<!– …some xdoclet
commentary…-->
</generator>
</id>
<property
name="LAST_NAME"
type="java.lang.String"
update="true"
insert="true"
column="LAST_NAME"
/>
Xdoclet Ant
task/** @hibernate.id
* unsaved-
value="null"
* generator-
class="sequence"*/
public
Long get
/** @hibernate.property */
<id name="PERSON_ID"
column="PERSON_ID"
type="java.lang.Long"
unsaved-value="null">
<generator class="sequence">
<!– …some xdoclet
commentary…-->
</generator>
</id><property
name="LAST_NAME"
type="java.lang.String"
update="true"
insert="true"
column="LAST_NAME"
/>
•Maintain tags in one location - Java
•On building (one click) create hbm.xml files
copyright 2006, rhinosystemsinc.com all rights re
Topics Not Covered
• SessionFactory
• PLSQL
• Custom UserType
• Caching (1st
& 2nd
Level)
• Transactions and
Isolation Levels
– JDBC trx semantics
– Concurrent Updates and
versioning.
• Session and Flushing
– Session "unit of work"
• Interceptors & Logging
• Definitions:
– Transient, Persistent,
Detached
• Subclass

Hibernate java and_oracle

  • 1.
    copyright 2006, rhinosystemsinc.comall rights re What we'll Cover • Introduction of HibernateV3 Configuration & Setup of Hibernate/Java • Code • Pros and Cons • RESOURCES • Q & A.
  • 2.
    copyright 2006, rhinosystemsinc.comall rights re Intro:About Hibernate • www.hibernate.org - Open Source since, and now with JBoss (since late 2003) w/ commercial support & training. Founder: Gavin King • Provides a layer for java to interact with the database. – Is technically a set of java-class libraries that you use to gain access to your database • Hibernate caches database objects – & Hibernate services your Java program 1/3
  • 3.
    copyright 2006, rhinosystemsinc.comall rights re Intro:About Hibernate • Hibernate layer resides in your JVM. • Improves performance – objects are cached in JVM and mapped to your object model. • Used in commercial applications – JBoss for one (yes, another open source…) – Many more opensource and commercial listed at: http://www.hibernate.org/27.html 2/3
  • 4.
    copyright 2006, rhinosystemsinc.comall rights re Intro:About Hibernate • Hibernate is highly customizable – Caching (2nd level cache). – Database dialects – Transaction – Connection pooling – Custom Types • Or use out of box (recommend changing: connection pooling). 3/3
  • 5.
    copyright 2006, rhinosystemsinc.comall rights re Intro:Importance of DAO • DAO: Data Access Objects – design/pattern • Purpose is to abstract your calls to the database. – Don't put SQL directly into your Java/JSP. • You can change our database with minimal affect on your code • Identify bottle necks and bugs easier. 1/2
  • 6.
    copyright 2006, rhinosystemsinc.comall rights re Intro:Importance of DAO • Better design translates into easier to understand code. • Hibernate DAO - 3rd party technology – Benefits of common knowledge – Fixes to technology – Specialized for each database and optimized for database access (caveats). 2/2
  • 7.
    copyright 2006, rhinosystemsinc.comall rights re Intro:Comparison to SQL • Problem with SQL – never lived up to promise of standardization amongst database vendors – Uses Jdbc to access database – (no forced design) – Is Relational • With Hibernate: – Caching – Easier to code – Standard access – Is Object Oriented and maps to Relational. 1/2
  • 8.
    copyright 2006, rhinosystemsinc.comall rights re Intro:Comparison to SQL • Speed/performance – Prepared Statements and caching (hib) – Can batch process many DML statements. (hib) – Better performance from PL/SQL (sql/hib) • Maintenance/Updates (hib) • Legacy system (depends) – Using hibernate with legacy database systems & Legacy java code retrofit with Hibernate 2/2
  • 9.
    copyright 2006, rhinosystemsinc.comall rights re Configuration Hibernate/Java • Download and install JDK 1.4 or 1.5 • Download version3 from www.hibernate.org • Setup Hibernate's Jars into your project's classpath. – The hibernate3.jar into your project's classpath; and if need be all the <hib>/lib (if you don't already have them). 1/5
  • 10.
    copyright 2006, rhinosystemsinc.comall rights re Config:hibernate.properties • Make sure that Oracle's client jdbc lib is in your CLASSPATH • Hibernate.properties needs to be in your project's & runtime classpath. HIBERNATE.PROPERTIES #for OCI (local instance of Oracle, not using tnsnames.ora). hibernate.dialect=org.hibernate.dialect.OracleDialect hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver hibernate.connection.username=joel hibernate.connection.password=xyz hibernate.connection.pool_size=5 hibernate.connection.url=jdbc:oracle:oci:@ hibernate.show_sql=true 2/5
  • 11.
    copyright 2006, rhinosystemsinc.comall rights re Config: Download & Config Xdoclet [OPTIONAL] • In order to run the "generation" of hibernate XML you need to setup your classpath to include Xdoclet libraries • Download latest (v1.2.3) from http://xdoclet.sourceforge.net/xdoclet/index.html (www.xdoclet.org?) – Download direct from http://sourceforge.net/project/showfiles.php?group_id=31602 – Tag References http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html 3/5
  • 12.
    copyright 2006, rhinosystemsinc.comall rights re config: Download & Config ANT [OPTIONAL] • In order to use ANT you'll need to setup your PATH to include ANT. Check – run "ant" • Download latest (v1.6.5) from http://ant.apache.org/ (www.ant.org?) – Download direct from http://www.axint.net/apache/ant/binaries/apache-ant-1.6.5-bin.zip – Unzip and make sure "ant" is in your path. (right-click my computer -> properties ->Advanced->Environmental Variables -> update PATH with directory <ant_install>/bin) 4/5
  • 13.
    copyright 2006, rhinosystemsinc.comall rights re config: Download & Config ANT • SUMMARY: – JDK [required]: PATH,CLASSPATH – HIBERNATE [required]: CLASSPATH • ORACLE or DBMS client [required]: CLASSPATH (connection/lib info w/ hibernate.properties) – XDOCLET [optional]:CLASSPATH – ANT [optional]:CLASSPATH 5/5
  • 14.
    copyright 2006, rhinosystemsinc.comall rights re Code & Fragments • What we'll cover in the CODE section. For each of the items below you'll see the XML, the DDL SQL, Java code and an explanation. –Single Entity –Primary Keys –Many-to-One –Many-to-Many –DML (Query, Insert, Update, Delete) –Extra topics
  • 15.
    copyright 2006, rhinosystemsinc.comall rights re Code: Overview • Hibernate layer is independent of your code • Entities map via hibernate XML • Hibernate & you: DML, caching, isolation levels
  • 16.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Java POJO Entity public class Person { Long PERSON_ID=null; //manufactured surrogate key String FIRST_NAME=null; String LAST_NAME=null; public Long getPERSON_ID(){…} public void setPERSON_ID(String) {…} public String getFIRST_NAME(){…} public void setFIRST_NAME(String) {…} public String getLAST_NAME(){…} public void setLAST_NAME (String) {…} } See SAMPLE 0 1/4 Standard get/set & need empty constructor ~
  • 17.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: XML for Entity <!--Must be named: Person.hbm.xml and reside in CLASSPATH--> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.rhinosystemsinc.hibernate.samples_0.Person" table="SAMPLE0_Person" > <id name="PERSON_ID" column="PERSON_ID" type="java.lang.Long" unsaved-value="null"> <generator class="hilo"> <param name="table">SAMPLE0_PERSON_SEQ</param> <param name="column">NEXT</param> </generator> </id> <property name="FIRST_NAME" type="java.lang.String" update="true" insert="true" column="FIRST_NAME" not-null="true" unique="false" length="100" /> <property name="LAST_NAME"/> </class> </hibernate-mapping> 2/4 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <id name="PERSON_ID" column="PERSON_ID" type="java.lang.Long" unsaved-value="null"> <generator class="hilo"> <param name="table">SAMPLE0_PERSON_SEQ</param> <param name="column">NEXT</param> </generator> </id> Define the manufactured surrogate primary key, based on SEQUENCE. Also natural composite primary keys: <composite-id> <key-property name="name"/> <key-property name="ssn"/> </composite-id> <property name="FIRST_NAME" type="java.lang.String" update="true" insert="true" column="FIRST_NAME" not-null="true" unique="false" length="100" /> <property name="LAST_NAME"/>
  • 18.
    copyright 2006, rhinosystemsinc.comall rights re Code: Primary Keys • All hibernate persisted Entity mappings must have a primary key. • Suggestion: Use surrogate generated keys. – Hib supports natural keys ( & multi-column). – Reason: • Sometimes (rarely) keys will change values • Easier to manage 3/4
  • 19.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Java Main Class //SAVING A NEW OBJECT org.hibernate.Session sess = sessFact.openSession(); //more on SessionFactory in a minute. Person p = new Person(); p.setFIRST_NAME("John"); p.setLAST_NAME("Smith"); Transaction tx = sess.beginTransaction(); sess.saveOrUpdate(p); tx.commit(); sess.close(); See SAMPLE 0 At this point, what if we do an update, via SQLPLUS? 4/4
  • 20.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Java 1-to-Many public class Person { Long PERSON_ID=null; //manufactured surrogate key String FIRST_NAME=null; String LAST_NAME=null; Set ADDRESSES=null; public Set getADDRESSES() { return ADDRESSES; } public void setADDRESSES(Set ADDRESSES) { this.ADDRESSES = ADDRESSES; } …//rest of get/set methods } NEW
  • 21.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Person XML 1-to-Many … <hibernate-mapping> <class name="com.rhinosystemsinc.hibernate.samples_0.Person" table="SAMPLE0_Person" > … <set name="ADDRESSES" lazy="false" inverse="true" cascade="save-update" sort="unsorted" outer-join="true"> <key column="PERSON_ID"/> <one-to-many class="com.rhinosystemsinc.hibernate.samples_1.Address"/> </set> … </class> </hibernate-mapping> NEW Any records that don't have address will be shown too. all or save-update-delete Notice one-to-many ~
  • 22.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Java 1-to-Many public class Address { Long ADDRESS_ID=null; String STREET=null; String APT_NO=null; String CITY=null; String STATE=null; String ZIP=null; Person person=null;// the person this address belongs to. public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } …//rest of get/set methods } New Address class Person reference ~ See Sample 1
  • 23.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Address XML 1-to-Many …usual header <class name="com.rhinosystemsinc.hibernate.samples_1.Address" table="SAMPLE1_Address" dynamic-update="true"> <id name="ADDRESS_ID" column="ADDRESS_ID" type="java.lang.Long" unsaved-value="null"> <generator class="sequence"> <param name="sequence">SAMPLE1_ADDRESS_SEQ</param> <param name="parameters">INCREMENT BY 1 START WITH 1</param> </generator> </id> <many-to-one name="person" class="com.rhinosystemsinc.hibernate.samples_1.Person" cascade="save-update" fetch="join" lazy="false" update="true" insert="true" column="PERSON_ID" not-null="false"/> …rest of properties defined Address class NOTICE different definition sample of the sequence will always be eagerly fetched. ~ For outer-join fetching or use 'select' sequential select fetching. N+1 select prob. Notice variable name in Address class See Sample 1
  • 24.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: main 1-to-Many public static void main(String args[]) …. Address addr = new Address(); Person p = new Person(); p.setFIRST_NAME("John"); p.setLAST_NAME("Thompson"); p.setADDRESSES(new HashSet()); p.getADDRESSES().add(addr); p.setCreated(new Date()); addr.setSTREET("12345 Easy"); addr.setCITY("Sacramento"); addr.setSTATE("CA"); addr.setORDER_POS(new Long(1)); addr.setCreated(new Date()); addr.setPerson(p); … sess.save(p); tx.commit(); … … See Sample 1 assign person reference to address ~ Create an empty "set" and assign to Person Add the address to the "set"
  • 25.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Java Many-to-Many public class Address { …same as before Set persons=null;// the person this address belongs to. public Set getPersons() { return persons; } public void setPersons(Set persons) { this.persons = persons; } …//rest of get/set methods } NEW ~ See Sample 2
  • 26.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: XML Many-to-Many …usual header <class name=...Address> <set name="persons" table="SAMPLE2_PERSON_ADDRESS"> <key column="ADDRESS_ID"/> <many-to-many class="com.rhinosystemsinc.hibernate.samples_2.Person" column="PERSON_ID"/> </set> …rest of properties defined </class> </hibernate-mapping> "Set persons=null" in java See Sample 1 Intermediate M-M table M-M definition, referencing the Person class by PERSON_ID ~
  • 27.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Main Many-to-Many Address addr = new Address(); Person p = new Person(); p.setFIRST_NAME("John"); p.setLAST_NAME("Thompson"); p.setADDRESSES(new HashSet()); p.getADDRESSES().add(addr); p.setCreated(new Date()); addr.setSTREET("12345 Easy"); … //revised! for many-to-many addr.setPersons(new HashSet()); addr.getPersons().add(p); //ADD NEW PERSON TO ADDRESS Person p2=new Person(); p2.setFIRST_NAME("Martha"); p2.setLAST_NAME("Thompson"); p2.setADDRESSES(new HashSet()); p2.getADDRESSES().add(addr); addr.getPersons().add(p2); See Sample 1 Create empty list of Addresses Martha also lives at the SAME address, here we add the address to the p2 object ~ Setup empty Address and Person object
  • 28.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Reference Data • Data that doesn't change usually loaded at system installation time. (Use 2nd level Cache). – Examples: STATES, CATEGORY, ZIP..etc. • Strategy – Setup XML class with element cache as read-only – Setup cache for relation as read-only – Call a method to load All read-only objects at initialization (actually even everytime). (for example…)
  • 29.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Reference Data … <class name="com.rhinosystemsinc.sample.Event"> … <many-to-one name="CATEGORY" class="com.rhinosystemsinc.sample.Category" cascade="none" update="false" insert="false" column="CATEGORY_ID" not-null="true" /> </class> For the EVENT class which operations should be cascaded from the parent object to the associated object Variable name for java class Category CATEGORY=null; We will not update or insert from this relationship. ~
  • 30.
    copyright 2006, rhinosystemsinc.comall rights re Code[0]: Reference Data … <class name="com.rhinosystemsinc.sample.Category" mutable="false"> <cache usage="read-only" /> … </class> ---------------------------------------------------------------------------------------------------------- In your Java code, load the objects at least once… … org.hibernate.Query q = sess.createQuery("from Category"); q.setCacheMode(org.hibernate.CacheMode.NORMAL); q.setCacheable(true); java.util.List result = q.list(); … For the CATEGORY class Set cache to be read-only Also - notice no need for a reference back to Event class Make sure your Query sets up the CACHING! (2nd level) ~
  • 31.
    copyright 2006, rhinosystemsinc.comall rights re Code: Components • Components "Fine-grained object Model" - more Classes than Tables – Value Type – not Entity reference • Meaning, you want separate Java class (from the main entity) for this property, and yet it is stored in DB with main entity • Lets Consider ZIP_CODE as example. • Address w/ ZipCode attribute • ZipCode defines ZIP_CODE and ZIP_4DIGIT_CODE as fields • Address Table has two fields in table (w/ same field names).
  • 32.
    copyright 2006, rhinosystemsinc.comall rights re Code: Component (example) //JAVA ADDRESS CLASS: public class Address { … ZipCode ZIP=null; public ZipCode getZIP(){…} public void setZIP(ZipCode ZIP) } //Address.hbm.xml ------------------------------------------------------------------------------------------ …usual header <class name="com.rhinosystemsinc.hibernate.samples_3.Address" table="SAMPLE3_Address" dynamic-update="true"> … <component name="ZIP" class="com.rhinosystemsinc.hibernate.samples_3.ZipCode"> … </class> ZipCode variable, name is same as mapping below. Notice use of "component", with class name ~ <component name="ZIP" class="com.rhinosystemsinc.hibernate.samples_3.ZipCode">
  • 33.
    copyright 2006, rhinosystemsinc.comall rights re Code: Component (example) //Java ZipCode Class public class ZipCode { // Components have NO ID...they are just entirely dependent on // containing Entity - in this case Address - in otherwords, // we want a Java Class representation of ZipCode, but want it // mapped into the Address table with ZIP_CODE and ZIP_4DIGIT_CODE // as attributes of Address table. String ZIP_CODE = ""; String ZIP_4DIGIT_CODE = ""; public String getZIP_4DIGIT_CODE(){…} public void setZIP_4DIGIT_CODE(String ZIP_4DIGIT_CODE){…} public String getZIP_CODE(){…} public void setZIP_CODE(String ZIP_CODE){…} } NO ID In this example: Attribute names map directly to database column names ~ Two attributes w/ getters/setters
  • 34.
    copyright 2006, rhinosystemsinc.comall rights re Code: Component (example) //SQL for Address table: create table SAMPLE3_Address ( ADDRESS_ID number primary key, STREET varchar2(512), APT_NO varchar2(64), CITY varchar2(512), STATE varchar2(2), ZIP_CODE varchar2(5), ZIP_4DIGIT_CODE varchar2(4) ); Notice same name as ZipCode variables. ~
  • 35.
    copyright 2006, rhinosystemsinc.comall rights re Code: Component (example) //Java Main Code Address addr = new Address(); Person p = new Person(); p.setFIRST_NAME("John"); p.setADDRESSES(new HashSet()); p.getADDRESSES().add(addr); addr.setSTREET("12345 Easy"); … ZipCode zip=new ZipCode(); zip.setZIP_CODE("95603"); zip.setZIP_4DIGIT_CODE("4456"); addr.setZIP(zip); addr.setPersons(new HashSet()); addr.getPersons().add(p); … sess.saveOrUpdate(p); Usual initialization here. Create a ZipCode object, initialize it, and set to the Address Note: query returns an Address object w/ ZipCode filled in automatically. ~
  • 36.
    copyright 2006, rhinosystemsinc.comall rights re Code: Querying • Hibernate offers a variety of ways to query the objects: – SQL – straight SQL (parsed SQL 92 standard) – HQL – hibernate's query language (SQL based) – QBE – query by example – Criteria Queries – Object oriented
  • 37.
    copyright 2006, rhinosystemsinc.comall rights re Code: Querying • HQL simple example: Query q = sess.createQuery("from Person"); java.util.List result = q.list(); // ALSO Query q = sess.createQuery("from Person p where " + "p.FIRST_NAME = :fname and " + "p.LAST_NAME =:lname"); q.setString("fname", First); //can also use positional parameters q.setString("lname", Last); java.util.List result = q.list(); See all examples for quering Can also use ? As positional param Then use: q.setString(0, First); q.setString(1, Last); Use the session to create the Query object. Returns a list, that you can iterate through and cast the "objects" to Person. ~
  • 38.
    copyright 2006, rhinosystemsinc.comall rights re Code: Querying • HQL scalar example (from reference.pdf): Iterator results = sess.createQuery( "select cat.color, min(cat.birthdate), count(cat) from Cat cat " + "group by cat.color").list().iterator(); while ( results.hasNext() ) { Object[] column = (Object[]) results.next(); Color type = (Color) column[0]; Date oldest = (Date) column[1]; Integer count = (Integer) column[2]; ..... } See all examples for quering Notice alias "cat" lowercase Notice cast – same as attribute declared in Cat class ~
  • 39.
    copyright 2006, rhinosystemsinc.comall rights re Code: SQL Querying • SQL example: org.hibernate.SQLQuery q = sess.createSQLQuery("select * " + " from SAMPLE0_PERSON" + " where " + " FIRST_NAME = :fname " + " and LAST_NAME = :lname"); q.addEntity(Person.class); q.setString("fname", First); //can also use positional parameters q.setString("lname", Last); java.util.List result = q.list(); … See all examples for quering createSQLQuery Tell hibernate about actual class Bind parameters ~
  • 40.
    copyright 2006, rhinosystemsinc.comall rights re Code: SQL Querying • SQL example #2: org.hibernate.SQLQuery q = sess.createSQLQuery("select {Person.*} " + " from SAMPLE0_PERSON Person where " + " Person.FIRST_NAME = :fname " + " and Person.LAST_NAME =:lname"); q.addEntity("Person",Person.class); q.setString("fname", First); //can also use positional parameters q.setString("lname", Last); java.util.List result = q.list(); …See all examples for quering See also: q.addScalar(String columnAlias, Type type). q.addJoin(String alias, String path). Where path is the path to the java collection member variable name of the parent class. In our example it would be "Person.ADDRESSES" ~ With AliasWith AliasWith Alias
  • 41.
    copyright 2006, rhinosystemsinc.comall rights re Pros/Cons • Cons – Lots of idiosyncrasies. – caching, flushing, inverse…etc. (But can configure/disable them.) – Learning curve is fairly steep. (learn a way and stick with it). – Not good for bulk-inserts (batch processing) (forced to use PL/SQL within hibernate or jdbc) – Java-XML-table all defining entity (good and bad) – (xdoclet and other tools – setup system of generating base/core code elements – use patterns and conventions)
  • 42.
    copyright 2006, rhinosystemsinc.comall rights re Pros/Cons • Pros – Simple to understand concepts. – Stick with tools and frameworks, then can speed up development time. – Caching for you – so better performance. (if you don’t use cache, then similar to straight SQL w/ preparedstatement performance). – Highly extensible and customizable to suite your needs for new development and conversion. – Defacto standard, via OpenSource LGPL license, non proprietary, and known by developer community (cost, maintenance, bugs..etc.)
  • 43.
    copyright 2006, rhinosystemsinc.comall rights re Resources • Hibernate.org – is the best most up-to-date – FAQs : http://www.hibernate.org/5.html • Advanced Problems, Tips and Tricks, Performance and more… • Evaluation- http://www.hibernate.org/263.html – Doc link: http://www.hibernate.org/5.html – Migration (A MUST!) – http://www.hibernate.org/250.html • Package change: net.sf -> org.hibernate • SessionFactory • Criteria Query
  • 44.
    copyright 2006, rhinosystemsinc.comall rights re Resources • Hibernate installation – <installdir>/doc/ {api,other,reference}/en/pdf/hibernate_reference.pdf (great for reference, yet not sufficient for beginners to "learn"). – <installdir>/eg – samples
  • 45.
    copyright 2006, rhinosystemsinc.comall rights re Resources • Miscellaneous – Book: Hibernate In Action http://www.manning.com/bauer – Performance #'s: http://www.sourcelabs.com/?sidemenu=3&page=software&sub=sash_hibernateperftest • Hibernate user FORUM - http://forum.hibernate.org/ (+200 msg/day) • CaveatEmptor Sample from Hibernate: http://caveatemptor.hibernate.org/
  • 46.
    copyright 2006, rhinosystemsinc.comall rights re End 0f Presentation Thank you! • Extra topics session follow, if time. ~ Or ~ • Q & A • Future Questions: email: joel@rhinosystemsinc.com
  • 47.
    copyright 2006, rhinosystemsinc.comall rights re XDoclet • XDoclet is used to generate hibernate XML files. – You specify annotation tags in your Java code – Run an xdoclet-ANT task on the java code – Make sure xml is in your classpath (the xdoclet-ant will put in src directory) • Hibernate Annotations, next step. See all examples of Xdoclet in the java code
  • 48.
    copyright 2006, rhinosystemsinc.comall rights re XDoclet example •Person.java Long PERSON_ID=null; String FNAME=null; /** @hibernate.id * unsaved-value="null" * generator- class="sequence"*/ public Long getPERSON_ID(){} public void setPERSON_ID(Long){} /** @hibernate.property */ public String getFNAME() {} public void setFNAME(String){} See all examples for xdoclet • Person.hbm.xml <id name="PERSON_ID" column="PERSON_ID" type="java.lang.Long" unsaved-value="null"> <generator class="sequence"> <!– …some xdoclet commentary…--> </generator> </id> <property name="LAST_NAME" type="java.lang.String" update="true" insert="true" column="LAST_NAME" /> Xdoclet Ant task/** @hibernate.id * unsaved- value="null" * generator- class="sequence"*/ public Long get /** @hibernate.property */ <id name="PERSON_ID" column="PERSON_ID" type="java.lang.Long" unsaved-value="null"> <generator class="sequence"> <!– …some xdoclet commentary…--> </generator> </id><property name="LAST_NAME" type="java.lang.String" update="true" insert="true" column="LAST_NAME" /> •Maintain tags in one location - Java •On building (one click) create hbm.xml files
  • 49.
    copyright 2006, rhinosystemsinc.comall rights re Topics Not Covered • SessionFactory • PLSQL • Custom UserType • Caching (1st & 2nd Level) • Transactions and Isolation Levels – JDBC trx semantics – Concurrent Updates and versioning. • Session and Flushing – Session "unit of work" • Interceptors & Logging • Definitions: – Transient, Persistent, Detached • Subclass