• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 42173
937
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?
 
please buy this thing and then I get a fat cut of the action:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic