I'm trying to get started with hibernate, but I can't find any tutorials I understand. I'm trying to create a simple app to get started, but I've really no idea where to get started.
I'm pretty advanced with java, although only used eclipse really. So.. where do I configure what DB it uses, and alike?
[edit]
I'm familiar with the concepts of ORM, and what Hibernate is. What I don't know or understand, is where to begin my application. I intend to use annotations, but, after annotating my POJOs, what do I do? Where do I specify database server, user, etc? What else do I need to do?
Hibernate annotations are the newest way to define mappings without the use of XML file. You can use annotations in addition to or as a replacement of XML mapping metadata. Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping.
@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.
@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.
My experiences and background were similar, and I also had a hard time finding a tutorial that addressed my development preferences (which seem similar to yours):
I read most of Java Persistence with Hibernate, and it took a long time to separate this use case from all the other options that it presents (XML configuration in Hibernate/JPA format, xdoclet "annotations", etc...).
It would have made so much more sense to me if the available documentation would take a stand and actively push me in this direction instead of giving me a bunch of options and wondering where to go. Lacking that, I learned the following lessons by converting the standard Hibernate Tutorial over to annotations.
Here's my copy of hibernate.cfg.xml. It only sets properties, and explicitly avoids any mapping configuration.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Information about the database to be used -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Misc. Hibernate configuration -->
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">false</property>
</session-factory>
</hibernate-configuration>
For example, some snippets from my version of EventManager:
public final class EventManager {
private final SessionFactory sessionFactory;
/** Default constructor for use with JSPs */
public EventManager() {
this.sessionFactory = HibernateUtil.getSessionFactory();
}
/** @param Nonnull access to sessions with the data store */
public EventManager(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/** @return Nonnull events; empty if none exist */
public List<Event> getEvents() {
final Session db = this.sessionFactory.getCurrentSession();
return db.createCriteria(Event.class).list();
}
/**
* Creates and stores an Event for which no people are yet registered.
* @param title Nonnull; see {@link Event}
* @param date Nonnull; see {@link Event}
* @return Nonnull event that was created
*/
public Event createEvent(String title, Date date) {
final Event event = new Event(title, date, new HashSet<Person> ());
this.sessionFactory.getCurrentSession().save(event);
return event;
}
/**
* Registers the specified person for the specified event.
* @param personId ID of an existing person
* @param eventId ID of an existing event
*/
public void register(long personId, long eventId) {
final Session db = this.sessionFactory.getCurrentSession();
final Person person = (Person) db.load(Person.class, personId);
final Event event = (Event) db.load(Event.class, eventId);
person.addEvent(event);
event.register(person);
}
...other query / update methods...
}
For example, I find this implementation of Event much simpler to understand as long as you remember that Hibernate accesses fields directly when it needs to.
@Entity(name="EVENT")
public final class Event {
private @Id @GeneratedValue @Column(name="EVENT_ID") Long id; //Nullable
/* Business key properties (assumed to always be present) */
private @Column(name="TITLE", nullable=false) String title;
@Column(name="DATE", nullable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date date;
/* Relationships to other objects */
@ManyToMany
@JoinTable(
name = "EVENT_PERSON",
joinColumns = {@JoinColumn(name="EVENT_ID_FK", nullable=false)},
inverseJoinColumns = {@JoinColumn(name="PERSON_ID_FK", nullable=false)})
private Set<Person> registrants; //Nonnull
public Event() { /* Required for framework use */ }
/**
* @param title Non-empty name of the event
* @param date Nonnull date at which the event takes place
* @param participants Nonnull people participating in this event
*/
public Event(String title, Date date, Set<Person> participants) {
this.title = title;
this.date = new Date(date.getTime());
this.registrants = new HashSet<Person> (participants);
}
/* Query methods */
/** @return Nullable ID used for persistence */
public Long getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
public Date getDate() {
return new Date(this.date.getTime());
}
/** @return Nonnull people registered for this event, if any. */
public Set<Person> getRegistrants() {
return new HashSet<Person> (this.registrants);
}
/* Update methods */
public void register(Person person) {
this.registrants.add(person);
}
}
Hope that helps!
I'll assume that you're having trouble with the Hibernate reference docs?
Maybe this Hibernate tutorial would be better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With