Vous êtes sur la page 1sur 10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server

Login FAQ

Register Search

Vie w unanswe re d posts | Vie w active topics Board index Hibernate & Java Persistence Hibernate Users All tim e s are UTC - 5 hours [ DST ]

@EmbeddedId that contains an Identity column on SQL Server


Page 1 of 1 [ 6 posts ] Previous topic | Next topic

Author
angusgr Ne wbie Joined: W e d Fe b 18, 2009 5:03 pm Posts: 5

Message
Post subject: @Em be dde dId that contains an Ide ntity colum n on SQ L Se rve r Posted: Thu O ct 08, 2009 3:08 am

I'm working on a schema requiring a composite primary key, something like:

Code:

@ E n t i t y p u b l i cc l a s sE v e n t{ @ E m b e d d e d I d p r i v a t eE v e n t K e yi d ; } @ E m b e d d a b l e p u b l i cc l a s sE v e n t K e y{ @ G e n e r a t e d V a l u e p u b l i cL o n gi d ; @ C o l u m n ( n u l l a b l e = f a l s e ) @ T e m p o r a l ( T e m p o r a l T y p e . T I M E S T A M P ) p u b l i cD a t et i m e s t a m p ; }

I want "id" in the composite key to be the IDENTITY column on the table, but that doesn't work. As shown above, the embedded id does not get treated like a generated value. DDL generation does not mark the table as an IDENTITY column at all. If you change it to this:
Code:

@ G e n e r a t e d V a l u e ( s t r a t e g y = G e n e r a t i o n T y p e . I D E N T I T Y ) @ G e n e r a t e d ( G e n e r a t i o n T i m e . I N S E R T ) @ C o l u m n ( c o l u m n D e f i n i t i o n = " B I G I N TI D E N T I T Y ( 1 , 1 ) " ) p r i v a t eL o n gi d ;

... then hbm2dll creates an Identity column (as it's explicitly there), but Hibernate doesn't recognise it. It tries to insert "NULL" as an explicit value, which is illegal. If you do this:
https://forum.hibernate.org/viewtopic.php?f=1&t=1000166 1/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
Code:

@ I d @ G e n e r a t e d V a l u e p u b l i cL o n gi d ;

Then you get "EventKey must not have @Id properties when used as an @EmbeddedId". Is there any way to make this work? I would have thought having @GeneratedValue inside an @EmbeddedId would infer that this was the IDENTITY column. Do I perhaps need to use @IdClass instead, and specify the @Id fields separately on every Entity class? Any/all help appreciated. :)

Top Post subject: R e : @Em be dde dId that contains an Ide ntity colum n on SQ L Se rve r Posted: Thu O ct 08, 2009 7:01 pm

angusgr

Ne wbie Joined: W e d Fe b 18, 2009 5:03 pm Posts: 5 Top

@IdClass doesn't seem to work either, gives the same results as described above.

angusgr

Post subject: R e : @Em be dde dId that contains an Ide ntity colum n on SQ L Se rve r Posted: Thu O ct 08, 2009 8:14 pm

Ne wbie Joined: W e d Fe b 18, 2009 5:03 pm Posts: 5

OK, further Googling has revealed that this won't work - you can't combine EmbeddedId with a GeneratedValue. Hibernate forum post where it's stated plainly: https://forums.hibernate.org/viewtopic. ... 130987bc22 Some discussion about this limitation in the spec; http://www.jboss.org/index.html?module= ... &p=3928306 and a related feature request. Apparently the workaround is to use UserType, so I'm off to read up on that now: http://opensource.atlassian.com/project ... se/ANN-268

Top Post subject: R e : @Em be dde dId that contains an Ide ntity colum n on SQ L Se rve r Posted: Tue Mar 30, 2010 4:36 pm

glh

Ne wbie Joined: Tue Jan 11, 2005 6:28 pm Posts: 3

This Oct 2009 thread included the suggestion that a workaround would involve a UserType implementation. I implemented UserType to deal with enumerations back in Hibernate 2 days, but it is not clear to me how a UserType would help implement a composite PK especially one that includes a sequence column. Can anyone provide an overview of how that would work?

Top Post subject: R e : @Em be dde dId that contains an Ide ntity colum n on SQ L Se rve r Posted: Thu O ct 07, 2010 10:20 am

Funkybreizh

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

2/10

1/4/14
Ne wbie

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server

up I have the same question

Joined: Thu O ct 07, 2010 10:16 am Posts: 4

Top laurents59 Ne wbie Joined: Fri O ct 19, 2012 7:37 am Posts: 1 Post subject: R e : @Em be dde dId that contains an Ide ntity colum n on SQ L Se rve r Posted: Fri O ct 19, 2012 7:43 am

I've got trapped in the same problem and decided to share my solution. I build my own EmbeddedSequenceGenerator which will, when well configured, allow to set a generated value in an embeddedId. Use case is very simple but it worked for me. Hope it'll help :

Code:

p a c k a g eo r g . h i b e r n a t e . i d ; i m p o r tj a v a . i o . S e r i a l i z a b l e ; i m p o r tj a v a . u t i l . P r o p e r t i e s ; i m p o r to r g . h i b e r n a t e . H i b e r n a t e E x c e p t i o n ; i m p o r to r g . h i b e r n a t e . M a p p i n g E x c e p t i o n ; i m p o r to r g . h i b e r n a t e . d i a l e c t . D i a l e c t ; i m p o r to r g . h i b e r n a t e . e n g i n e . s p i . S e s s i o n I m p l e m e n t o r ; i m p o r to r g . h i b e r n a t e . t y p e . P r i m i t i v e T y p e ; i m p o r to r g . h i b e r n a t e . t y p e . T y p e ; / * *S e q u e n c eg e n e r a t o rf o rs p e c i f i e df i e l di nE m b e d d e d I d . < b r/ > * *T h i sG e n e r a t o rn e e d sm u l t i p l ep r o p e r t i e st ob es e t: *< u l > < l i > c l a s s N a m e:t h ec l a s sn a m eo ft h ee n t i t yp e r s i s t e d < / l i > * * * * * < l i > i d G e t t e r M e t h o d:t h eg e t t e r M e t h o dn a m ef o rt h ei d < / l i > < l i > i d C l a s s N a m e:t h ec l a s sn a m eo ft h eE m b e d d e d I d < / l i > < l i > i d C l a s s H i b e r n a t e T y p e:t h ec l a s sn a m eo ft h eG e n e r a t e dV a l u eb y < l i > g e n e r a t e d F i e l d G e t t e r M e t h o d:t h eg e t t e rm e t h o do nt h ei df o rt h eg e n e r a t e d < l i > g e n e r a t e d F i e l d S e t t e r M e t h o d:t h es e t t e rm e t h o do nt h ei df o rt h eg e n e r a t e d

H i b e r n a t e < / l i > v a l u e < / l i > v a l u e < / l i > *< / u l > * *E x a m p l eo fu s e: *& # 6 4 ; G e n e r i c G e n e r a t o r ( * * * * * * * * * * * n a m e=" O P E R A T I O N _ I D _ S E Q " , s t r a t e g y=" f r . s f r . s i v p n . p e r s i s t a n c e . s e q u e n c e . E m b e d d e d S e q u e n c e G e n e r a t o r " , p a r a m e t e r s= { & # 6 4 ; P a r a m e t e r ( n a m e=" s e q u e n c e " ,v a l u e=" I D _ R T E _ S E Q " ) , & # 6 4 ; P a r a m e t e r ( n a m e=" c l a s s N a m e " ,v a l u e= & # 6 4 ; P a r a m e t e r ( n a m e=" i d G e t t e r M e t h o d " ,v a l u e=" g e t I d " ) , & # 6 4 ; P a r a m e t e r ( n a m e=" i d C l a s s N a m e " ,v a l u e= & # 6 4 ; P a r a m e t e r ( n a m e=" g e n e r a t e d F i e l d G e t t e r M e t h o d " ,v a l u e=" g e t I d O p " ) , & # 6 4 ; P a r a m e t e r ( n a m e=" i d C l a s s H i b e r n a t e T y p e " ,v a l u e= & # 6 4 ; P a r a m e t e r ( n a m e=" g e n e r a t e d F i e l d S e t t e r M e t h o d " ,v a l u e=" s e t I d O p " )

" f r . s f r . s i v p n . p e r s i s t a n c e . e n t i t y . e n g i n e . O p e r a t i o n E n t i t y " ) ,

" f r . s f r . s i v p n . p e r s i s t a n c e . e n t i t y . i d s . O p e r a t i o n I d " ) ,

" o r g . h i b e r n a t e . t y p e . L o n g T y p e " ) ,

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

3/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
* * *@ a u t h o rl s c h o e l e n s *@ v e r s i o n1 . 0 *@ s i n c e1 . 0( 2 0 1 2 / 1 0 / 1 9 ) * / p u b l i cc l a s sE m b e d d e d S e q u e n c e G e n e r a t o re x t e n d sS e q u e n c e G e n e r a t o r { / * *E n t i t yc l a s sp a r a mn a m e .* / p u b l i cs t a t i cf i n a lS t r i n gC L A S S _ N A M E=" c l a s s N a m e " ; / * *P a r a m t r ep o u rd f i n i rl eg e t t e rd el ' i ds u rl ' e n t i t .* / p u b l i cs t a t i cf i n a lS t r i n gI D _ G E T T E R _ M E T H O D=" i d G e t t e r M e t h o d " ; / * *P a r a m t r ep o u rd f i n i rl ac l a s s ed el ' i dd el ' e n t i t .* / p u b l i cs t a t i cf i n a lS t r i n gI D _ C L A S S _ N A M E=" i d C l a s s N a m e " ; / * *P a r a m t r ep o u rd f i n i rl ac l a s s eh i b e r n a t ed el ' i dd el ' e n t i t .* / p u b l i cs t a t i cf i n a lS t r i n gI D _ C L A S S _ H I B E R N A T E _ T Y P E=" i d C l a s s H i b e r n a t e T y p e " ; / * *P a r a m t r ep o u rd f i n i rl eg e t t e rd uc h a m pg n r .* / p u b l i cs t a t i cf i n a lS t r i n gI D _ G E T T E R _ G E N E R A T E D _ F I E L D= " g e n e r a t e d F i e l d G e t t e r M e t h o d " ; / * *P a r a m t r ep o u rd f i n i rl es e t t e rd uc h a m pg n r .* / p u b l i cs t a t i cf i n a lS t r i n gI D _ S E T T E R _ G E N E R A T E D _ F I E L D= " g e n e r a t e d F i e l d S e t t e r M e t h o d " ; / * *L ac l a s s ed el ' e n t i t .* / p r i v a t eC l a s s < ? >c l a z z ; / * *L am t h o d ed er c u p r a t i o nd el ' i d .* / p r i v a t ej a v a . l a n g . r e f l e c t . M e t h o dg e t t e r M e t h o d ; / * *L ac l a s s ed el ' e n t i t .* / p r i v a t eC l a s s < ? >i d C l a z z ; / * *L ac l a s s eh i b e r n a t ed el ' i dg n r a us e i nd el ' i d .* / p r i v a t eT y p ei d T y p e ; / * *L am t h o d ed er c u p r a t i o nd el av a l e u rg n r e rs u rl ' i d .* / p r i v a t ej a v a . l a n g . r e f l e c t . M e t h o dg e t t e r G e n e r a t e d V a l u e M e t h o d ; / * *L am t h o d ed er c u p r a t i o nd el av a l e u rs e t t e rs u rl ' i d .* / p r i v a t ej a v a . l a n g . r e f l e c t . M e t h o ds e t t e r G e n e r a t e d V a l u e M e t h o d ; / * *C o n f i g u r et h es u p e rg e n e r a t o rw i t hr i g h tH i b e r n a t e T y p ef o rt h eg e n e r a t e d v a l u e . < b r/ > *T h e nc o n f i g u r em e t h o d sa c c e s sa n dc l a s sn a m e sf o rt h eg e n e r a t i o np r o c e s s . * *T O D O:a d daw a yt oa c c e s sp r o p e r t yd i r e c t l y . *@ s e eo r g . h i b e r n a t e . i d . S e q u e n c e G e n e r a t o r # c o n f i g u r e ( * * / @ O v e r r i d e p u b l i cv o i dc o n f i g u r e ( T y p et y p e ,P r o p e r t i e sp a r a m s ,D i a l e c td i a l e c t ) t h r o w sM a p p i n g E x c e p t i o n { f i n a lS t r i n gi d T y p e N a m e=p a r a m s . g e t P r o p e r t y ( I D _ C L A S S _ H I B E R N A T E _ T Y P E ) ; o r g . h i b e r n a t e . t y p e . T y p e ,j a v a . u t i l . P r o p e r t i e s , o r g . h i b e r n a t e . d i a l e c t . D i a l e c t ) } )

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

4/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
i f( i d T y p e N a m e= =n u l l ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ C L A S S _ H I B E R N A T E _ T Y P E+"p r o p e r t yc a n n o t b en u l l " ) ; } t r y { C l a s s < ? >c l a z z T y p e=C l a s s . f o r N a m e ( i d T y p e N a m e ) ; i d T y p e=( T y p e )c l a z z T y p e . n e w I n s t a n c e ( ) ; } c a t c h( C l a s s N o t F o u n d E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ C L A S S _ H I B E R N A T E _ T Y P E+"i su n k n o w n c l a s s " ,e ) ; } c a t c h( C l a s s C a s t E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ C L A S S _ H I B E R N A T E _ T Y P E+"i sn o taT y p e c l a s s " ,e ) ; } c a t c h( I n s t a n t i a t i o n E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ C L A S S _ H I B E R N A T E _ T Y P E+"c a n n o tb e i n s t a n t i a t e d " ,e ) ; } c a t c h( I l l e g a l A c c e s s E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ C L A S S _ H I B E R N A T E _ T Y P E+"c a n n o tb e a c c e s s e d " ,e ) ; } s u p e r . c o n f i g u r e ( i d T y p e ,p a r a m s ,d i a l e c t ) ; i n i t F i e l d s ( p a r a m s ) ; } / * * *I n i tm e t h o df o rf i e l d s . *@ p a r a mp a r a m s-P a r a m e t e r si na n n o t a t i o n . * / p r i v a t ev o i di n i t F i e l d s ( P r o p e r t i e sp a r a m s ) { f i n a lS t r i n gc l a s s N a m e=p a r a m s . g e t P r o p e r t y ( C L A S S _ N A M E ) ; i f( c l a s s N a m e= =n u l l ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( C L A S S _ N A M E+"p r o p e r t yc a n n o tb en u l l " ) ; } t r y { c l a z z=C l a s s . f o r N a m e ( c l a s s N a m e ) ; } c a t c h( C l a s s N o t F o u n d E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( C L A S S _ N A M E+"i su n k n o w nc l a s s " ,e ) ; } f i n a lS t r i n gi d G e t t e r M e t h o d=p a r a m s . g e t P r o p e r t y ( I D _ G E T T E R _ M E T H O D ) ; i f( i d G e t t e r M e t h o d= =n u l l )

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

5/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
{ t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ G E T T E R _ M E T H O D+"p r o p e r t yc a n n o tb e n u l l " ) ; } t r y { g e t t e r M e t h o d=c l a z z . g e t D e c l a r e d M e t h o d ( i d G e t t e r M e t h o d ) ; } c a t c h( S e c u r i t y E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ G E T T E R _ M E T H O D+"c a n n o tb ea c c e s s: s e c u r i t yv i o l a t i o n " ,e ) ; } c a t c h( N o S u c h M e t h o d E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ G E T T E R _ M E T H O D+"d o e sn o te x i s t s " ,e ) ; } f i n a lS t r i n gc l a s s N a m e I d=p a r a m s . g e t P r o p e r t y ( I D _ C L A S S _ N A M E ) ; i f( c l a s s N a m e I d= =n u l l ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ C L A S S _ N A M E+"p r o p e r t yc a n n o tb e n u l l " ) ; } t r y { i d C l a z z=C l a s s . f o r N a m e ( c l a s s N a m e I d ) ; } c a t c h( C l a s s N o t F o u n d E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ C L A S S _ N A M E+"i su n k n o w nc l a s s " ,e ) ; } C l a s s < ? >i d C l a z z V a l u e=i d T y p e . g e t R e t u r n e d C l a s s ( ) ; f i n a lS t r i n gi d G e n e r a t e d V a l u e G e t t e r M e t h o d= p a r a m s . g e t P r o p e r t y ( I D _ G E T T E R _ G E N E R A T E D _ F I E L D ) ; i f( i d G e n e r a t e d V a l u e G e t t e r M e t h o d= =n u l l ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ G E T T E R _ G E N E R A T E D _ F I E L D+"p r o p e r t y c a n n o tb en u l l " ) ; } t r y { g e t t e r G e n e r a t e d V a l u e M e t h o d= i d C l a z z . g e t D e c l a r e d M e t h o d ( i d G e n e r a t e d V a l u e G e t t e r M e t h o d ) ; } c a t c h( S e c u r i t y E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ G E T T E R _ G E N E R A T E D _ F I E L D+"c a n n o tb e a c c e s s:s e c u r i t yv i o l a t i o n " ,e ) ; } c a t c h( N o S u c h M e t h o d E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ G E T T E R _ G E N E R A T E D _ F I E L D+"d o e sn o t e x i s t s " ,e ) ; }

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

6/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
f i n a lS t r i n gi d G e n e r a t e d V a l u e S e t t e r M e t h o d= p a r a m s . g e t P r o p e r t y ( I D _ S E T T E R _ G E N E R A T E D _ F I E L D ) ; i f( i d G e n e r a t e d V a l u e S e t t e r M e t h o d= =n u l l ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ S E T T E R _ G E N E R A T E D _ F I E L D+"p r o p e r t y c a n n o tb en u l l " ) ; } t r y { s e t t e r G e n e r a t e d V a l u e M e t h o d= i d C l a z z . g e t D e c l a r e d M e t h o d ( i d G e n e r a t e d V a l u e S e t t e r M e t h o d ,i d C l a z z V a l u e ) ; } c a t c h( S e c u r i t y E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ S E T T E R _ G E N E R A T E D _ F I E L D+"c a n n o tb e a c c e s s:s e c u r i t yv i o l a t i o n " ,e ) ; } c a t c h( N o S u c h M e t h o d E x c e p t i o ne ) { t h r o wn e wI l l e g a l S t a t e E x c e p t i o n ( I D _ S E T T E R _ G E N E R A T E D _ F I E L D+"d o e sn o t e x i s t s " ,e ) ; } } / * *A c c e s st h ei do ft h ec u r r e n te n t i t ya n dg e n e r a t ev a l u ei fa n do n l yi f g e n e r a t e dv a l u et ob es e ti sn u l l . * *T O D OD i r e c ta c c e s st oi df i e l da n dv a l u ef i e l d *@ s e e o r g . h i b e r n a t e . i d . S e q u e n c e G e n e r a t o r # g e n e r a t e ( o r g . h i b e r n a t e . e n g i n e . S e s s i o n I m p l e m e n t o r , j a v a . l a n g . O b j e c t ) * / @ O v e r r i d e p u b l i cs y n c h r o n i z e dS e r i a l i z a b l eg e n e r a t e ( S e s s i o n I m p l e m e n t o rs e s s i o n ,O b j e c to b j ) t h r o w sH i b e r n a t e E x c e p t i o n { i f( o b j . g e t C l a s s ( ) . e q u a l s ( c l a z z ) ) { t r y { S e r i a l i z a b l ei d O b j=( S e r i a l i z a b l e )g e t t e r M e t h o d . i n v o k e ( o b j ) ; i f( i d O b j= =n u l l ) { i d O b j=( S e r i a l i z a b l e )i d C l a z z . n e w I n s t a n c e ( ) ; } i f( i d O b j . g e t C l a s s ( ) . e q u a l s ( i d C l a z z ) ) { O b j e c ti d=g e t t e r G e n e r a t e d V a l u e M e t h o d . i n v o k e ( i d O b j ) ; i f( i s N u l l O r D e f a u l t ( i d ) ) { i d=s u p e r . g e n e r a t e ( s e s s i o n ,o b j ) ; s e t t e r G e n e r a t e d V a l u e M e t h o d . i n v o k e ( i d O b j ,i d ) ; } } r e t u r ni d O b j ; }

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

7/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
c a t c h( I l l e g a l A r g u m e n t E x c e p t i o ne ) { t h r o wn e wH i b e r n a t e E x c e p t i o n ( e ) ; } c a t c h( I l l e g a l A c c e s s E x c e p t i o ne ) { t h r o wn e wH i b e r n a t e E x c e p t i o n ( e ) ; } c a t c h( j a v a . l a n g . r e f l e c t . I n v o c a t i o n T a r g e t E x c e p t i o ne ) { t h r o wn e wH i b e r n a t e E x c e p t i o n ( e ) ; } c a t c h( I n s t a n t i a t i o n E x c e p t i o ne ) { t h r o wn e wH i b e r n a t e E x c e p t i o n ( e ) ; } } r e t u r ns u p e r . g e n e r a t e ( s e s s i o n ,o b j ) ; } / * * *C h e c ki ft h ei di sn u l lo rd e f a u l ti nc a s eo fP r i m i t i v e T y p e . *@ p a r a mi d-t h ei d *@ r e t u r n{ @ l i n kB o o l e a n }-< c o d e > t r u e < / c o d e >i nc a s ei d T y p ei s < c o d e > n u l l < / c o d e >o rd e f a u l tv a l u e * / p r i v a t eb o o l e a ni s N u l l O r D e f a u l t ( O b j e c ti d ) { i f( i d T y p ei n s t a n c e o fP r i m i t i v e T y p e < ? > ) { r e t u r ni d= =n u l l| |( ( P r i m i t i v e T y p e < ? > ) i d T y p e ) . g e t D e f a u l t V a l u e ( ) . e q u a l s ( i d ) ; } r e t u r ni d= =n u l l ; } }

With an example : Entity class :

Code:

@ E n t i t y @ T a b l e ( n a m e=" M Y _ T A B L E " ) @ G e n e r i c G e n e r a t o r ( n a m e=" M Y _ S E Q " , s t r a t e g y=" o r g . h i b e r n a t e . i d . E m b e d d e d S e q u e n c e G e n e r a t o r " , p a r a m e t e r s= { @ P a r a m e t e r ( n a m e=" s e q u e n c e " ,v a l u e=" M Y _ S E Q _ N A M E " ) , @ P a r a m e t e r ( n a m e=" c l a s s N a m e " ,v a l u e=" m y . p a c k a g e . O p e r a t i o n E n t i t y " ) , @ P a r a m e t e r ( n a m e=" i d G e t t e r M e t h o d " ,v a l u e=" g e t I d " ) , @ P a r a m e t e r ( n a m e=" i d C l a s s N a m e " ,v a l u e=" m y . p a c k a g e . O p e r a t i o n I d " ) , @ P a r a m e t e r ( n a m e=" g e n e r a t e d F i e l d G e t t e r M e t h o d " ,v a l u e=" g e t I d O p " ) , @ P a r a m e t e r ( n a m e=" i d C l a s s H i b e r n a t e T y p e " ,v a l u e= " o r g . h i b e r n a t e . t y p e . L o n g T y p e " ) , @ P a r a m e t e r ( n a m e=" g e n e r a t e d F i e l d S e t t e r M e t h o d " ,v a l u e=" s e t I d O p " ) } )

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

8/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
p u b l i cc l a s sO p e r a t i o n E n t i t yi m p l e m e n t sS e r i a l i z a b l e { / * *I d .* / p r i v a t eO p e r a t i o n I di d ; / * * *@ r e t u r n{ @ l i n kO p e r a t i o n I d }-t h ei d * / @ E m b e d d e d I d @ G e n e r a t e d V a l u e ( g e n e r a t o r=" O P E R A T I O N _ I D _ S E Q " ) p u b l i cs y n c h r o n i z e dO p e r a t i o n I dg e t I d ( ) { / /C r e a t en e wi fn u l lt oa v o i dN P Ee r r o i f( i d= =n u l l ) { i d=n e wO p e r a t i o n I d ( ) ; } r e t u r ni d ; } / * * *@ p a r a mi d-t h ei d * / p u b l i cv o i ds e t I d ( O p e r a t i o n I di d ) { t h i s . i d=i d ; } / /O t h e rp r o p e r t i e s }

Id class :
Code:

/ * * *M yE m b e d d e d I df o rO p e r a t i o n E n t i t yc l a s s . * / @ E m b e d d a b l e p u b l i cc l a s sO p e r a t i o n I di m p l e m e n t sS e r i a l i z a b l e { p r i v a t es t a t i cf i n a ll o n gs e r i a l V e r s i o n U I D=7 2 6 9 5 8 2 9 1 1 4 2 0 9 7 0 7 0 7 L ; / * *G e n e r a t e di dw i t hs e q u e n c e .* / p r i v a t eL o n gi d O p ; / * *O t h e rf i e l ds e t t e db yu s e r .* / p r i v a t eD a t es e n d D a t e ; p u b l i cO p e r a t i o n I d ( ) { / /D e f a u l t:n oi d t h i s ( n u l l ,n e wD a t e ( ) ) ; } p u b l i cO p e r a t i o n I d ( f i n a lL o n ga n I d O p ,f i n a lD a t ea S e n d D a t e ) { t h i s . i d O p=a n I d O p ; t h i s . s e n d D a t e=a S e n d D a t e ; }

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

9/10

1/4/14

Hibernate Community View topic - @EmbeddedId that contains an Identity column on SQL Server
/ * * *@ r e t u r n{ @ l i n kL o n g }-G e n e r a t e di d * / @ C o l u m n ( n a m e=" I D " ) p u b l i cL o n gg e t I d O p ( ) { r e t u r ni d O p ; } / * * *@ p a r a mi d O p-G e n e r a t e di d * / p u b l i cv o i ds e t I d O p ( L o n gi d O p ) { t h i s . i d O p=i d O p ; } / * * *@ r e t u r n{ @ l i n kD a t e }-O t h e rf i e l d * / @ C o l u m n ( n a m e=" S E N D _ D A T E " ,n u l l a b l e=f a l s e ) p u b l i cD a t eg e t S e n d D a t e ( ) { r e t u r ns e n d D a t e ; } / * * *@ p a r a ms e n d D a t e-O t h e rf i e l d * / p u b l i cv o i ds e t S e n d D a t e ( D a t es e n d D a t e ) { t h i s . s e n d D a t e=s e n d D a t e ; } }

With some work, it could be fully operationnal even for non getter / setter field access (idOp would be public in this case).

Top Display posts from pre vious:

All posts

Sort by

Post time

Ascending

Go

Page 1 of 1 [ 6 posts ]

Board index Hibernate & Java Persistence Hibernate Users

All tim e s are UTC - 5 hours [ DST ] You cannot post ne w topics You cannot re ply to topics You cannot e dit your posts You cannot de le te your posts in in in in this this this this forum forum forum forum

Se arch for:

Go

C opyright 2009, R e d Hat Middle ware , LLC . All rights re se rve d. JBoss and Hibe rnate are re giste re d trade m ark s and se rvice m ark s of R e d Hat, Inc.

https://forum.hibernate.org/viewtopic.php?f=1&t=1000166

10/10

Vous aimerez peut-être aussi