-->
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.  [ 7 posts ] 
Author Message
 Post subject: generate StringID in hibernate mapping files
PostPosted: Tue Feb 28, 2006 11:28 am 
Newbie

Joined: Tue Feb 28, 2006 10:09 am
Posts: 5
Location: banglore
How can we generate ID for any table using hibernate mapping files. we can generate ID using assigned or native like in case of numeric. But If it is a string like ABC12300 means ABC is a string and 12300 is number which is automatically incrementing. When ever i store data in my DB2 using hibernate mapping file it should be like

ABC12300 next time it should be
ABC12301 next time it should be
ABC12302

Thanks and Regards,
Guru


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 11:57 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Here is your generator:

public class MyGenerator implements IdentifierGenerator {

String sql = "select next from dual";

public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
return new String(getText().concat(getNext(session)));
}

private String getNext(SessionImplementor session) {
long next;

Connection conn = session.connection();

try {
PersistentIdentifierGenerator.SQL.debug(sql);
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = null;
try {
rs = st.executeQuery();
if (rs.next()) {
next = rs.getLong(1) + 1;
if (rs.wasNull())
next = 1;
} else {
next = 1;
}
sql = null;
} finally {
if (rs != null)
rs.close();
st.close();
}

} catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(session.getFactory()
.getSQLExceptionConverter(), sqle,
"could not fetch initial value", sql);
}

return new Long(next).toString();
}

public String getText() {
return "ABC";
}

}

Hope it solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 4:34 am 
Newbie

Joined: Tue Feb 28, 2006 10:09 am
Posts: 5
Location: banglore
It's fine. Thanks a lot. But i am not able to understand the parameters in generate method.

public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
return new String(getText().concat(getNext(session)));
}

How can i call this generate method. means what parameters i have to pass. If in EXAMPLE class i am calling this generate method then.

i create object of this class like obj. Then
obj.generate( ? , ? ) what are the parameters i have to pass.


One more doubt regarding storing of hbm files
where can i store hibernate.cfg.xml. usually i put in src folder. can i store hibernate.cfg.xml. in seperate package under src only.

and where i have to store my hbm.xml files. means all together in a package under src. Or each hbm file in seperate package like usecase wise.

Thanks and Regards,
GURU


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 5:18 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hello again,

1. I think you should read first how to implement a custom id generator.

2. You don't have to call the generate method. Just use it in your hbm files. Hibernate will take care about the parameters.

<id name="id" column="ID" type="string">
<generator class="MyGenerator"/>
</id>

3. About hbm files: place them wherever you want or whereever are you project request.

Best


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 6:27 am 
Newbie

Joined: Tue Feb 28, 2006 10:09 am
Posts: 5
Location: banglore
Thank you very much sir. Let me try this if any quires i will ask later

Thanks and Regards,
Guru


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 11:22 am 
Newbie

Joined: Tue Feb 28, 2006 10:09 am
Posts: 5
Location: banglore
what ever you send that mygenerator class it is working fine. but i am unable to run that class for second time. it is working only once.


second time when ever in execution i reaches this line.

next = rs.getLong(1) + 1;

it is giving error like

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not fetch initial value
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:82)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at com.test.MyGenerator.getNext(MyGenerator.java:44)
at com.test.MyGenerator.generate(MyGenerator.java:20)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at com.test.TestRun.main(TestRun.java:31)
Caused by: com.ibm.db2.jcc.b.SqlException: Invalid operation: result set closed
at com.ibm.db2.jcc.b.cc.bk(cc.java:3031)
at com.ibm.db2.jcc.b.cc.h(cc.java:3001)
at com.ibm.db2.jcc.b.cc.getLong(cc.java:641)
at com.test.MyGenerator.getNext(MyGenerator.java:32)
... 10 more




Please give me solution. any way thanks . thank you very much.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 6:28 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

You should check the sql used to generate the next value. In my example I've put "select next from dual" and that was just a sample. Make sure you put here a valid sql (you can test it before using it) and that should solve your problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.