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.  [ 3 posts ] 
Author Message
 Post subject: increment generator question
PostPosted: Wed Feb 21, 2007 6:48 am 
Newbie

Joined: Mon Feb 19, 2007 1:44 am
Posts: 8
Hi,

I want to add the generator tag in the mapping file. I want to give increment class.
What should be the parameter i need to use if i want the id start with 100 since by default it start with only 1.

<generator class="increment">

</generator>


Somebody please help me.

Thanks and regards,
Jacob Baby


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 3:10 pm 
Newbie

Joined: Tue Feb 20, 2007 10:19 am
Posts: 4
There isn't a parameter to set the initial value for the increment generator. The initial value of 1 is hardcoded in the org.hibernate.id.IncrementGenerator class.

To solve your problem, you could use your own id generator class.

Following is a working example. It assumes MySQL, but you could use any database with some adjustments.

The example.IncrementGenerator class is a copy of org.hibernate.id.IncrementGenerator, but with code added to set the initial id value from the initial_value parameter.

1. Create test database.
2. Create table (e.g. use create.sql) in test database.
3. Create a user with access to the person table (e.g. user = testuser, pass = testpass)
4. Compile the following:
src\hibernate.cfg.xml
src\example\IncrementGenerator.java,
src\example\Main.java
src\example\Person.hbm.xml
src\example\Person.java
5. Run Main.class
The first inserted record should have id = 100.

To compile, you need these dependencies:
- hibernate 3

To run, you need these dependencies:
- hibernate 3
- MySQL JDBC driver

--- create.sql ---
Code:
create table person (id bigint not null, name varchar(50) not null, primary key (id));


--- src\hibernate.cfg.xml ---
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">jdbc:mysql://localhost/test</property>
  <property name="connection.username">testuser</property>
  <property name="connection.password">testpass</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <mapping resource="example/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>



--- src\example\IncrementGenerator.java ---
Code:
package example;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentifierGeneratorFactory;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.mapping.Table;
import org.hibernate.type.Type;
import org.hibernate.util.StringHelper;

public class IncrementGenerator implements IdentifierGenerator, Configurable {

        private static final Log log = LogFactory.getLog(IncrementGenerator.class);

        private long next;
        private String sql;
        private Class returnClass;
        private long initialValue;

   public synchronized Serializable generate(SessionImplementor session, Object object)
        throws HibernateException {

                if (sql!=null) {
                        getNext( session );
                }
                return IdentifierGeneratorFactory.createNumber(next++, returnClass);
        }

        public void configure(Type type, Properties params, Dialect dialect)
        throws MappingException {

                String tableList = params.getProperty("tables");
                if (tableList==null) tableList = params.getProperty(PersistentIdentifierGenerator.TABLES);
                String[] tables = StringHelper.split(", ", tableList);
                String column = params.getProperty("column");
                if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
                String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
                String catalog = params.getProperty(PersistentIdentifierGenerator.CATALOG);
                returnClass = type.getReturnedClass();
      

                StringBuffer buf = new StringBuffer();
                for ( int i=0; i<tables.length; i++ ) {
                        if (tables.length>1) {
                                buf.append("select ").append(column).append(" from ");
                        }
                        buf.append( Table.qualify( catalog, schema, tables[i] ) );
                        if ( i<tables.length-1) buf.append(" union ");
                }
                if (tables.length>1) {
                        buf.insert(0, "( ").append(" ) ids_");
                        column = "ids_." + column;
                }
      
                sql = "select max(" + column + ") from " + buf.toString();

                initialValue = getLong("initial_value", params, 1);
        }

        private void getNext( SessionImplementor session ) {

                log.debug("fetching initial value: " + sql);
      
                try {
                        PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
                        try {
                                ResultSet rs = st.executeQuery();
                                try {
                                        if ( rs.next() ) {
                                                next = rs.getLong(1) + 1;
                                                if ( rs.wasNull() ) next = initialValue;
                                        }
                                        else {
                                                next = initialValue;
                                        }
                                        sql=null;
                                        log.debug("first free id: " + next);
                                }
                                finally {
                                        rs.close();
                                }
                        }
                        finally {
                                session.getBatcher().closeStatement(st);
                        }
         
                }
                catch (SQLException sqle) {
                        throw JDBCExceptionHelper.convert(
                                        session.getFactory().getSQLExceptionConverter(),
                                        sqle,
                                        "could not fetch initial value for increment generator",
                                        sql
                                );
                }
        }

        public static long getLong(String property, Properties properties, long defaultValue) {
            String propValue = properties.getProperty(property);
            return propValue == null ? defaultValue : Long.parseLong(propValue.trim());
        }

}



--- src\example\Main.java ---
Code:
package example;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Main {
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        try {
            Transaction transaction = session.beginTransaction();
            try {
                Person person = new Person();
                person.setName("John");
                session.save(person);
            } catch (Exception ex) {
                transaction.rollback();
                throw ex;
            }
            transaction.commit();
        } finally {
            session.close();
        }
    }
}



--- src\example\Person.hbm.xml ---
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="example.Person" table="person">
  <id name="id">
   <generator class="example.IncrementGenerator">
    <param name="initial_value">100</param>
   </generator>
  </id>
  <property name="name"/>
</class>
</hibernate-mapping>



--- src\example\Person.java ---
Code:
package example;

public class Person {
    private Long id;
    private String name;
    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 1:19 am 
Newbie

Joined: Mon Feb 19, 2007 1:44 am
Posts: 8
thanks a lot for your prompt answer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

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.