-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Dialect does not support identity key generation
PostPosted: Sun Apr 04, 2004 6:30 pm 
Beginner
Beginner

Joined: Sun Apr 04, 2004 8:45 am
Posts: 20
I'm beginner with Hibernate and I tried to execute an example I found on http://blog.ideoplex.com/software/2003/07/09.html#a365. But, I have every time the same error:


Code:
Exception in thread "main" net.sf.hibernate.MappingException: Dialect does not support identity key generation
        at net.sf.hibernate.dialect.Dialect.getIdentitySelectString(Dialect.java:289)
        at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:633)
        at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:715)
        at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)
        at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
        at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:750)
        at Main.main(Main.java:24)



I use PostgreSQL 7.4.1 and I created the table KEYWORDS as follow:
Code:
CREATE TABLE KEYWORDS(ID INTEGER PRIMARY KEY NOT NULL, NAME VARCHAR(32));


The hibernabe.properties file is as follow:

Code:
hibernate.connection.username=student
hibernate.connection.password=geheim
hibernate.connection.url=jdbc:postgresql://localhost:5432/keyword_db
hibernate.connection.driver_class=org.postgresql.Driver
hibernate.dialect=net.sf.hibernate.dialect.PostgreSQLDialect   
hibernate.connection.pool_size=2


And here ist my Keword.hbm.xml file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <!-- table created by: CREATE TABLE KEYWORDS ( ID IDENTITY, NAME VARCHAR(25) ); -->
  <class name="hb.Keyword"
         table="keywords">
     
    <id name="id"
        type="integer"
        column="id">
         <generator class="identity"/>
    </id>
   

       
    <property name="name"
              column="NAME"
              not-null="true"
              unique="true"
              />
  </class>
</hibernate-mapping>



This example is prepared for hsqldb, but it doesn't word with hsqldb to (I tried first time with hsqldb, but it didn't work and I changed to PostgreSQL).

And here is my Java-Code:

Code:
public class Main {
   
    public static void main ( String[] args )
        throws MappingException,
               HibernateException
    {
        Configuration cfg = new Configuration();
        cfg.addClass( hb.Keyword.class );

        SessionFactory sessions = cfg.buildSessionFactory(); // error-line 24
        Session session = sessions.openSession();

        Transaction tx = null;
        Keyword     kw = new Keyword( 1,"red" );
       
        try
        {
            tx = session.beginTransaction();
            session.save( kw );
            tx.commit();
        }
        catch ( HibernateException he )
        {
            if ( tx != null ) tx.rollback();
            throw he;
        }
        finally
        {
            session.close();
        }
    }
}


What is wrong here? Thanks for any help and best regards.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 3:32 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
The documentation about generators, http://www.hibernate.org/hib_docs/reference/html/or-mapping.html#or-mapping-s1-4--generator
does not mention PostgreSQL among the databases that support the "identity" generator.

Personally, I use PostgreSQL 7.4.2 and the "seqhilo" generator. Works just fine for me. YMMV.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 4:51 am 
Beginner
Beginner

Joined: Sun Apr 04, 2004 8:45 am
Posts: 20
joib wrote:
The documentation about generators, http://www.hibernate.org/hib_docs/reference/html/or-mapping.html#or-mapping-s1-4--generator
does not mention PostgreSQL among the databases that support the "identity" generator.

Personally, I use PostgreSQL 7.4.2 and the "seqhilo" generator. Works just fine for me. YMMV.


There are two following lines in this document:


identity
supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.

sequence
uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int


I tried with HypersonicSQL and it didn't work. After that I changed to PostgreSQL. And I tried with SEQUENCE too. Here is my script:

Code:
CREATE SEQUENCE id_sequence START 1 INCREMENT 1;

CREATE TABLE KEYWORDS
(
   id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('id_sequence'),
   name VARCHAR(32) NOT NULL
);


And here is the Keyword.hbm.xml file:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="hb.Keyword"
         table="keywords">
     
    <id name="id" type="integer" column="id">
        <generator class="sequence">
                <param name="sequence">id_sequence</param>
        </generator>
   </id>
       
    <property name="name"
              column="name"
              not-null="true"
              unique="true"
              />
  </class>
</hibernate-mapping>


Do I misunderstand something?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 5:31 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
When using the sequence generator, inserting a new object means:

1. SELECT next value from the sequence.

2. INSERT the new object into it's table using the value obtained in step #1.

So your table should be

Code:
CREATE TABLE KEYWORDS
(
   id INTEGER PRIMARY KEY,
   name VARCHAR(32) NOT NULL
);


If you want to avoid hitting the database twice for every insert you should use another generator. seqhilo, for example, works fine with PostgreSQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 05, 2004 5:47 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Oh, and another thing I just noticed:

Code:
Keyword     kw = new Keyword( 1,"red" );
       
        try
        {
            tx = session.beginTransaction();
            session.save( kw );


assuming that

new Keyword(1, "red")

sets the id to 1, that is not a good idea unless you use the "assigned" id generator. The whole point of all these id generators is that hibernate will generate the id:s for you. If you try to save an object with it's id set to something else than the "unsaved-value" mapping parameter is set to, you will IIRC get an Exception.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 5:47 am 
Beginner
Beginner

Joined: Sun Apr 04, 2004 8:45 am
Posts: 20
joib wrote:
When using the sequence generator, inserting a new object means:

1. SELECT next value from the sequence.

2. INSERT the new object into it's table using the value obtained in step #1.

So your table should be

Code:
CREATE TABLE KEYWORDS
(
   id INTEGER PRIMARY KEY,
   name VARCHAR(32) NOT NULL
);


If you want to avoid hitting the database twice for every insert you should use another generator. seqhilo, for example, works fine with PostgreSQL.


I did it. Here is my skript:
Code:
CREATE SEQUENCE id_sequence START 1 INCREMENT 1;

CREATE TABLE KEYWORDS
(
   id INTEGER PRIMARY KEY,
   name VARCHAR(32) NOT NULL
);


And here is the hbm.xml file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="hb.Keyword" table="keywords">
     
    <id name="id" type="integer" column="id">
        <generator class="sequence">
           <param>id_sequence</param>
        </generator>
   </id>

    <property name="name" column="name" not-null="true" unique="true" />
   
  </class>
</hibernate-mapping>


But, I have the same error message:
Code:
...

06.04.2004 11:38:05 net.sf.hibernate.cfg.Configuration configureCaches
INFO: instantiating and configuring caches
06.04.2004 11:38:06 net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Exception in thread "main" net.sf.hibernate.MappingException: Dialect does not support identity key generation
        at net.sf.hibernate.dialect.Dialect.getIdentitySelectString(Dialect.java:289)
        at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:633)
        at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:715)
        at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)
        at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
        at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:750)
        at Main.main(Main.java:24)
Press any key to continue...


Could you show a simple example with sequence or seqhilo which works, please? It would be a great help.

Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 6:59 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Well, obviously the .hbm.xml file that you're trying to map is not the same that you showed above, as the Exception tells that it still tries to use the identity generator.

Using seqhilo is as easy as sequence, once you get sequence to work you can just change "sequence" in the hbm.xml file to "seqhilo" and everything should work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 10:17 am 
Beginner
Beginner

Joined: Sun Apr 04, 2004 8:45 am
Posts: 20
joib wrote:
Well, obviously the .hbm.xml file that you're trying to map is not the same that you showed above, as the Exception tells that it still tries to use the identity generator.

Using seqhilo is as easy as sequence, once you get sequence to work you can just change "sequence" in the hbm.xml file to "seqhilo" and everything should work.


I've created a new project with a class Person as follow:
Code:
public class Person
{
   protected int id;
   protected String name;
   
   public Person() {}
   
   public Person(String name) { this.name = name;    }
   
   public int getId() { return id; }
   
   public String getName() { return name; }
   
   public void setId(int i) { id = i; }
   
   public void setName(String string) { name = string; }
}


The class Main is as follow defined:

Code:
// imports ...

public class Main
{

   public static void main(String[] args)
      throws MappingException, HibernateException
   {
      Configuration cfg = new Configuration();
      cfg.addClass(Person.class); // error line (line 19)

      SessionFactory sessions = cfg.buildSessionFactory();
      Session session = sessions.openSession();

      Transaction tx = null;
      Person p = new Person("John Smith");
   
      p.setName("John Smith");

      try
      {
         tx = session.beginTransaction();
         session.save(p);
         tx.commit();
      }
      catch (HibernateException he)
      {
         if (tx != null)
            tx.rollback();
         throw he;
      }
      finally
      {
         session.close();
      }
   }
}


The mapping file Person.hbm.xml is as follw:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="Person" table="t_personen">
     
    <id name="id" type="integer" column="id">
        <generator class="sequence">
           <param>id_sequence</param>
        </generator>
   </id>

    <property name="name" column="name" not-null="true" />
   
  </class>
</hibernate-mapping>


And lastly the hibernate.properties file:

Code:
hibernate.connection.username=student
hibernate.connection.password=geheim
hibernate.connection.url=jdbc:postgresql://localhost/personen
hibernate.connection.driver_class=org.postgresql.Driver
hibernate.dialect=net.sf.hibernate.dialect.PostgreSQLDialect   


I've created the database personen and with the follwong script the table t_personen:

Code:
CREATE SEQUENCE id_sequence START 1 INCREMENT 1;

CREATE TABLE t_personen
(
   id INTEGER PRIMARY KEY,
   name VARCHAR(32) NOT NULL
)


If I try to execute my project, I have the folloving message:

[code]...
06.04.2004 16:13:45 net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
06.04.2004 16:13:45 net.sf.hibernate.cfg.Configuration addClass
INFO: Mapping resource: Person.hbm.xml
06.04.2004 16:13:45 net.sf.hibernate.util.XMLHelper$ErrorLogger error
SCHWERWIEGEND: Error parsing XML: XML InputStream(11) Attributwert f


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 11:26 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
In your Person.hbm.xml, replace

Code:
<param>id_sequence</param>


with

Code:
<param name="sequence">id_sequence</param>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 12:13 pm 
Beginner
Beginner

Joined: Sun Apr 04, 2004 8:45 am
Posts: 20
joib wrote:
In your Person.hbm.xml, replace

Code:
<param>id_sequence</param>


with

Code:
<param name="sequence">id_sequence</param>

I changed it and now I came to line 21. After execution I have the follwing message:

Code:
06.04.2004 18:06:20 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
06.04.2004 18:06:20 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: cache provider: net.sf.ehcache.hibernate.Provider
06.04.2004 18:06:20 net.sf.hibernate.cfg.Configuration configureCaches
INFO: instantiating and configuring caches
06.04.2004 18:06:21 net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Exception in thread "main" 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:80)
        at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:631)
        at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:715)
        at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)
        at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
        at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:750)
        at Main.main(Main.java:21)
Caused by: net.sf.hibernate.MappingException: Dialect does not support sequences
        at net.sf.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:319)
        at net.sf.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:62)
        at net.sf.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:78)
        ... 7 more
Press any key to continue...


I tried with seqhilo to, but it was not better. If I try with hilo, I came to line 22, than in this case is the class javax/transaction/Synchronization missing. I will try to find out, in wich jar-file ist this class to find.

Could you say anything more about the message I have with sequence?

Best regards.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 5:36 am 
Newbie

Joined: Fri Jan 09, 2004 1:00 am
Posts: 3
Location: Shanghai, China
jta.jar is in lib/ of Hibernate distr.

I think you can try "native" generator-class and let hibernate to export schema DDL to DB for you. In this way, you can see what DDL Hibernate have generated for your code. Then, just learn form them.


Top
 Profile  
 
 Post subject: Dialect does not support identity key generation
PostPosted: Thu Apr 08, 2004 12:43 am 
Newbie

Joined: Thu Apr 08, 2004 12:38 am
Posts: 2
I have been getting the exact problem while running an application exemple at :

http://homepage.mac.com/edahand/project ... mple1.html

I use Tomcat 5.0.15, MySQL 3.23 and Struts 1.1.

here is my mapping :
<hibernate-mapping>
<class name="com.edhand.hibernate1.Item" table="item" >
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
</class>
</hibernate-mapping>

and my hibernate.properties.:

hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect
#hibernate.connection.driver_class = org.gjt.mm.mysql.Driver
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql:///test_hibernate
hibernate.connection.username = admin
hibernate.connection.password =

I don't know what is going on. I am new to hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2004 8:29 am 
Newbie

Joined: Fri Jan 09, 2004 1:00 am
Posts: 3
Location: Shanghai, China
Where's is the output of exceptions? It's helpful for us to solve your problem. :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2004 12:01 pm 
Newbie

Joined: Thu Apr 08, 2004 12:38 am
Posts: 2
Error shown in the browser:

org.apache.jasper.JasperException: net.sf.hibernate.MappingException: Dialect does not support identity key generation
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:274)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:320)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2004 8:04 am 
Newbie

Joined: Fri Jan 09, 2004 1:00 am
Posts: 3
Location: Shanghai, China
Tomcat has wrapped the exception :(
U should write a simple standalone application to test you persistent object with hibernate. It will bring you more information about the exception.

Last time i saw this exception in my code, it's because i lost a jar file in the classpath. I found it also from a exception output.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 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.