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.