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.  [ 1 post ] 
Author Message
 Post subject: Design pattern for implementing source of unique values
PostPosted: Wed Mar 09, 2005 7:55 pm 
Newbie

Joined: Sun Jan 23, 2005 6:49 pm
Posts: 7
I have following class:

public class MyData implements Serializable {
private Long id;
private Long number;
private Long category;
private String text;
// + getters and setters
}

The id property is mapped as ID in the mapping file (native generator).

I would additionally set number property using some generator, which value must be unique for particular category value. What is the best design pattern?

I was thinking about creating new persistent class NumberGenerator:
public class NumberGenerator implements Serializable {
private Long id;
private Long category; // unique="true" in the hbm.xml
private Long currentValue;
// + getters and setters
}

and provide NumberGeneratorService working this way:
...
public Long generateNewNumberForCategory(Long category) {
Iterator ngIt = = session.iterate("from NumberGenerator ng where category = " + category)
NumberGenerator ng = null;
if (ngIt.hasNext()) {
ng = (NumberGenerator) ngIt.next();
} else {
ng = new NumberGenerator();
ng.setCategory(category);
ng.setCurrentValue(1);
session.save(ng);
}
return ng.getCurrentValue();
}

Or, I was thinking about creating composite primary key and create own IdentityGenerator or use Interceptor for generating its number value.

Is there better solution for these?

_________________
Piotr Bzdyl


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.