-->
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.  [ 6 posts ] 
Author Message
 Post subject: pk generation using diferent table
PostPosted: Mon Nov 03, 2003 1:59 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 4:32 pm
Posts: 36
Location: S
I have a sequence table called tb_sequence :

Code:
cd_sequence varchar2(20) primary key
vl_sequence numeric(9)


and the other tables catch the next sequence using that table:

for example :

table tb_customer :
Code:

cd_Customer  numeric(9) primary key
ds_Name       varchar2(5)
...



To get the next sequence, I search in tb_sequence using as pk : 'TB_CUSTOMER' and catch the value of vl_sequence.
Each table has a record on tb_sequence itself

My question is : How I do that using <id generator> from Hibernate features ??

Thanks in advance

Pedro Herrera


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2003 2:35 pm 
Newbie

Joined: Sat Oct 11, 2003 1:47 pm
Posts: 17
You have to write a Generator to satisfy this condition.
I did a similar one and maybe it would help

Code:
package com.yoyo.hibernate.idgen;

import com.yoyo.CounterPersist;
import com.yoyo.persistentfactory.CounterFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.dialect.Dialect;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.id.Configurable;
import net.sf.hibernate.id.PersistentIdentifierGenerator;
import net.sf.hibernate.type.Type;
import net.sf.hibernate.util.PropertiesHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import java.util.Properties;

/**
* Generates Sequence numbers for individual entities.
* A Separate table holds keys that correspond to entities and values that need to
* be incremented and returned for the next id. Based on the "Sequence Blocks" Pattern
* From EJBDesign Patterns.
*/

public class SequenceGenerator implements PersistentIdentifierGenerator, Configurable {

    private static String KEY = "key";
    private static String TABLE = "table";
    /** Key - Name of the entity in the Counter Table **/
   private String key;

    /** Table - Name of the table that maintains key information **/
    private String table;

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

   public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
      this.key    = PropertiesHelper.getString(KEY, params, "");
        this.table  = PropertiesHelper.getString(TABLE, params, "T_COUNTER");
        log.info("SequenceGenerator configured with key: " + this.key + " and table: " + this.table);
   }

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

        CounterPersist counter = CounterFactory.getCounterPersistence("Hibernate");
        int nextValue = counter.getCounter(this.key).getNextValue();
        log.debug("Sequence ID generated: " + nextValue);
        return new Integer(nextValue);
   }

   public String sqlDropString(Dialect dialect) throws HibernateException {
      return "";
   }

    public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
        return new String[0];
    }

    public Object generatorKey() {
      return "com.yoyo.hibernate.idgen.SequenceGenerator";
   }

}


Use your own logic to get the sequence number in the generate method above.

Your Sequence generator in the code will be something like(using XDoclet)
/**
* @hibernate.id column="addressid"
* unsaved-value="null"
* generator-class="com.yoyo.hibernate.idgen.SequenceGenerator"
* @hibernate.generator-param name="table" value="T_COUNTER"
* @hibernate.generator-param name="key" value="Address"
* @return
*/


-Raj


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2003 3:37 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 4:32 pm
Posts: 36
Location: S
What are the classes CounterPersist and CounterFactory ?
How do I implement that ??


Thanks for your help


Pedro Herrera


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2003 4:58 pm 
Newbie

Joined: Sat Oct 11, 2003 1:47 pm
Posts: 17
That is my implementation of incrementing the counter.

You need to replace the logic in the generate method to suite your need. Basically look for your key and if there, increment value and if not, create and increment value.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 8:30 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 4:32 pm
Posts: 36
Location: S
I


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 10:39 am 
Newbie

Joined: Sat Oct 11, 2003 1:47 pm
Posts: 17
You dont have to implement CounterPersist. Thats one of the interfaces in *MY* Code to do the counter mechanism. You might have a different way to deal with the counters(like direct JDBC). So your Class that creates the primary key would be something like
Code:
public class SequenceGenerator implements PersistentIdentifierGenerator, Configurable {
..................

   public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
              //Use JDBC or some other mechanism to create the key and return the value.
   }
........................
}


Hope that answers your question.


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