Skip to content

Instantly share code, notes, and snippets.

@keating
Created June 1, 2012 08:40
Show Gist options
  • Save keating/2850398 to your computer and use it in GitHub Desktop.
Save keating/2850398 to your computer and use it in GitHub Desktop.
for building sequence
import static com.beiyanght.jpa.ThreadLocalEntityManager.em;
import java.math.BigDecimal;
/**
*
* @author keating_andy_given
*/
public class SequenceHelp {
/**
* 获取ID
*/
public static int computeId(int increaseSize, String keyName) {
BigDecimal value = null;
try {
value = (BigDecimal) em().createNativeQuery(
"select t.gen_value from ID_SEQUENCE t where t.keyid='"
+ keyName + "'").getSingleResult();
}catch(javax.persistence.NoResultException e) {
em().createNativeQuery(
"insert into ID_SEQUENCE(keyid,GEN_VALUE) values('"
+ keyName + "','" + increaseSize + "')")
.executeUpdate();
return increaseSize;
}
em().createNativeQuery(
"update ID_SEQUENCE set GEN_VALUE = '"
+ (value.toBigInteger().longValue() + increaseSize)
+ "' where keyid='" + keyName + "'").executeUpdate();
return value.toBigInteger().intValue() + increaseSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment