I'm in section 2.2, and I've modified the HelloWorld example to use annotations instead of Message.hbm.xml. I believe I've made the needed changes to files as described in the book, but I get an error that I haven't been able solve.
Here are the runtime errors, and the changes I've made to files.
[b]Hibernate version: 3.2
Name and version of the database you are using: HSQLDB server 1.8.0
output
-------
Buildfile: build.xml
compile:
copymetafiles:
run:
[java] 08:40:32,503 WARN UserSuppliedConnectionProvider:23 - No connection properties specified - the user must supply JDBC connections
[java] Exception in thread "main" java.lang.ExceptionInInitializerError
[java] at persistence.HibernateUtil.<clinit>(Unknown Source)
[java] at hello.HelloWorld.main(Unknown Source)
[java] Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
[java] at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
[java] at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
[java] at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:409)
[java] at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:119)
[java] at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1933)
[java] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1216)
[java] ... 2 more
[java] Java Result: 1
BUILD SUCCESSFUL
build.xml changes
--------------------
<!-- Export the database schema -->
<target name="schemaexport" depends="compile, copymetafiles"
description="Exports a generated schema to DB and file">
<hibernatetool destdir="${basedir}">
<classpath path="${build.dir}"/>
<annotationconfiguration
configurationfile="${build.dir}/hibernate.cfg.xml"/>
hibernate.cfg.xml
------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- SQL to stdout logging
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
-->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<mapping class="hello.Message"/>
</session-factory>
</hibernate-configuration>
Message.java
---------------
package hello;
import javax.persistence.*;
@Entity
@Table(name = "MESSAGES")
public class Message {
@Id @GeneratedValue
@Column(name = "MESSAGE_ID")
private Long id;
@Column(name = "MESSAGE_TEXT")
private String text;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "NEXT_MESSAGE_ID")
private Message nextMessage;
Message() {}
public Message(String text) {
this.text = text;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}
HibernateUtil.java
---------------------
import org.hibernate.*;
import org.hibernate.cfg.*;
/**
* Startup Hibernate and provide access to the singleton SessionFactory
*/
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(hello.Message.class);
sessionFactory = cfg.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
// Alternatively, we could look up in JNDI here
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
HelloWorld.java
-----------------
package hello;
import org.hibernate.*;
import persistence.*;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
// ############################################################################
// First unit of work
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello World");
session.save(message);
tx.commit();
session.close();
// ############################################################################
// Second unit of work
Session secondSession = HibernateUtil.getSessionFactory().openSession();
Transaction secondTransaction = secondSession.beginTransaction();
List messages =
secondSession.createQuery("from Message m order by m.text asc").list();
System.out.println( messages.size() + " message(s) found:" );
for ( Iterator iter = messages.iterator(); iter.hasNext(); ) {
Message loadedMsg = (Message) iter.next();
System.out.println( loadedMsg.getText() );
}
secondTransaction.commit();
secondSession.close();
// ############################################################################
// Third unit of work
Session thirdSession = HibernateUtil.getSessionFactory().openSession();
Transaction thirdTransaction = thirdSession.beginTransaction();
// message.getId() holds the identifier value of the first message
Message loadedMessage = (Message) thirdSession.get( Message.class, message.getId());
loadedMessage.setText( "Greetings Earthling" );
loadedMessage.setNextMessage(
new Message( "Take me to your leader (please)" )
);
thirdTransaction.commit();
thirdSession.close();
// ############################################################################
// Final unit of work (just repeat the query)
// TODO: You can move this query into the thirdSession before the commit, makes more sense!
Session fourthSession = HibernateUtil.getSessionFactory().openSession();
Transaction fourthTransaction = fourthSession.beginTransaction();
messages =
fourthSession.createQuery("from Message m order by m.text asc").list();
System.out.println( messages.size() + " message(s) found:" );
for ( Iterator iter = messages.iterator(); iter.hasNext(); ) {
Message loadedMsg = (Message) iter.next();
System.out.println( loadedMsg.getText() );
}
fourthTransaction.commit();
fourthSession.close();
// Shutting down the application
HibernateUtil.shutdown();
}
}
|