I am making a program that makes a file and gives it a unique name. As per the propose technology, we need to have sequential file name. ex. Myfile-00001 (format : MyFile-Sequence). I am using oracle sequence to maintain the sequence number for the file name.
Below is the sample code I am trying to write to get just the sequence out of oracle sequence. If I can get just the string of sequence out of Oracle sequence, I can use it to namemy file I am creating.
Below is my Code:
public class TestSequence {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session ses = sf.openSession();
System.out.println("Next Val: "+getNextKey(ses));
ses.close();
sf.close();
}
public static String getNextKey(Session ses)
{
Query query = ses.createSQLQuery( "select atl_seq.nextval from dual" );
String key = (String) query.uniqueResult();
return key;
}
}
But its giving me error:
Next Val:
Hibernate: select atl_seq.nextval from dual
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.String
at test.TestSequence.getNextKey(TestSequence.java:31)
at test.TestSequence.main(TestSequence.java:23)
Please help how do I convert the unique result into string. No Mapping file is used, as I am running the query directly.
I have googled a lot and found similar example, they cast the unique result into Long, but even that code is not running and it gives me same error :cannot cast [Ljava.lang.Object to Long.
My sequence is :
CREATE SEQUENCE "ATL_SEQ" MINVALUE 1 MAXVALUE 100 INCREMENT BY 1 START WITH 1 CACHE 48