hibernate - Can EntityManager not assign an id? -


are there specified cases in jpa2 when merge not assign @id variable ? , yet return new instance of entity without firing exception ?

say i'm having hierarchy:

@mappedsuperclass abstract class bar {    @id   @generatedvalue(strategy = generationtype.auto)   private long id;    ... }  @entity @table(name = "bar_1s") @access(accesstype.property) class bar1 extends bar {   ... }  @entity @table(name = "bar_2s") @access(accesstype.property) class bar2 extends bar {   ... } 

for yet found reason instances of bar1 id after merge() while instances of bar2 don't. @ least that's looks like. i've been trying various approaches @ no prevail.

it looks hibernate (4.1.4.final) doesn't want assign id instances of particular class. :-)

the questions have:

  • did ever had ?

  • can tell me in hibernate id's set ? can debug part of code , find out why skips assignment. intellij doesn't seem break on modification of fields in entities.

edit - environment configuration

  • os: lmde amd64
  • database: mysql (5.1.61-2) connectj (5.1.17)
  • jvm: 1.7.0_04
  • hibernate: 4.1.4.final
  • spring: 3.1.1

edit - code

all entity classes (in)direct subclasses of bar. there 1 place @id defined. , there few identical cases of bar2 do id. bar2 doesn't.

final bar1 bar = new bar1(); jpatemplate.merge(bar); <-- gets id

final bar2 bar = new bar2(); jpatemplate.merge(bar); <-- has no id

the application using jpatemplate of spring merge entities. runs okay exception particular class.

so, if find hibernate class assigning id find out silly detail overlooked. :-)

first note: jpatemplate deprecated, , should not used anymore. delegates entitymanager's merge() method, , both return entity.

here's jpa spec says merge:

if x new entity instance, new managed entity instance x' created , state of x copied new managed entity instance x'.

so, if id generated, assigned attached entity, , not object pass merge method. should never ignore result of merge method. instead of

jpatemplate.merge(bar); 

you should do:

bar = jpatemplate.merge(bar); 

moreover, jpa spec says

a generated id not guaranteed available until after database insert has occurred.

and other reason why don't have id. afaik, auto strategy, mysql, consists in relying on autoincrement id column in database, , it's possible value of id after insert has been done, , hibernate waits until absolutely necessary flush, , perform insert. if need have id right after merge, following:

bar = jpatemplate.merge(bar); jpatemplate.flush(); long id = bar.getid(); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -