This link has been bookmarked by 66 people . It was first bookmarked on 08 Aug 2006, by Steven Van Vooren.
-
18 Jun 17
kevinoempty
-
15 Sep 10
-
29 Dec 09
-
24 Feb 09
-
18 Aug 08
-
06 Aug 08
-
29 Jul 08
-
The id property holds a unique identifier value for a particular event. All persistent entity classes (there are less important dependent classes as well) will need such an identifier property if we want to use the full feature set of Hibernate. In fact, most applications (esp. web applications) need to distinguish objects by identifier, so you should consider this a feature rather than a limitation. However, we usually don't manipulate the identity of an object, hence the setter method should be private. Only Hibernate will assign identifiers when an object is saved. You can see that Hibernate can access public, private, and protected accessor methods, as well as (public, private, protected) fields directly. The choice is up to you and you can match it to fit your application design.
-
The id property holds a unique identifier value for a particular event. All persistent entity classes (there are less important dependent classes as well) will need such an identifier property if we want to use the full feature set of Hibernate. In fact, most applications (esp. web applications) need to distinguish objects by identifier, so you should consider this a feature rather than a limitation. However, we usually don't manipulate the identity of an object, hence the setter method should be private. Only Hibernate will assign identifiers when an object is saved. You can see that Hibernate can access public, private, and protected accessor methods, as well as (public, private, protected) fields directly. The choice is up to you and you can match it to fit your application design.
The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you, using Java Reflection. The constructor can be private, however, package visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation.
-
Note that the Hibernate DTD is very sophisticated. You can use it for auto-completion of XML mapping elements and attributes in your editor or IDE. You also should open up the DTD file in your text editor - it's the easiest way to get an overview of all elements and attributes and to see the defaults, as well as some comments. Note that Hibernate will not load the DTD file from the web, but first look it up from the classpath of the application. The DTD file is included in hibernate3.jar as well as in the src/ directory of the Hibernate distribution.
-
The id element is the declaration of the identifer property, name="id" declares the name of the Java property - Hibernate will use the getter and setter methods to access the property. The column attribute tells Hibernate which column of the EVENTS table we use for this primary key. The nested generator element specifies the identifier generation strategy, in this case we used native, which picks the best strategy depending on the configured database (dialect). Hibernate supports database generated, globally unique, as well as application assigned identifiers (or any strategy you have written an extension for).
-
Finally we include declarations for the persistent properties of the class in the mapping file. By default, no properties of the class are considered persistent:
-
Without the column attribute Hibernate by default uses the property name as the column name. This works fine for title. However, date is a reserved keyword in most database, so we better map it to a different name.
-
The next interesting thing is that the title mapping also lacks a type attribute. The types we declare and use in the mapping files are not, as you might expect, Java data types. They are also not SQL database types. These types are so called Hibernate mapping types, converters which can translate from Java to SQL data types and vice versa. Again, Hibernate will try to determine the correct conversion and mapping type itself if the type attribute is not present in the mapping. In some cases this automatic detection (using Reflection on the Java class) might not have the default you expect or need. This is the case with the date property. Hibernate can't know if the property (which is of java.util.Date) should map to a SQL date, timestamp, or time column. We preserve full date and time information by mapping the property with a timestamp converter.
-
-
14 Jul 08
-
place all required libraries found in /lib into into the /lib directory of your new development working directory
-
minimum set of required libraries
-
JavaBean naming conventions
-
Hibernate can also access fields directly
-
recommended design - but not required.
-
Java Reflection
-
no-argument constructor is a requirement for all persistent classes
-
what table in the database it has to access, and what columns in that table it should use
-
-
22 May 08
-
19 May 08
-
The no-argument constructor is required to instantiate an object of this class through reflection.
-
All persistent entity classes (there are less important dependent classes as well) will need such an identifier property if we want to use the full feature set of Hibernate
-
setter method should be private
-
The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you, using Java Reflection. T
-
-
08 Apr 08
-
20 Mar 08
-
12 Mar 08
-
Hibernate needs to know how to load and store objects of the persistent class. This is where the Hibernate mapping file comes into play. The mapping file tells Hibernate what table in the database it has to
-
-
14 Feb 08
-
25 Nov 07
-
package util; import org.hibernate.*; import org.hibernate.cfg.*; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
-
This class does not only produce the global SessionFactory in its static initializer
-
-
16 Nov 07
-
23 Sep 07
-
Copy this file into the source directory, so it will end up in the root of the classpath.
-
Hibernate's automatic session management for persistence contexts will come in handy as you will soon see
-
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property>
-
If you have several databases, use several <session-factory> configurations, usually in several configuration files (for easier startup).
-
hbm2ddl.auto option turns on automatic generation of database schemas - directly into the database.
-
<session-factory>
-
"connection.password">
-
"connection.username"
-
Event.hbm.xml
-
Hibernate will try to determine the correct conversion and mapping type itself if the type attribute is not present in the mapping
-
types we declare and use in the mapping files are not, as you might expect, Java data types. They are also not SQL database types
-
Hibernate mapping types,
-
title mapping also lacks a type attribute
-
Without the column attribute Hibernate by default uses the property name as the column name
-
column attribute
-
declarations for the persistent properties of the class in the mapping file.
-
name attribute of the property element tells Hibernate which getter and setter methods to use.
-
name="id" declares the name of the Java property
-
ative, which picks the best strategy depending on the configured database (dialect)
-
we don't want to care about handling this identifier, we configure Hibernate's > identifier generation strategy for a surrogate primary key column: >
-
<generator class="native"/>
-
how to load and store objects of the persistent class.
-
directory called src in the development folder
-
The no-argument constructor is a requirement for all persistent classes; Hibernate > has to create objects for you, using Java Reflection >
-
Only Hibernate will assign identifiers when an object is saved
-
will need such an identifier property if we want to use the full feature set of Hibernate
-
t. All persistent entity classes
-
The no-argument constructor is required to instantiate an object of this class through reflection.
-
log4j.jar
-
et up our development directory and put all the Java libraries we need into it
-
-
24 Aug 07
-
13 Jun 07
-
31 May 07
-
If you give the SessionFactory a name in your configuration file, Hibernate will in fact try to bind it to JNDI after it has been built. To avoid this code completely you could also use JMX deployment and let the JMX-capable container instantiate and bind a HibernateService to JNDI. These advanced options are discussed in the Hibernate reference documentation.
-
thread-bound programming model is the most popular
-
What does sessionFactory.getCurrentSession() do?
-
session-per-operation an anti-pattern.
-
List result = session.createQuery("from Event").list();
-
mgr.listEvents();
-
Now disable hbm2ddl
-
any modifications you made to it while detached can be saved to the database
-
you might have to modify some of the previous methods to return that identifier
-
element part, which tells Hibernate that the collection does not contain references to another entity
-
id property holds a unique identifier value
-
Eager fetch
-
eager fetch
-
hibernate.cfg.xml
-
java -classpath ../lib/hsqldb.jar org.hsqldb.Server
-
element
-
-
16 May 07
-
28 Apr 07
-
27 Apr 07
-
10 Apr 07
-
28 Mar 07
-
22 Mar 07
-
conventions
-
sophisticated
-
slightly
-
infrastructure
-
verbose
-
granularity
-
shield
-
navigate
-
explicit
-
-
04 Mar 07
-
07 Nov 06
-
31 Oct 06
-
24 Oct 06
-
08 Jun 06
-
02 Apr 06
bevan_koopmanThis chapter is an introductory tutorial for new users of Hibernate. We start with a simple command line application using an in-memory database and develop it in easy to understand steps.
-
06 Nov 05
Page Comments
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.