Beginner |
|
Joined: Mon Sep 06, 2004 9:36 am Posts: 35
|
Hi all,
I am trying to follow the Hello World example in the book. My aim is to have a Global Hibernate running on JBoss whose job is to create a SessionFactory.
I created an ODBC connection which is connecting to my MS SQL Server database which already has table MESSAGES with concerned columns. Then in JBoss's odbc-ds.xml I add this entry under <datasources> </datasources>.
<local-tx-datasource>
<jndi-name>HTestDS</jndi-name>
<connection-url>jdbc:odbc:HTest</connection-url>
<driver-class>sun.jdbc.odbc.JdbcOdbcDriver</driver-class>
<user-name>sa</user-name>
<password></password>
</local-tx-datasource>
Now I created a POJO Message.java
package hello;
import java.io.Serializable;
public class Message implements Serializable {
private Long id;
private String text;
private Message nextMessage;
public Message() {}
public Message(String text) {
this.text = text;
}
public Long getId() {
return id;
}
public 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;
}
}
Then I created Message.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hello.Message" table="MESSAGES">
<id name="id" column="MESSAGE_ID">
<generator class="increment"/>
</id>
<property name="text" column="MESSAGE_TEXT"/>
<many-to-one
name="nextMessage"
cascade="all"
column="NEXT_MESSAGE_ID"/>
</class>
</hibernate-mapping>
Then I created hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<!-- Bound the SessionFactory to JNDI -->
<session-factory name="java:/hibernate/HibernateFactory">
<property name="show_sql">true</property>
<property name="connection.datasource">
java:/comp/env/jdbc/HTestDS<
/property>
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<propert name="transaction.mamager_lookup_class">
net.sf.hibernate.transaction.JBossTransactionManagerLookup
</property>
<!-- Hello mapping files. -->
<mapping resource="hello/Message.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Finally I created HibernateGlobal.java in package global.
package global;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.HibernateException;
public class HibernateGlobal {
private String configFilePath = "/hibernate.cfg.xml";
private SessionFactory factory = null;
public HibernateGlobal() {
Configuration configuration = null;
try {
factory = new Configuration()
.configure(configFilePath)
.buildSessionFactory();
} catch (Throwable t) {
System.out.println("Exception while initializing Hibernate.");
t.printStackTrace();
}
}
public SessionFactory getFactory() {
return factory;
}
}
Now what steps I should take so that HibernateGlobal is installed successfully on JBoss and available for other *.java to access? Please give an input.
Thanks
For example I have written this MessageManipulation.java
package hello;
import global.HibernateGlobal;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
public class MessageManipulation {
public static void addMessage(String messageText) throws Exception {
HibernateGlobal hg = new HibernateGlobal();
Session session = hg.openSession();
Transaction tx = session.beginTransaction();
Message message = new Message(messageText);
session.save(message);
tx.commit();
session.close();
}
}
|
|