max wrote:
write a UserType.
But you should really rethink why you have a composite key with one part being unique...what is the other part for ?
For that problem i have writen a UserType and an IdentifierGenerater too. I have modified the IdentifierGeneratorFactory as follows...
public static Serializable get(ResultSet rs, Type type)
throws SQLException, IdentifierGenerationException {
Class clazz = type.getReturnedClass();
if ( clazz==Long.class ) {
return new Long( rs.getLong(1) );
}
else if ( clazz==Integer.class ) {
return new Integer( rs.getInt(1) );
}
else if ( clazz==Short.class ) {
return new Short( rs.getShort(1) );
}
else if ( clazz==String.class ) {
return rs.getString(1);
}
else if( clazz==domain.ArticleUserTypeId.class) {
Long articleId=(new Long(rs.getString(1)));
log.debug( "articleId ="+articleId);
return new domain.ArticleUserTypeId(articleId,0);
}
else {
throw new IdentifierGenerationException("this id generator generates long, integer, short or string");
}
}
And the i wrote Generator is as follows
public class CompositeIdGenerator extends SelectGenerator
{
/**
*
*/
public CompositeIdGenerator()
{
super();
// TODO Auto-generated constructor stub
}
private String uniqueKeyPropertyName;
private TextType idType;
private String entityName;
public void configure(TextType type, Properties params, Dialect d)
throws MappingException
{
uniqueKeyPropertyName = params.getProperty("key");
entityName = params.getProperty(ENTITY_NAME);
this.idType = type;
}
public Serializable generate(SessionImplementor session, Object obj)
throws HibernateException
{
final Serializable id = session.getEntityPersister(entityName, obj)
// TODO: cache the persister, this shows up in yourkit
.getIdentifier(obj, session.getEntityMode());
ArticleUserTypeId articleUserTypeId = (ArticleUserTypeId) id;
if (id == null)
{
throw new IdentifierGenerationException(
"ids for this class must be manually assigned before calling save(): "
+ entityName);
}
System.out
.println("In generate method of idnetity generator article id is "
+ articleUserTypeId.getArticleId());
System.out
.println("In generate method of idnetity generator article version is "
+ articleUserTypeId.getArticleVersion());
if (articleUserTypeId.getArticleId() == null)
{
return IdentifierGeneratorFactory.POST_INSERT_INDICATOR;
} else
{
return id;
}
}
}
Is this acceptable. If i modify like this i am able to insert the composite id with and without assigned values. But only one thing is to get clarification is .. Does this modification voilates the rules of hibernate architecture. If it does that please give show me a way to solve this problem with out modifing the hibernate files.
I will be waiting for your reply from you or from your team.
Max can u find some time to answer.
Anybody please help me in this problem.