-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Id not generated by hibernate
PostPosted: Thu Apr 13, 2006 3:58 pm 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,
There is something I don't understand regarding the generation of the id by using a sequence in Oracle.
Here is my sequence:
Code:
SQL> SELECT SEQ_INSTRUMENT.nextval FROM DUAL;
NEXTVAL
----------
         1

My Pojo definition, generated by Hibernate Tool:
Code:
/**
* Instrument generated by hbm2java
*/
@Entity
@Table(name="INSTRUMENT"
, uniqueConstraints = { @UniqueConstraint( columnNames = { "CODE" } ) })
public class Instrument  implements java.io.Serializable {
....
    @Id( generate=GeneratorType.SEQUENCE, generator="SEQ_INSTRUMENT_LOG")
    @SequenceGenerator(name="SEQ_INSTRUMENT_LOG", sequenceName="SEQ_INSTRUMENT")
    @Column(name="ID", unique=true, nullable=false, insertable=true, updatable=true, precision=18, scale=0)
    public long getId() {
        return this.id;
    }

And my service which persists the Pojo:
Code:
@Stateless
@RemoteBinding(jndiBinding="hedgefundpro/remote/InstrumentService")
@LocalBinding(jndiBinding="hedgefundpro/local/InstrumentService")
public class InstrumentService implements IInstrument, Serializable  {
   @PersistenceContext(unitName="HedgeFundPro")
   protected EntityManager em;
   public InstrumentService() {
      super();
   }
   public Instrument add(Instrument instrument) {
      em.persist(instrument);
      return instrument;
   }

When I look at the logs, there are no statement to retrieve the id from the sequence.
The logs:
Code:
21:04:16,390 INFO  [STDOUT] Hibernate: select instrument_.ID, instrument_.SHORT_NAME as SHORT2_237_, instrument_.LONG_NAME as LONG3_237_ from INSTRUMENT_TYPE instrument_ where instrument_.ID=?
21:04:16,396 INFO  [STDOUT] Hibernate: insert into INSTRUMENT (NAME, CODE, PARENT, INSTRUMENT_TYPE, BASE_CURRENCY, UNDERLYING_CURRENCY, ID) values (?, ?, ?, ?, ?, ?, ?)
21:04:16,405 WARN  [JDBCExceptionReporter] SQL Error: 1, SQLState: 23000
21:04:16,405 ERROR [JDBCExceptionReporter] ORA-00001: unique constraint (MY_SCHEMA.SYS_C009217) violated


I could use the following workaround, but I would appreciate to understand my mistake and benefits from every bit of Hibernate...
Code:
...
Long id = (Long)em.createNativeQuery("SELECT SEQ_INSTRUMENT.nextval FROM DUAL").getSingleResult();
instrument.setId(id);
...

Can someone tell me what I'm missing or did wrong?
Best regards.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 15, 2006 4:22 am 
Beginner
Beginner

Joined: Wed Aug 24, 2005 5:32 am
Posts: 23
Hi,
I assume that you are using beta8 or beta9, if so your mapping is wrong. The ID should be mapped using @Id and @GeneratedValue, like

Code:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator=="<generator name>")
@SequenceGenerator....
public int getId() {
   return id;
}


Take a look at http://jroller.com/page/eyallupu?entry=the_new_persistence_api_ids

And at http://forum.hibernate.org/viewtopic.php?t=956929

Eyal Lupu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 9:05 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
year it's a shame that the compiler does not complain on such case

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


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

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.