persistence@glassfish.java.net

Re: Scenario where the user wants to get the value GenerationType.SEQUENCE and getting value

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Tue, 14 Mar 2006 09:32:59 -0800

Linda,

Will it make sense to add such note to the spec?

thanks,
-marina

Linda DeMichiel wrote:
>
>
> Pramod Gopinath wrote:
>
>> Hi Linda
>> Can you clarify the spec for the below :
>> The user has defined the following for his ID field
>>
>> @Id
>> @GeneratedValue(strategy=GenerationType.SEQUENCE,
>> generator="USERS_SEQUENCE_GENERATOR")
>> @SequenceGenerator(name="USERS_SEQUENCE_GENERATOR",sequenceName="users_id_seq")
>>
>> @Column(name="id")
>> public Long getId() {
>> return id;
>> }
>>
>> And then they try this in their stateless bean
>> public Long createUser(String userName) {
>> User user = new User();
>> user.setName(userName);
>> em.persist(user);
>> return user.getId();
>> }
>>
>>
>> What is the expected value from the user.getId() call in the case
>> where they have used either a SEQUENCE or an IDENTITY generator.
>> Since in this case the value of the is obtained after the insert
>> statement has successfully completed, how is the user expected to get
>> the value of the id ?
>>
>
> Flush and refresh can be used.
>
>>
>>
>> Thanks
>> Pramod
>>
>>
>>
>>