[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Trying to get JBoss and Hibernate to integrate
thetikigod
do-not-reply at jboss.com
Mon Jun 16 16:59:32 EDT 2008
Running
JBossAS 4.2.0
JBoss Portal 2.6.5
Hibernate 3
Hey all, I have been working on trying to get JBoss to work with hibernate and MySQL. I have successfully accessed the database through a datasource, but I cant seem to configure hibernate correctly. My session objects are always returned to me as null. Any help would be appreciated.
Here are my configs and src code:
This is in the deploy directory of the server.
SSApp-mysql-ds.xml:
| <?xml version="1.0" encoding="UTF-8"?>
|
| <!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->
| <!-- Datasource config for MySQL using 3.0.9 available from:
| http://www.mysql.com/downloads/api-jdbc-stable.html
| -->
|
| <datasources>
| <local-tx-datasource>
| <jndi-name>SupplySideDS</jndi-name>
| <connection-url>jdbc:mysql://localhost:3306/myDB</connection-url>
| <driver-class>com.mysql.jdbc.Driver</driver-class>
| <user-name>tiki</user-name>
| <password>god</password>
| <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
|
| <!-- should only be used on drivers after 3.22.1 with "ping" support
| <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
| -->
| <!-- sql to call when connection is created
| <new-connection-sql>some arbitrary sql</new-connection-sql>
| -->
| <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
| <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
| -->
|
| <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
| <metadata>
| <type-mapping>mySQL</type-mapping>
| </metadata>
| </local-tx-datasource>
| </datasources>
|
This next file is in the meta-inf of the project
hibernate.cfg.xml:
| <hibernate-configuration xmlns="urn:jboss:hibernate-deployer:1.0">
|
| <session-factory name="java:/hib-timers/SessionFactory" bean="jboss.test.har:service=Hibernate,testcase=TimersUnitTestCase">
| <property name="datasourceName">java:/SupplySideDS</property>
| <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
| <property name="sessionFactoryInterceptor">org.jboss.test.hibernate.SimpleInterceptor</property>
| <property name="hbm2ddlAuto">create</property>
| <depends>jboss:service=Naming</depends>
| <depends>jboss:service=TransactionManager</depends>
| </session-factory>
|
| </hibernate-configuration>
|
HibernateUtil.java:
| import org.hibernate.HibernateException;
| import org.hibernate.Session;
| import org.hibernate.SessionFactory;
| import org.hibernate.cfg.AnnotationConfiguration;
|
|
| public class HibernateUtil {
|
| private static final SessionFactory sessionFactory;
|
| static {
|
| try {
|
| AnnotationConfiguration config = new AnnotationConfiguration();
| config.addAnnotatedClass( User.class );
| config.configure();
|
| sessionFactory = config.buildSessionFactory();
|
| } catch (Throwable ex) {
|
| // Log exception!
| throw new ExceptionInInitializerError(ex);
| }
| }
|
| public static Session getSession() throws HibernateException {
|
| return sessionFactory.openSession();
| }
|
| }
|
User.java:
| import javax.persistence.Column;
| import javax.persistence.Entity;
| import javax.persistence.Id;
| import javax.persistence.Table;
|
|
| @Entity
| @Table( name="UserInfo" )
| public class User {
|
| private String Username;
| private String EMail;
| private String FirstName;
| private String LastName;
| private String Phone;
|
| //private Set<Vendor> vendor;
|
| @Id
| public String getUsername() {
|
| return Username;
| }
|
| @Column( name="EMail" )
| public String getEMail() {
|
| return EMail;
| }
|
| @Column( name="FirstName" )
| public String getFirstName() {
|
| return FirstName;
| }
|
| @Column( name="LastName" )
| public String getLastName() {
|
| return LastName;
| }
|
| @Column( name="Phone" )
| public String getPhone() {
|
| return Phone;
| }
|
| public void setUsername( String username ) {
|
| Username = username;
| }
|
| public void setEMail( String mail ) {
|
| EMail = mail;
| }
|
| public void setFirstName( String firstName ) {
|
| FirstName = firstName;
| }
|
| public void setLastName( String lastName ) {
|
| LastName = lastName;
| }
|
| public void setPhone( String phone ) {
|
| Phone = phone;
| }
| }
|
MainView.java
| Session session = null;
|
| try{
|
| //SessionFactory sf = new Configuration().configure().buildSessionFactory();
| session = HibernateUtil.getSession();
|
| User usr = new User();
| usr.setUsername( "thetikigod" );
| usr.setFirstName( "tiki" );
| usr.setLastName( "god" );
| usr.setPhone( "9377668982" );
| usr.setEMail( "spamme at now.com" );
|
| session.beginTransaction();
| session.save(usr);
| session.getTransaction().commit();
| }
| catch(NoClassDefFoundError ncdfe){
|
| log.error( "Error" + ncdfe.getMessage(), ncdfe );
| notification.addError( "No class definition found: " + ncdfe.getMessage() );
| }
| catch( ExceptionInInitializerError eiie) {
|
| log.error( "Error" + eiie.getMessage(), eiie );
| notification.addError( eiie.getMessage() );
| }
| catch(Exception e) {
|
| log.error( "Error" + e.getMessage(), e );
| notification.addError( e.getMessage() );
| }
| finally {
|
| // Actual contact insertion will happen at this step
| try {
| session.flush();
| session.close();
| }
| catch (Exception e) {
|
| System.out.println(e.getMessage());
| }
| }
|
Here is the exception I am getting:
| 16:37:25,863 ERROR [MainView] Errornull
| java.lang.ExceptionInInitializerError
| at com.burris.ssapp.database.HibernateUtil.<clinit>(HibernateUtil.java:26)
| at com.burris.ssapp.user.views.MainView.prepare(MainView.java:333)
| at com.burris.ssapp.user.portlet.UserPortlet.doView(UserPortlet.java:80)
| at javax.portlet.GenericPortlet.doDispatch(GenericPortlet.java:133)
| at javax.portlet.GenericPortlet.render(GenericPortlet.java:306)
| at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.invokeRender(PortletContainerImpl.java:483)
| at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.dispatch(PortletContainerImpl.java:405)
| at org.jboss.portal.portlet.container.PortletContainerInvoker$1.invoke(PortletContainerInvoker.java:86)
| at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:131)
| at org.jboss.portal.core.aspects.portlet.TransactionInterceptor.org$jboss$portal$core$aspects$portlet$TransactionInterceptor$invokeNotSupported$aop(TransactionInterceptor.java:86)
| at org.jboss.portal.core.aspects.portlet.TransactionInterceptor$invokeNotSupported_N4547270787964792031.invokeNext(TransactionInterceptor$invokeNotSupported_N4547270787964792031.java)
| at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
| at org.jboss.aspects.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:112)
| at org.jboss.portal.core.aspects.portlet.TransactionInterceptor$invokeNotSupported_N4547270787964792031.invokeNext(TransactionInterceptor$invokeNotSupported_N4547270787964792031.java)
| at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
| at org.jboss.aspects.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:102)
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4158502#4158502
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4158502
More information about the jboss-user
mailing list