NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

EntityManager persist does not return generated id at inherited entity

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
ndoxx



Joined: 06 Apr 2010
Posts: 3

PostPosted: Tue Apr 06, 2010 2:53 pm    Post subject: EntityManager persist does not return generated id at inherited entity Reply with quote

Hi all,

I have problem with entitymanager and inherited entity. I have entity class hierarchy like this:

Code:

// PARENT ENTITY

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="account_type", discriminatorType=DiscriminatorType.STRING)
@SequenceGenerator(name = "user_acc_seq", sequenceName="user_account_user_id_seq", allocationSize=1)
@Table(name = "user_account")
@NamedQueries(...)
public abstract class UserAccount implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="user_acc_seq")
    @Basic(optional = false)
    @Column(name = "user_id")
    private Long userId;

...


Code:

// CHILD ENTITY

@Entity
@Table(name = "user_account_individual")
@PrimaryKeyJoinColumn(name="user_id", referencedColumnName="user_id")
@DiscriminatorValue("INDIVIDUAL")
@NamedQueries(...)
public class UserAccountIndividual extends UserAccount{
....


which i use like this
Code:

UserAccount account = new UserAccountIndividual()
...
em.persist(account);


after calling persist, the user_id still returns null.

This was not happening if I remove the inheritance relationship in both entities (each entities stands it owns and does not have any relationship).

so when i remove the inheritance, returns the parent class to concrete class, and call em.persist(account), the account id has the auto generated value.

Does this have something to do with inheritance properties set up incorrectly?

Thanks,
Ndoxx
Back to top
ndoxx



Joined: 06 Apr 2010
Posts: 3

PostPosted: Wed Apr 07, 2010 3:45 am    Post subject: Reply with quote

another info, i have also tried to call em.flush() after persist to no avail.

Thanks.
Ndoxx
Back to top
jhalupka



Joined: 05 Feb 2010
Posts: 52

PostPosted: Wed Apr 07, 2010 8:56 pm    Post subject: Re: EntityManager persist does not return generated id at inherited entity Reply with quote

After you persist the entity, call em.refresh(account).

This forces the persistence layer to generate the ID for the entity. That
is, it responds as if the entity had been committed to the database. Note
that the entity will not physically exist in the database until you commit
the transaction. (I'm assuming your code is part of a transaction.)


ndoxx-2 wrote:
Quote:

I have problem with entitymanager and inherited entity. I have entity
class hierarchy like this:

Code:

UserAccount account = new UserAccountIndividual()
...
em.persist(account);

after calling persist, the user_id still returns null.



--
View this message in context: http://old.nabble.com/EntityManager-persist-does-not-return-generated-id-at-inherited-entity-tp28153080p28170649.html
Sent from the Netbeans - J2EE mailing list archive at Nabble.com.
Back to top
ndoxx



Joined: 06 Apr 2010
Posts: 3

PostPosted: Fri Apr 09, 2010 4:29 am    Post subject: Reply with quote

Thanks jhalupka, it works.

Problem solved.
Back to top
troy giunipero
Posted via mailing list.





PostPosted: Fri Apr 09, 2010 11:18 am    Post subject: Re: EntityManager persist does not return generated id at inherited entity Reply with quote

For the sake of conversation, how did you solve it? I was under the
impression that calling em.refresh() would update the persistence
context created by your transaction with the database. But if you are
in a transaction and require an id for a newly created record (i.e.,
record exists in the persistence context but not yet in the database),
you would need to call em.flush() to flush changes in the persistence
context to the database first.

Did you use refresh(), flush(), or solve it by some other means entirely?



ndoxx wrote:
Quote:
Thanks jhalupka, it works.

Problem solved.




Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo