Created
June 1, 2012 08:40
-
-
Save keating/2850398 to your computer and use it in GitHub Desktop.
for building sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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