Java Reference
In-Depth Information
<property
<property name=
"hibernate.connection.url"
value=
"jdbc:hsqldb:mem:jpademo.db"
//>
<property
<property name=
"hibernate.connection.username"
value=
"sa"
//>
<property
<property name=
"hibernate.connection.password"
value=
""
//>
<property
<property name=
"hibernate.transaction.factory.class"
value=
"org.hibernate.transaction.JTATransactionFactory"
//>
<property
<property name=
"hibernate.dialect"
value=
"org.hibernate.dialect.HSQLDialect"
//>
<!-- Properties for other providers -->
</properties>
</persistence-unit>
</properties>
</persistence-unit>
Once all these pieces are in place, the program in
Example 18-1
can be run. The data objects
are saved to disk and displayed by the program.
Example 18-2
shows the exact same example, using the exact same annotated entity classes,
using the Hibernate API.
Example 18-2. HibernateSimple
public
public class
HibernateSimple
{
@SuppressWarnings
(
"unchecked"
)
public
class
HibernateSimple
public static
static
void
void
main
(
String
[]
args
) {
System
.
out
.
println
(
"HibernateSimple.main()"
);
Configuration cf
=
new
new
AnnotationConfiguration
();
cf
.
configure
();
SessionFactory sf
=
null
null
;
Session session
=
null
null
;
try
try
{
sf
=
cf
.
buildSessionFactory
();
session
=
sf
.
openSession
();
Transaction tx
=
session
.
beginTransaction
();
// Create an entity in the database.
Person np
=
new
new
Person
(
"Tom"
,
"Boots"
);
System
.
out
.
println
(
np
);
session
.
save
(
np
);
tx
.
commit
();
int
int
id
=
np
.
getId
();
System
.
out
.
println
(
"Created Person with Id "
+
id
);