-->
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.  [ 4 posts ] 
Author Message
 Post subject: Custom Id Generator with String type
PostPosted: Fri Mar 03, 2006 7:32 am 
Beginner
Beginner

Joined: Tue Aug 09, 2005 1:34 am
Posts: 26
Location: Bangalore
Hi,

Hibernate version:3.1

In our project we have the requirement to develop a Custom Id Generator
which should be of type String and the value in the column of the table
with the name of the table and Id generated by the sequence.

For Example: -
If name of the table is EMP the values in the Empno should go as EMP_100,
EMP_101, EMP_102 so on.


Actually once I found the code in net when I was searching long back while doing some RnD. Now I dont have the URL. If anybody knows any such URLs pls let me know.

Thanks in Advance.

Manjith A.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 9:28 am 
Regular
Regular

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

1. Some usefull links:

http://forum.hibernate.org/viewtopic.ph ... rgenerator

http://forum.hibernate.org/viewtopic.ph ... rgenerator

2. A implemntation of what you need is:

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 "EMP";
}
}

Enjoy.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 9:39 am 
Beginner
Beginner

Joined: Tue Aug 09, 2005 1:34 am
Posts: 26
Location: Bangalore
Hi,

Thanks For the solution.

I will try with the same and let you know if any issues.

I would be grateful to you If you can can tell me how do you map the
Identifier property in .hbm.xml if it differs from the normal id mapping.

Thanks
Manjith A


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 11:48 am 
Regular
Regular

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

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


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