-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Unable to generate a sequence number with Oracle Database
PostPosted: Thu Apr 21, 2005 12:30 pm 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
Hi, I am using hibernate 2.1 have created a mapping and so on and have managed to retrieve some data from a table using hibernate succesfully. I am now trying to create a record in the database. In my mapping file for the table I have defined as follows:
Code:
<class name="PtfPortfolio" table="PTF_PORTFOLIO">
      <id name="ptfPk" column="PTF_PK">
         <generator class="native" />
      </id>
      <property name="name" column="NAME" type="java.lang.String" />
      <property name="description" column="DESCRIPTION"
         type="java.lang.String" />
      <property name="volume" column="VOLUME" type="java.lang.Long" />
   </class>


When I try and create a row hibernate complains because it is trying to use the hilo algorithm to generate the value of PTF_PK. Reading the documentation I thought it would use SEQUENCE as I am using ' Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production'

Code:
[21/04/05 17:23:05:660 BST] 0000004f TableGenerato E   TRAS0014I: The following exception was logged java.sql.SQLException: ORA-00942: table or view does not exist

   at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
   at oracle.jdbc.driver.T2CConnection.checkError(T2CConnection.java:630)
   at oracle.jdbc.driver.T2CPreparedStatement.execute_for_describe(T2CPreparedStatement.java:851)
   at oracle.jdbc.driver.OracleStatement.execute_maybe_describe(OracleStatement.java:896)
   at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:986)
   at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
   at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:2929)
   at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.pmiExecuteQuery(WSJdbcPreparedStatement.java:667)
   at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeQuery(WSJdbcPreparedStatement.java:477)
   at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:94)
   at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)
   at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:776)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)


So I then tried changing the generator class defined to sequence. After redeploying I couldnt even retrieve any data, it is now giving the following exception trace
Code:
[21/04/05 17:06:50:107 BST] 0000004f SystemErr     R %%%% Error Creating SessionFactory %%%%
[21/04/05 17:06:50:107 BST] 0000004f SystemErr     R net.sf.hibernate.MappingException: could not instantiate id generator
   at net.sf.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:82)
   at net.sf.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:82)
   at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:644)
   at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:690)
   at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:42)
   at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
   at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:805)
   at com.abnamro.att.server.hibernate.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:47)


Any help appreciated, this is my first use of hibernate so apologies if Im doing something stupid


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 10:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
If you use the native generator youy are using a sequence for Oracle dialect. If the sequence paramater is not given then the sequence name it tried to use is 'hibernate_sequence' which you have to create in the database.


See docs for more info http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id-generator


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 4:47 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
Ok, thanks I had made two mistakes which I have now rectified:
Ive created a SEQUENCE object called hibernate_sequence
Ive added a Dialect property <property name="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</property>
Ive also enabled sql_ouput.

Now no errors are generated on retrieval or creation, but on creation all Hibernate does is get the the next sequence number

Hibernate: select hibernate_sequence.nextval from dual

It never trys to do the creation, why is this ?

thanks Paul


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 7:26 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Can you post your code where you are doing the creation? If I had to guess, it sounds like you are not committing the transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 8:13 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
pmularien wrote:
Can you post your code where you are doing the creation? If I had to guess, it sounds like you are not committing the transaction?


Thats what I thought, but I would have thought the create would still use in the log file.

Anyway here is the code running within a Websphere 6 AppServer

Code:
try
        {
            Session s = HibernateSessionFactory.currentSession();
            try
            {
                PtfPortfolio ptfPortfolio = new PtfPortfolio();
                ptfPortfolio.setName(portfolioVO.getName());
                ptfPortfolio.setDescription(portfolioVO.getDescription());
                ptfPortfolio.setVolume(portfolioVO.getVolume());

                s.save(ptfPortfolio);
            }
            finally
            {
                HibernateSessionFactory.closeSession();
            }
        }


and here is the config file for the table
Code:
<hibernate-mapping package="com.abnamro.att.server.domain">

   <class name="PtfPortfolio" table="PTF_PORTFOLIO">
      <id name="ptfPk" column="PTF_PK">
         <generator class="sequence" />
      </id>
      <property name="name" column="NAME" type="java.lang.String" />
      <property name="description" column="DESCRIPTION"
         type="java.lang.String" />
      <property name="volume" column="VOLUME" type="java.lang.Long" />
   </class>

</hibernate-mapping>




Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 8:26 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Um, where are you wrapping this with a Transaction? If you don't have this within a Transaction, the record won't get inserted.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 8:37 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
It is all a method within a SessionBean, have I got to do something else ?

Code:
public void CreatePortfolio(PortfolioVO portfolioVO) throws EJBException
    {
        try
        {
            Session s = HibernateSessionFactory.currentSession();
            try
            {
                PtfPortfolio ptfPortfolio = new PtfPortfolio();
                ptfPortfolio.setName(portfolioVO.getName());
                ptfPortfolio.setDescription(portfolioVO.getDescription());
                ptfPortfolio.setVolume(portfolioVO.getVolume());

                s.save(ptfPortfolio);
            }
            finally
            {
                HibernateSessionFactory.closeSession();
            }
        }
        catch (Exception e)
        {
            throw new EJBException("Failed to create portfolio", e);
        }
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 8:45 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Um, can you try adding
Code:
Transaction tx = s.beginTransaction();
...
tx.commit();

and see what happens? Why would a session bean implicitly start and end transactions in a third-party library that the container knows nothing about?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 9:39 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
I now get this:
Code:
[22/04/05 14:37:18:689 BST] 00000032 SystemOut     O In create
[22/04/05 14:37:19:314 BST] 00000032 ServletWrappe A   SRVE0242I: [/WEB-INF/jsp/addeditportfolio.jsp]: Initialization successful.
[22/04/05 14:37:22:392 BST] 00000088 SystemOut     O Writing To database
[22/04/05 14:37:22:392 BST] 00000088 SystemOut     O Hibernate: select hibernate_sequence.nextval from dual
[22/04/05 14:37:22:423 BST] 00000088 SessionImpl   E   Could not synchronize database state with session
[22/04/05 14:37:22:423 BST] 00000088 ExceptionUtil E   CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "CreatePortfolio" on bean "BeanId(ATT-EnterpriseApplication#ATT-EJB.jar#Portfolio, null)". Exception data: javax.ejb.EJBException: Failed to create portfolio; nested exception is: net.sf.hibernate.HibernateException: Session is closed
net.sf.hibernate.HibernateException: Session is closed
   at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3339)
   at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:63)
   at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:58)
   at net.sf.hibernate.impl.BatcherImpl.prepareBatchStatement(BatcherImpl.java:111)
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:454)
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:436)
   at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at com.abnamro.att.server.ejb.PortfolioBean.CreatePortfolio(PortfolioBean.java:121)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 9:51 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
I assume you put the Transaction.commit as the last statement in the try block? (before you closed the session in the finally clause?)

I can't see your code if you don't post it, and I can't tell you what you're doing from the stack trace.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 10:09 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
Sorry, no I had it in th wrong place (outside the hibernate code first try block) Ive now put it into the second try block and Websphere seems to be complaing that I am setting commit when it is already automticaly autocommitting.
Code:
public void CreatePortfolio(PortfolioVO portfolioVO) throws EJBException
    {
        try
        {
            Session s = HibernateSessionFactory.currentSession();
           
            try
            {
                Transaction tx = s.beginTransaction();
               
                PtfPortfolio ptfPortfolio = new PtfPortfolio();
                ptfPortfolio.setName(portfolioVO.getName());
                ptfPortfolio.setDescription(portfolioVO.getDescription());
                ptfPortfolio.setVolume(portfolioVO.getVolume());

                s.save(ptfPortfolio);
               
                tx.commit();
            }
            finally
            {
                HibernateSessionFactory.closeSession();
            }
           

        }
        catch (Exception e)
        {
            throw new EJBException("Failed to create portfolio", e);
        }
    }


Stack trace
Code:
************* End Display Current Environment *************
[22/04/05 15:03:52:147 BST] 00000088 JDBCTransacti E   TRAS0014I: The following exception was logged java.sql.SQLException: DSRA9350E: Operation setAutoCommit is not allowed during a global transaction.
   at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.setAutoCommit(WSJdbcConnection.java:2410)
   at net.sf.hibernate.transaction.JDBCTransaction.toggleAutoCommit(JDBCTransaction.java:104)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:74)
   at com.abnamro.att.server.ejb.PortfolioBean.CreatePortfolio(PortfolioBean.java:118)
   at com.abnamro.att.server.interfaces.EJSRemoteStatelessPortfolio_c29e384f.CreatePortfolio(Unknown Source)
   at com.abnamro.att.server.interfaces._Portfolio_Stub.CreatePortfolio(_Portfolio_Stub.java:259)
   at com.abnamro.att.ui.ejb.PortfolioHelper.CreatePortfolio(PortfolioHelper.java:54)
[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 10:14 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Where is the code for HibernateSessionFactory.currentSession()?

What are the transactional properties of the session bean?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 10:25 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
pmularien wrote:
Where is the code for HibernateSessionFactory.currentSession()?

Thanks for persevering with this.
Code:
public static Session currentSession() throws HibernateException
    {
        Session session = (Session) threadLocal.get();

        if (session == null)
        {
            if (sessionFactory == null)
            {
                try
                {
                    cfg.configure(CONFIG_FILE_LOCATION);
                    sessionFactory = cfg.buildSessionFactory();
                }
                catch (Exception e)
                {
                    System.err
                            .println("%%%% Error Creating SessionFactory %%%%");
                    e.printStackTrace();
                }
            }
            session = sessionFactory.openSession();
            threadLocal.set(session);
        }

        return session;
    }

pmularien wrote:
What are the transactional properties of the session bean?

Im actaully taking on some protype code written by someone else. The ejb-jar.xml is created automtically by XDoclet. Ive shown the relevent code below but looking at it doesnt actaully seem to be specify a transaction-type in the assembly descriptor for this particular method.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar id="ejb-jar_1">

   <description><![CDATA[No Description.]]></description>
   <display-name>Generated by XDoclet</display-name>

   <enterprise-beans>

      <!-- Session Beans -->
      <session id="Session_Portfolio">
         <description><![CDATA[A Portfolio]]></description>
         <display-name>Portfolio</display-name>

         <ejb-name>Portfolio</ejb-name>

         <home>com.abnamro.att.server.interfaces.PortfolioHome</home>
         <remote>com.abnamro.att.server.interfaces.Portfolio</remote>
         <local-home>com.abnamro.att.server.interfaces.PortfolioLocalHome</local-home>
         <local>com.abnamro.att.server.interfaces.PortfolioLocal</local>
         <ejb-class>com.abnamro.att.server.ejb.PortfolioSession</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Container</transaction-type>

      </session>
   </enterprise-beans>

   <assembly-descriptor id="AssemblyDescriptor_1">
        <method-permission>
            <description>Generated unchecked method permission</description>
            <unchecked></unchecked>
       
            <method>
                <description>generatedAllMethodsDescription</description>
                <ejb-name>Portfolio</ejb-name>
                <method-intf>Remote</method-intf>
                <method-name>CreatePortfolio</method-name>
                <method-params>
                    <method-param>com.abnamro.att.valueobject.PortfolioVO</method-param>
                </method-params>
            </method>           
        </method-permission> 
   </assembly-descriptor>

</ejb-jar>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 10:40 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Quote:
Ive shown the relevent code below but looking at it doesnt actaully seem to be specify a transaction-type in the assembly descriptor for this particular method.


That's because transaction-type is declared at the bean level. So you can see that the bean is using container-managed transactions.

If you want to use bean managed transactions, try switching the bit in the deployment descriptor.

If you want to use container-managed, you may want to read in the Hibernate docs about utilizing the Transaction Strategy to set it to use the JTA configuration of the container. That should get things working as you intend. However, if you're using container managed transactions - I'd assume that you'd not want to close the Hibernate session within this method, since the transaction wouldn't be committed until the the method call returns, right?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 11:49 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 12:15 pm
Posts: 34
pmularien wrote:
That's because transaction-type is declared at the bean level. So you can see that the bean is using container-managed transactions.


I meant it doesn't specify the trnsaction-type as in New/Required ... for the ContainerManaged Bean.

pmularien wrote:
If you want to use container-managed, you may want to read in the Hibernate docs about utilizing the

Ok, Ive set the property to <property name="net.sf.hibernate.transaction.JTATransactionFactory">net.sf.hibernate.transaction.WebSphereTransactionManagerLookup</property>
and taken out the hibernate trnsaction stuff and Im back to where I was earlier.

I have no errors but it only does the
select hibernate_sequence.nextval from dual
statement rather than create. As I said earlier I think it should show the create being done in the log even if it is not committed whatever the state of my transactions, so I ma going to try and create a record without using the sequence number generation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.