Professional Documents
Culture Documents
http://www.acornbay.com
http://www.acornbay.com http://www.acornbay.com
1
Technologies in the “objects
RDBMS support
to database” space
• Standardized API Tested with:
• DB2 7.1, 7.2;
•
•
Mckoi SQL 0.93
Progress 9
– JDO implementations • MySQL 3.23; • Pointbase Embedded 4.3
– J2EE CMP • PostgreSQL 7.1.2, 7.2, 7.3; • SAP DB 7.3
• Oracle 8i, 9i;
– OJB • Sybase 12.5 (JConnect
(JConnect 5.5); Thought to be compatible with
• Proprietary API • Interbase 6.0.1 (Open Source)
with Firebird InterClient 2.01;
the latest versions of:
• Informix
– TopLink • HypersonicSQL 1.61, 1.7.0; • Ingres
– CocoBase O/R • Microsoft SQL Server 2000; • FrontBase
– Hibernate
http://www.acornbay.com http://www.acornbay.com
• IDE plug-in support: Eclipse, IDEA public void setCreditCard(CreditCard cc) { _creditCard
public CreditCard getCreditCard()
_creditCard = cc; }
getCreditCard() { return _creditCard
_creditCard;
; }
// and so on
http://www.acornbay.com http://www.acornbay.com
2
Rules for your class Creating the mapping file
• Must have Bean-
Bean-style getXXX()
getXXX() and setXXX()
setXXX() • Defining a class
• Must have default constructor
– <class>
• Not final
• Identifier recommended (not required) – <id>
– any primitive type – <generator>
– any primitive wrapper
– <property>
– String
– Date • Defining relationships
– user-
user-defined class (good for existing tables with
composite keys) • Tools support
http://www.acornbay.com http://www.acornbay.com
<class> <id>
<class name=“name.of.class
name=“name.of.class”
” can be an interface <class name=“xpetstore.domain.Customer
name=“xpetstore.domain.Customer”” table=“T_CUSTOMER”>
table=“name_of_table
table=“name_of_table”
” default: class name <id name="userId
name="userId"
" length="10">
schema=“database_schema
schema=“database_schema”” default: none <generator class="assigned“ />
proxy=“name.of.proxy.class
proxy=“name.of.proxy.class”” default: no proxy i/f </id>
discrimator-
discrimator-value=“val
value=“val”
” default: class name </class>
mutable=“true”
polymorphism=“implicit”
dynamic-
dynamic-update=“false”
dynamic-
dynamic-insert=“false” Additional <id> attributes:
persister=“
persister=“PersisterClass
PersisterClass”>
”>
type=“typename
type=“typename”
”
...
column=“column_name
column=“column_name”
”
unsaved-
unsaved-value=“something” options: any, none, null, value
</class>
http://www.acornbay.com http://www.acornbay.com
3
<generator> options <property>
<class name=“xpetstore.domain.Customer
name=“xpetstore.domain.Customer”” table=“T_CUSTOMER”>
<id name="userId
name="userId"
" length="10">
Class Description Types
<generator class="assigned“ />
vm Intra-
Intra-VM identifier long, short, int </id>
identity DB-
DB-supported identity column long, short, int <property name=“email” length=“100” not-
not-null=“false”
unique=“false”/>
sequence DB-
DB-supported sequence long, short, int ...
hilo Database-
Database-unique values long, short, int </class>
seqhilo hi/lo using sequences long, short, int
uuid.hex 128 bit UUID algorithm incorporating IP 32 digit hex string
address Additional <property> attributes:
uuid.string same 16 character ASCII string
type=“typename
type=“typename”
”
native Varies best of identity, sequence, or
column=“column_name
column=“column_name”
”
hilo,
hilo, based on database
update=“true”
capabilities
insert=“true”
assigned Application assigned Any supported type
foreign Uses identifier of associated object, for one-
one-to-
to- Any supported type
one associations
http://www.acornbay.com http://www.acornbay.com
4
One-to-one mapping One-to-many mapping
Java usage:
Java usage:
Set myBars = Foo.getBars();
Foo.getBars();
Bar myBar = Foo.getBar();
Foo.getBar();
Mapping file:
Mapping file:
<class name=“Foo
name=“Foo”>
”>
<class name=“Foo
name=“Foo”>
”> ...
... <set role=“bars” table=“bar” inverse=“true” lazy=“true”>
<one-
<one-to-
to-one name=“bar” class=“Bar” /> <key column-
column-”foo_id”
foo_id” />
</class> <one-
<one-to-
to-many class=“Bar”>
</set>
Schema: </class>
Schema:
Foo Bar
id id Foo Bar
id Id foo_id
http://www.acornbay.com http://www.acornbay.com
<class name=“Foo
name=“Foo”>
”> <class name=“Foo
name=“Foo”>
”>
... ...
<set role=“bars” table=“Foo_Bar
table=“Foo_Bar”>
”> <set role=“people” table=“Person”>
<key column=”foo_id
column=”foo_id”
” /> <key column=”foo_id
column=”foo_id”
” />
<many-
<many-to-
to-many column=“bar_id
column=“bar_id”
” class=“Bar”> <element column=“name” type=“string”>
</set> </set>
</class> </class>
Schema: Schema:
5
Map Creating the mapping file
Java usage:
• Defining a class ;
• Defining relationships ;
Map myAges = Foo.getAges();
Foo.getAges();
Mapping file:
<class name=“Foo
name=“Foo”>
”> • Tools support
...
<map role=“ages” table=“Ages”>
<key column=”id” />
<index column=“name” type=“string” />
<element column=“age” type=“string”>
</map>
</class>
Schema:
Foo Ages
id foo_id name age
http://www.acornbay.com http://www.acornbay.com
hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username SCOTT
hibernate.connection.password TIGER
hibernate.connection.url jdbc:oracle:thin:@localhost:1521:test
hibernate.show_sql false
http://www.acornbay.com http://www.acornbay.com
6
Manipulating objects Initialization
• Initialization
// Loading the configuration
// Looks for mapping files in same classpath location as class
http://www.acornbay.com http://www.acornbay.com
// Later sess.delete(customer);
sess.delete(customer);
// Loading an object
// Deleting many objects
// If we know the ID
Customer customer = (Customer) sess.load(Customer.class,
sess.load(Customer.class, id); sess.delete(“from customer as Customer where amount_purchased < ?”,
new Double(10.0),
// Querying Hibernate.DOUBLE);
Hibernate.DOUBLE);
List customers = sess.find(
sess.find(
“from Customer as customer where customer.email = ?”,
email,
Hibernate.STRING);
Hibernate.STRING);
http://www.acornbay.com http://www.acornbay.com
7
Updating stale objects Filtering results
// Updating objects from a previous session // Filtering query results
// Somewhere else in the application after s1 is defunct // Give me them in customer.name order
customer.setAccount(newAccount);
customer.setAccount(newAccount); List sortedUsers = sess.filter(customers,
sess.filter(customers, “order by this.name”);
this.name”);
// Second session // Give me only those who owe more than 100.00
s2.update(customer); List lotsDueFromThem = sess.filter(customers,
sess.filter(customers,
s2.update(newAccount); “where this.account_balance > 100”);
http://www.acornbay.com http://www.acornbay.com
http://www.acornbay.com http://www.acornbay.com
8
Extension points Other useful interfaces
http://www.acornbay.com http://www.acornbay.com
http://www.acornbay.com http://www.acornbay.com
9
HQL vs JDOQL Wrap up
http://www.acornbay.com http://www.acornbay.com
Presentation Sources
http://www.acornbay.com
10