Hi,
I am having the same problem above but unfortunely it does not seem the VETO issue as described. Any help would be really appreciated.
Hibernate.cfg.xml
Code:
<hibernate-configuration>
<session-factory name="hibernate.session-factory.Oracle">
<property name="connection.datasource">ectrackDS</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="jndi.url">t3://127.0.0.1:7001</property>
<property name="hibernate.transaction.manager_lookup_class">net.sf.hibernate.transaction.WeblogicTransactionManagerLookup </property>
<mapping resource="edu/mit/ssit/mysample/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration
HBNSessionManager: (taken pretty well from the caveatemptor app and info from hibernate in action
Code:
public class HibernateSessionManager
{
public String _id = "$Id: $";
private static final Category log = Category.getInstance(HibernateSessionManager.class.getName());
private static final ThreadLocal threadTransaction = new ThreadLocal();
public static final ThreadLocal threadSession = new ThreadLocal();
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory
log.debug("Attempting to create the Hibernate Session Factory");
//session factory is now in JNDI based on values from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
log.debug("Hibernate Session Factory created successfully");
}
catch (HibernateException ex)
{
log.debug("Hibernate Exception when attempting to create the session factory:"+ex.getMessage());
ex.printStackTrace();
throw new RuntimeException("Hibernate Configuration problem: " + ex.getMessage(), ex);
}
}
public static SessionFactory getSessionFactory()
{
SessionFactory sessions = null;
try
{
Context ctx = new InitialContext();
String jndiName = "java:hibernate/HibernateFactory";
sessions = (SessionFactory)ctx.lookup("hibernate.session-factory.Oracle");
}
catch(NamingException ex)
{
throw new RuntimeException("Failed to get HibernateSessionFactory from context");
}
return sessions;
}
/**
* Retrieves the current Session local to the thread.
* <p/>
* If no Session is open, opens a new Session for the running thread.
*
* @return Session
*/
public static Session getSession()
throws HibernateException {
Session s = (Session) threadSession.get();
try
{
if (s == null)
{
log.debug("Opening new Session for this thread.");
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
catch (HibernateException ex)
{
log.error("Failed to get hibernate session:"+ex.getMessage());
throw ex;
}
return s;
}
/*
public static Session currentSession() throws HibernateException
{
log.debug("Attempting to get current session");
Session s = (Session) session.get();
log.debug("Session is "+s);
// Open a new Session, if this Thread has none yet
if (s == null)
{
log.debug("Session was null. Attempting to open a new session");
s = sessionFactory.openSession();
log.debug("New Session created");
session.set(s);
log.debug("Session set successfully");
}
return s;
}
*/
/*
public static void closeSession() throws HibernateException
{
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
*/
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws HibernateException
{
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null)
{
log.debug("Starting new database transaction in this thread.");
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex)
{
log.error("Failed to begin hibernate transaction."+ex.getMessage());
throw ex;
}
}
/**
* Closes the Session local to the thread.
*/
public static void closeSession()
throws HibernateException
{
try
{
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen())
{
log.debug("Closing Session of this thread.");
s.close();
}
}
catch (HibernateException ex)
{
log.error("Failed to close hibernate session."+ex.getMessage());
throw ex;
}
}
public static void commitTransaction()
throws HibernateException
{
Transaction tx = (Transaction) threadTransaction.get();
try
{
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() )
{
log.debug("Committing database transaction of this thread.");
tx.commit();
}
threadTransaction.set(null);
}
catch (HibernateException ex)
{
log.error("Hibernate commit failed. Attempting to rollback. "+ex.getMessage());
rollbackTransaction();
throw ex;
}
}
/**
* Commit the database transaction.
*/
public static void rollbackTransaction()
throws HibernateException
{
Transaction tx = (Transaction) threadTransaction.get();
try
{
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
log.debug("Tyring to rollback database transaction of this thread.");
tx.rollback();
}
}
catch (HibernateException ex)
{
log.error("Rollback failed. "+ex.getMessage());
throw ex;
}
finally
{
closeSession();
}
}
} //end of class
I am using a sessionbean to call a dao (but currently handling transactions in the Session bean (or trying to anyway):
StudentSessionBean:
Code:
public void updateStudent(Student criteria)
throws Exception
{
log.info("updating student ID:"+criteria.getStudentId());
try
{
new StudentDAO().updateStudent(criteria);
//we want our container to handle transactions
HibernateSessionManager.commitTransaction();
}
catch(Exception e)
{
throw e; //we logged it already
}
finally
{
HibernateSessionManager.closeSession();
}
}
DAO that does the update:
Code:
public void updateStudent(Student student) throws HibernateException
{
Session hbnsession = null;
List results = null;
try
{
hbnsession = HibernateSessionManager.getSession();
hbnsession.update(student);
}
catch (HibernateException e)
{
log.error(e.getMessage());
//TODO: map this to a generic infrastructure exception
throw e;
}
}
I am using middlegen to generate the POJO and mark it up with hibernate tags. Here is the mapping file:
Code:
<hibernate-mapping>
<class
name="com.mysample.model.Student"
table="STUDENT"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="STUDENT"
</meta>
<composite-id>
<meta attribute="class-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="studentId"
column="STUDENT_ID"
type="java.math.BigDecimal"
length="22"
>
<meta attribute="field-description">
@hibernate.property
column="STUDENT_ID"
</meta>
</key-property>
<key-property
name="firstName"
column="FIRST_NAME"
type="java.lang.String"
length="30"
>
<meta attribute="field-description">
@hibernate.property
column="FIRST_NAME"
</meta>
</key-property>
<key-property
name="lastName"
column="LAST_NAME"
type="java.lang.String"
length="30"
>
<meta attribute="field-description">
@hibernate.property
column="LAST_NAME"
</meta>
</key-property>
<key-property
name="gender"
column="GENDER"
type="java.lang.String"
length="1"
>
<meta attribute="field-description">
@hibernate.property
column="GENDER"
</meta>
</key-property>
<key-property
name="graduationYear"
column="GRADUATION_YEAR"
type="java.lang.Integer"
length="4"
>
<meta attribute="field-description">
@hibernate.property
column="GRADUATION_YEAR"
</meta>
</key-property>
</composite-id>
<!-- associations -->
</class>
</hibernate-mapping>
Everything works fine re a select so I know the JNDI mapping is fine...The problem is if I try and persist it never gets saved when running in Weblogic8.1 I do not see an messages get written to the log (although I can only set it to INFO as DEBUG makes my app server (running on a laptop) run out of memory.
Any help would be appreciated..
Thanks!