NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
ndoxx
Joined: 06 Apr 2010 Posts: 3
|
Posted: Tue Apr 06, 2010 2:53 pm Post subject: EntityManager persist does not return generated id at inherited entity |
|
|
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
|
Posted: Wed Apr 07, 2010 3:45 am Post subject: |
|
|
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
|
Posted: Wed Apr 07, 2010 8:56 pm Post subject: Re: EntityManager persist does not return generated id at inherited entity |
|
|
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
|
Posted: Fri Apr 09, 2010 4:29 am Post subject: |
|
|
Thanks jhalupka, it works.
Problem solved. |
|
| Back to top |
|
 |
troy giunipero Posted via mailing list.
|
Posted: Fri Apr 09, 2010 11:18 am Post subject: Re: EntityManager persist does not return generated id at inherited entity |
|
|
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 |
|
 |
|
|
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
|
|