facebook

SQLGrammarException "hibernate_sequence" [closed]

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #221803 Reply

    thurisaz
    Member

    Hi there,

    I have a very serious situation! I’ve connected MyEclipse with my Postgresql-Database, created all needed Tables wth the SQL-Editor but whenever I try to save an object I always get the error-message “net.sf.hibernate.exception.SQLGrammarException: Could not save object”.

    Even searching with google does not give me any hints what exactly the problem is. Does anybody of you guys have an idea?

    P.S: I’ve also tried another Postgresql-Database (the local one on my own system) – same problem!

    
        private void run()
        {
            initObjects();
            try
            {
                Session session = LocalDatabaseSessionFactory.currentSession();
                Transaction tx = session.beginTransaction();
                session.save(myGUIProperties);
                tx.commit();
                System.out.println("Save successful.");
            } 
            catch (HibernateException e)
            {
                System.out.println(e.toString());
            }
            finally
            {
                try
                {
                    LocalDatabaseSessionFactory.closeSession();
                } 
                catch (HibernateException e1)
                {
                    System.out.println(e1.toString());
                }
            }
        }
    

    log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    net.sf.hibernate.exception.SQLGrammarException: Could not save object

    #221807 Reply

    support-jeff
    Member

    Have you selected the Postgresql dialect in your hibernate config file?

    [Unrelated: you should put a rollback in your catch block]

    #221819 Reply

    thurisaz
    Member

    Hi Jeff,

    yes I think so:

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
    
    <!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <hibernate-configuration>
    
        <session-factory>
            <!-- properties -->
            <property name="connection.username">XXX</property>
            <property name="connection.url">jdbc:postgresql:kandmsma</property>
            <property name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>
            <property name="connection.password">XXX</property>
            <property name="connection.driver_class">org.postgresql.Driver</property>
    
            <!-- mapping files -->
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Category.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Mathmlformular.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Picturecomponent.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Textcomponent.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Downloadcomponent.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Userinformation.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Usergroup.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Userobject.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Link.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Keyword.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Article.hbm.xml"/>
            <mapping resource="de/hendrik_neumann/projects/commercial/kandmsma/model/beans/Guiproperties.hbm.xml"/>
    
        </session-factory>
    
    </hibernate-configuration>
    #221821 Reply

    Greg
    Member

    Try to print the full stacktrace and see if there are any nested exceptions.

    
    catch (HibernateException e)
    {
        System.out.println(e.toString());
        e.printStackTrace();
        if (e.getCause() != null )
            System.out.println(e.getCause().getMessage());
    
    } 

    The reason I am suggesting this is to see if you are getting bit by this Hibernate 2.1.7c bug: http://jira.jboss.com/jira/browse/HIBERNATE-1

    #221822 Reply

    thurisaz
    Member

    Hi Greg,

    thank you for your support. This is the requested output:

    
    log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    net.sf.hibernate.exception.SQLGrammarException: Could not save object
    net.sf.hibernate.exception.SQLGrammarException: Could not save object
        at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
        at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
        at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110)
        at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:792)
        at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747)
        at de.hendrik_neumann.projects.commercial.kandmsma.testcases.HibernateTest.run(HibernateTest.java:25)
        at de.hendrik_neumann.projects.commercial.kandmsma.testcases.HibernateTest.main(HibernateTest.java:15)
    Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
    
        at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139)
        at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:152)
        at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:100)
        at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43)
        at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:517)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50)
        at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:233)
        at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:64)
        at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:774)
        ... 3 more
    ERROR: relation "hibernate_sequence" does not exist
    
    #221823 Reply

    thurisaz
    Member

    what exactly is this “hibernate_sequence” ???

    #221826 Reply

    Greg
    Member

    If you are using “sequence” as your ID generator, the <generator> element in your hbm file will be configured to use sequence without specifiying a name. The default name for a sequence in Hibernate 2.1 is “hibernate_sequence”.

    Here are a couple of options as given by LaLiLuna in his tutorial located here: Struts+hibernate+Myeclipse Tutorial

    The fastest to create a sequence called hibernate_sequence. A disadvantage is that all tables share the same sequence. You will have one table under heavy load.

    CREATE SEQUENCE hibernate_sequence
    INCREMENT 1
    MINVALUE 1
    MAXVALUE 9223372036854775807
    START 1
    CACHE 1;

    The nicest way, but only possible when you are sure not to regenerate your mapping files (you would override your changes) is to change the mapping from

    <generator class=”sequence”/>

    to the following for the book. The changes for the customer are analogues.

    <generator class=”sequence”>book_id_seq
    <param name=”sequence”>book_id_seq</param>
    </generator>

    Make sure you understand the second option. If you choose to manually modify the hbm file associated with your object, any changes will be overwritten if you update or create any hibernate mappings.

    Another alternative is to use another ID generator other than sequence, like native or increment.

    #221828 Reply

    thurisaz
    Member

    *wow* Thank you very much, you’ve just saved my christmas holidays 😀

    Using the “increment” generator works well, nevertheless the best solution for me is to use one Postgresql-Sequenences for one table. Therefore I need to manipulate the hibernate files.

    Now I have to other questions:
    1. Is it possible to turn the hibernate-sychnronization off after creating all needed files from the tables?
    2. Why does your Hibernate-Synchronization doesn’t create boolean values? I have a table which contains boolean rows but whenever I create a hibernate mapping from that table I get Java-classes with byte-values. Why that?

    P.S. Happy christmas days to all of you support guys!

    #221834 Reply

    Greg
    Member

    @thurisaz wrote:

    1. Is it possible to turn the hibernate-sychnronization off after creating all needed files from the tables?

    The sychronization isn’t automatic. You just need to not create any more mappings and the files wont be regenerated.
    @thurisaz wrote:

    2. Why does your Hibernate-Synchronization doesn’t create boolean values? I have a table which contains boolean rows but whenever I create a hibernate mapping from that table I get Java-classes with byte-values. Why that?

    When using the “Create Hibernate Mapping” dialog, does swtiching from Java types to Hibernate types help? Just an idea.

    #221835 Reply

    thurisaz
    Member

    @support-greg wrote:

    When using the “Create Hibernate Mapping” dialog, does swtiching from Java types to Hibernate types help? Just an idea.

    No, I’ve already tried this but the files didn’t differ. The database mapping with these files didn’t work (Postgresql didn’t accept bytes where the values should be boolean) – therfore I’ve changed it “by hand” to boolean.. now it works

Viewing 10 posts - 1 through 10 (of 10 total)
Reply To: SQLGrammarException "hibernate_sequence" [closed]

You must be logged in to post in the forum log in