• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DB2 Hibernate Sequence generator mapping

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have the hibernate annotations set

@Entity
@Table( name="pens")
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PEN_SEQ")
@SequenceGenerator(name="PEN_SEQ",sequenceName="PEN_ID_SEQ")
@Column( name="pen_id" , nullable = false, updatable=false )
private Integer id;

i have a pensidgenerator:

public class PenIdGenerator implements IdentifierGenerator {

public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {

Connection connection = session.connection();
try {

PreparedStatement ps = connection.prepareStatement("SELECT MAX(pen_id) as nextval from pens");

ResultSet rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("nextval");
log.debug("Generated Pen ID: " + (id+1));
return id+1;
}


and i have SEquqnce cretaed in DB:

CREATE SEQUENCE PEN_ID_SEQ
MINVALUE 1
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO CYCLE
CACHE 24;

i am able to get the next val correctly when i try in the Database

but from the hibernate i am able to save a Pen but with some long Id ex: 18100.....

dont know what i am missing

thanks
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be missing something, but what error message do you get?
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic