문서의 이전 판입니다!
Real MySQL 1판 922페이지
create table jpa_sequences ( name varchar(16) NOT NULL, curval bigint not null, primary key(name)) ENGINE=MyISAM;
insert into jpa_sequences set name='books', curval=(@v_current_value:=1) on duplicate key update curval=(@v_current_value:=curval+1);
select @v_current_value
final EntityManager entityManager = getEntityManager(); final Query generateNextSequenceQuery = entityManager.createNativeQuery("insert into jpa_sequences set name=?, curval=(@v_current_value\\:=1) on duplicate key update curval=(@v_current_value\\:=curval+1);"); generateNextSequenceQuery.setParameter(1, sequenceName); generateNextSequenceQuery.executeUpdate(); final Query nextSequenceQUery = entityManager.createNativeQuery("select @v_current_value;"); Object nextSequenceObject = nextSequenceQUery.getSingleResult();
if(nextSequenceObject instanceof BigInteger) { return ((BigInteger)nextSequenceObject).longValue(); }
return (Long)nextSequenceObject;