[jboss-user] [EJB 3.0] - Re: Use oracle to generate the id with the orcle seqence tab

sraick do-not-reply at jboss.com
Tue Sep 18 08:48:49 EDT 2007

What I do here, not very clean but it works,

I have created an annotation called Sequence and then I place it on my bean as follow 
@Sequence(name="TABLE_SEQ") where table_seq is the name of my Oracle sequence.  

In my persistent class, I create a new persistent bean when needed with empty properties.  But I set already its id by doing this (pb is my persistent bean):

  | StringBuilder sb = new StringBuilder("select ");
  |             Sequence seqAnnotation = persistentBeanClass.getAnnotation(Sequence.class);
  |             if (seqAnnotation != null)
  |                sb.append(seqAnnotation.name());
  |             sb.append(".nextVal from dual");
  |             Query query = entityManager.createNativeQuery(sb.toString());
  |             Long id = new Long(((Number) query.getSingleResult()).longValue());
  |             pb.setId(id);

So your bean has the same id generated by the Oracle sequence, before comit.  Hope this helps

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4085466#4085466

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4085466

More information about the jboss-user mailing list