-->
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: Custom Counter going as NULL
PostPosted: Thu Apr 04, 2013 3:32 am 
Newbie

Joined: Wed Mar 13, 2013 7:06 am
Posts: 4
Hi,
I have a use case where I need to generate a custom counter. The case is where I need to place orders and generate an order number.
I have written a code generator class
Code:
package filters.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;

import play.db.jpa.JPA;

public class CodeGenerator implements IdentifierGenerator
{
   public Serializable generate(SessionImplementor session, Object object)
           throws HibernateException
   {

      String prefix = "BAKR";
      String code = null;

      Query query = JPA.em().createNativeQuery(
              "SELECT nextval ('s_bkg1') as nextval"); //this is a Postgres sequence created manually by hand

      String result = String.valueOf(query.getResultList().get(0));
      code = prefix + "00" + result;

      return code;
   }
}

In my model I have called it
Code:
public class Booking
{
   @Id
   public int trans_pid;
   @GenericGenerator(name="bakr", strategy="filters.controllers.CodeGenerator")
   @GeneratedValue(generator="bakr")
   public String booking_no;


and I am not setting the same through code.
I want to generate order nos as BAKR001,BAKR002, ..However I am getting NULLS inserted in the database.


Please could somebody assist.
-S


Top
 Profile  
 
 Post subject: Re: Custom Counter going as NULL
PostPosted: Thu Apr 04, 2013 3:52 am 
Newbie

Joined: Wed Mar 13, 2013 11:32 am
Posts: 16
Generators are only call for @Id fields
You need to make booking_no you primary key (e.g. @Id) and remove the trans_pid column as primary key or you need to extract the booking_no key in your business logic and assign it.

Alternately try experimenting with @SQLInsert on you entity. You may be able to insert using nextval('s_bkg1') writing you own native insert SQL (you also need to have 'BAKR' and '00' in added in you native insert statement)


Top
 Profile  
 
 Post subject: Re: Custom Counter going as NULL
PostPosted: Thu Apr 04, 2013 5:05 am 
Newbie

Joined: Wed Mar 13, 2013 7:06 am
Posts: 4
Hi,
I cannot use the complete booking no in my flow, passing it between the various tables as a PK-FK; that would be very inefficient on the database end. If I use a native query I would need to write some wrapper function to handle the calls as well and that would mean if the client requests a change in the database vendor the same would need to be tackled and not be database agnostic.

Any other ideas?

-S


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.