-->
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.  [ 7 posts ] 
Author Message
 Post subject: custom id generator
PostPosted: Tue Mar 21, 2006 8:22 am 
Newbie

Joined: Tue Mar 21, 2006 8:07 am
Posts: 3
Hi ,
i have a user class ,it consists one is id as string type and another one is name which is also string type my data base table name is sampleTest , i have to generate id in db2 database which is this formate abc1, abc2,abc3 .... like this only , please tell me how can i implement , if any body solve my problem thanks to advance...waiting eagerly to reply ...

Thanks & Regards

Sandeep narela


Top
 Profile  
 
 Post subject: IdentifierGenerator
PostPosted: Tue Mar 21, 2006 11:51 am 
Newbie

Joined: Fri Aug 19, 2005 4:39 am
Posts: 5
You can implement your own generator, just implement IdentifierGenerator and in the Mapping file specify like this:

<id name="id" type="string" column="string_id">
<generator class="org.hibernate.YourCustomGenerator">
</generator>
</id>

You can have a look in the hibernate documentation (5.1.4 id).


Top
 Profile  
 
 Post subject: Re: IdentifierGenerator
PostPosted: Wed Mar 22, 2006 12:12 am 
Newbie

Joined: Tue Mar 21, 2006 8:07 am
Posts: 3
stefan_f wrote:
You can implement your own generator, just implement IdentifierGenerator and in the Mapping file specify like this:

<id name="id" type="string" column="string_id">
<generator class="org.hibernate.YourCustomGenerator">
</generator>
</id>

You can have a look in the hibernate documentation (5.1.4 id).

Hi Dear Stefan,

my concern to generate id like this ABC1, ABC2,ABC3....
in hiberrnate doc 5.1.4 only generate numeric number i want to generate alphnumeric number ,if u could provide me complete solution , i would be greatful to u.eagerly waiting to your reply.this is essentially need in my project and i have to implement so please give the complete way,so that i would be able to do implement id like ABC1,ABC2..... .

Thanks & Regards
sandeep


Top
 Profile  
 
 Post subject: IdentifierGenerator Implementor
PostPosted: Wed Mar 22, 2006 5:10 am 
Newbie

Joined: Fri Aug 19, 2005 4:39 am
Posts: 5
your generator class looks something like this:

public class AlphaNumericGenerator implements IdentifierGenerator {

public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
// implement your algorithm, e.g. you could select max(id) from your_table and add 1

return "ABC1";
}

}


and in the mapping specify <id>...<generator class=AlphaNumericGenerator>

thats all ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 5:55 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Here is your generator:

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;

public class MyGenerator implements IdentifierGenerator {

String sql = "select next from dual";

public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
return new String(getText().concat(getNext(session)));
}

private String getNext(SessionImplementor session) {
long next;

Connection conn = session.connection();

try {
PersistentIdentifierGenerator.SQL.debug(sql);
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = null;
try {
rs = st.executeQuery();
if (rs.next()) {
next = rs.getLong(1) + 1;
if (rs.wasNull())
next = 1;
} else {
next = 1;
}
sql = null;
} finally {
if (rs != null)
rs.close();
st.close();
}

} catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(session.getFactory()
.getSQLExceptionConverter(), sqle,
"could not fetch initial value", sql);
}

return new Long(next).toString();
}

public String getText() {
return "ABC";
}
}

Of course:

<id name="id" type="string" column="string_id">
<generator class="MyGenerator">
</generator>
</id>

like stefan_f suggested.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 2:38 am 
Newbie

Joined: Tue Mar 21, 2006 8:07 am
Posts: 3
[quote="TOMBATLECRUISEr"]Here is your generator:

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;

public class MyGenerator implements IdentifierGenerator {

String sql = "select next from dual";

public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
return new String(getText().concat(getNext(session)));
}

private String getNext(SessionImplementor session) {
long next;

Connection conn = session.connection();

try {
PersistentIdentifierGenerator.SQL.debug(sql);
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = null;
try {
rs = st.executeQuery();
if (rs.next()) {
next = rs.getLong(1) + 1;
if (rs.wasNull())
next = 1;
} else {
next = 1;
}
sql = null;
} finally {
if (rs != null)
rs.close();
st.close();
}

} catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(session.getFactory()
.getSQLExceptionConverter(), sqle,
"could not fetch initial value", sql);
}

return new Long(next).toString();
}

public String getText() {
return "ABC";
}
}

Of course:

<id name="id" type="string" column="string_id">
<generator class="MyGenerator">
</generator>
</id>

Hi dear,
i tried this code but given me error,i am sending my code to u.just check ,where i have to correction ?

******TestH .java**********
public class TestH {
private String next ;
private String text;

/**
* @return Returns the id.
*/

/**
* @param string
*/

/**
* @return Returns the next.
*/
public String getNext() {
return next;
}
/**
* @param next The next to set.
*/
public void setNext(String next) {
this.next = next;
}
/**
* @return Returns the text.
*/
public String getText() {
return text;
}
/**
* @param text The text to set.
*/
public void setText(String text) {
this.text = text;
}
}
############# this is my TestH.hbm.xml#########
<?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 package="com.test">

<class name="TestH" table="sampleTest" >
<id name="next" type="string">
<generator class="com.test.MyGenerator" >

</generator>
</id>
<property name="text" type="string" />
</class>

</hibernate-mapping>

*********This is my MyGenerator.java class************
package com.test;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;

public class MyGenerator implements IdentifierGenerator {

String sql = "select next from sampleTest";

public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
return new String(getText().concat(getNext(session)));
}

private String getNext(SessionImplementor session) {
long next;

Connection conn = session.connection();

try {
PersistentIdentifierGenerator.SQL.debug(sql);
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = null;
try {
rs = st.executeQuery();
if (rs.next()) {
next = rs.getLong(1) + 1;
if (rs.wasNull())
next = 1;
} else {
next = 1;
}
sql = null;
} finally {
if (rs != null)
rs.close();
st.close();
}

} catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(session.getFactory()
.getSQLExceptionConverter(), sqle,
"could not fetch initial value", sql);
}

return new Long(next).toString();
}

public String getText() {
return "ABC";
}
}
%%%%%%%%%% This is my test porgram%%%%%%%%%

package com.test;

import org.hibernate.Session;
import org.hibernate.Transaction;

/**
* @author sandeepn
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestRun {
public static void main(String args[])
{
TestH testH = new TestH();
//testH.setNext("abc2");
testH.setText("dddd");
System.out.println("insideTestRun1");
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
System.out.println("insideTestRun2");
session.save(testH);
System.out.println("insideTestRun3");
tx.commit();
}
}

When i run this code i got this error just see dear...where i have take correactive action to do generate my alphanumeric id properly ,waiting eagerly to your reply ..... a lot of thanks in advance

&&&&&&&&&Error which i am getting to running test program&&&&&&&
insideTestRun1
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
insideTestRun2
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at com.test.MyGenerator.getNext(MyGenerator.java:49)
at com.test.MyGenerator.generate(MyGenerator.java:20)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at com.test.TestRun.main(TestRun.java:33)
Caused by: com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -206, SQLSTATE: 42703, SQLERRMC: ID
at com.ibm.db2.jcc.b.co.e(co.java:1361)
at com.ibm.db2.jcc.b.co.a(co.java:984)
at com.ibm.db2.jcc.a.bd.g(bd.java:121)
at com.ibm.db2.jcc.a.bd.a(bd.java:42)
at com.ibm.db2.jcc.a.r.a(r.java:31)
at com.ibm.db2.jcc.a.bp.g(bp.java:103)
at com.ibm.db2.jcc.b.co.g(co.java:968)
at com.ibm.db2.jcc.b.cp.T(cp.java:1378)
at com.ibm.db2.jcc.b.cp.d(cp.java:1781)
at com.ibm.db2.jcc.b.cp.K(cp.java:316)
at com.ibm.db2.jcc.b.cp.executeQuery(cp.java:299)
at com.test.MyGenerator.getNext(MyGenerator.java:33)
... 10 more


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 6:17 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

I think here is the problem. Are you sure this sql is returnign something?

String sql = "select next from sampleTest";

As far as I know it should be something like: "select hibernate_sequence.nextval from dual".

The sql I've provided was just a sample. In my application I get the next value from another table and I have a totally different sql.

Hope this solve your problem (and of course - rate if it's helpfull).


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