Hi,
Here is your generator:
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";
}
}
Hope it solve your problem.
|