Problems with hibernate
franz77 Jul 5, 2005 5:37 AMHello!
I'm using JBoss 4.0.2. I want to access a hibernate persistence layer from stateless session beans. To test this i created a POJO and the configurations as described in the Application Server Guide, apart from some class names in the hibernate-service.xml. I had to rename "net.sf.hibernate" package names to "org.hibernate", otherwise i got a ClassNotFoundException.
Now it runs, but data isn't stored into the database, although i'm using SessionFactory via JNDI - that means doing transactions manually i thought:
Here the hibernate-service.xml
<server> <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate"> <attribute name="DatasourceName">java:/adminstorage</attribute> <attribute name="Dialect">org.hibernate.dialect.SAPDBDialect</attribute> <attribute name="SessionFactoryName"> java:/hiberfac </attribute> <attribute name="CacheProviderClass"> org.hibernate.cache.HashtableCacheProvider </attribute> <attribute name="Hbm2ddlAuto">create-drop</attribute> <attribute name="ShowSqlEnabled">true</attribute> <attribute name="Username">***</attribute> <attribute name="Password">***</attribute> </mbean> </server>
the stateless sessionbeans buissness method:
/** * @ejb.interface-method view-type = "both" * * @throws EJBException */ public void doPersistence() throws EJBException, NamingException { InitialContext ictx = new InitialContext(); SessionFactory fac = (SessionFactory)ictx.lookup("java:/hiberfac"); Session sess = fac.openSession(); // Session sess = HibernateContext.getSession("java:/hiberfac"); Transaction tx = sess.beginTransaction(); AppUserPOJO user = new AppUserPOJO(); user.setLogon("***"); user.setPassword("***"); user.setFirstName("****"); user.setLastName("******"); user.setPhone("*******"); user.setEmail("********"); Long ident = (Long)sess.save(user); System.out.println(ident); tx.commit(); sess.close(); }
and the pojo + mapping:
package com.hijklm.abcdefg.morep.pojos; import java.io.Serializable; /** * @hibernate.class table = "APPUSER" * * @author ********** * */ public class AppUserPOJO implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Long ident = new Long(-1); private String logon = ""; private String password = ""; private String firstName = ""; private String lastName = ""; private String email = ""; private String phone = ""; /** * @hibernate.property column = "email" * length = "70" * type = "string" * * @return */ public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } /** * @hibernate.property column = "firstname" * type = "string" * length = "70" * @return */ public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } /** * @hibernate.property column = "lastname" * type = "string" * length = "70" * @return */ public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } /** * @hibernate.property column = "password" * type = "string" * length = "15" * @return */ public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /** * @hibernate.property column = "phone" * type = "string" * length = "70" * @return */ public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } /** * @hibernate.property column = "logon" * type = "string" * unique = "true" * length = "15" * @return */ public String getLogon() { return logon; } public void setLogon(String logon) { this.logon = logon; } /** * @hibernate.id column = "ident" * type = "long" * generator-class = "increment" * @return */ public Long getIdent() { return ident; } private void setIdent(Long ident) { this.ident = ident; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="com.hijklm.abcdefg.morep.pojos.AppUserPOJO" table="APPUSER" > <id name="ident" column="ident" type="long" > <generator class="increment"> <!-- To add non XDoclet generator parameters, create a file named hibernate-generator-params-AppUserPOJO.xml containing the additional parameters and place it in your merge dir. --> </generator> </id> <property name="email" type="string" update="true" insert="true" column="email" length="70" /> <property name="firstName" type="string" update="true" insert="true" column="firstname" length="70" /> <property name="lastName" type="string" update="true" insert="true" column="lastname" length="70" /> <property name="password" type="string" update="true" insert="true" column="password" length="15" /> <property name="phone" type="string" update="true" insert="true" column="phone" length="70" /> <property name="logon" type="string" update="true" insert="true" column="logon" length="15" unique="true" /> <!-- To add non XDoclet property mappings, create a file named hibernate-properties-AppUserPOJO.xml containing the additional properties and place it in your merge dir. --> </class> </hibernate-mapping>
and the datasource configuration:
<?xml version='1.0' encoding='ISO-8859-1'?> <datasource> <local-tx-datasource> <jndi-name>adminstorage</jndi-name> <connection-url>jdbc:sapdb://localhost/MRS</connection-url> <driver-class>com.sap.dbtech.jdbc.DriverSapDB</driver-class> <username>*****</username> <password>*****</password> </local-tx-datasource> </datasource>
Sorry for that much code, but i'm very helpless!
I'll be happy about any help!
Thx! Franz