Created
February 24, 2014 10:10
-
-
Save odwrotnie/9185023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ID Sequences | |
You can provide a `Sequence` for your `Entities` in order to alter generated ID's from default UUID's. | |
The traits you can use: | |
``` | |
trait Entity extends BaseEntity with UUID | |
trait EntityWithCustomID[ID] extends BaseEntity with CustomID[ID] | |
trait EntityWithGeneratedID[ID] extends BaseEntity with GeneratedID[ID] | |
``` | |
## ID Generators | |
``` | |
abstract class IdGenerator[E <: BaseEntity: Manifest] | |
abstract class SegmentedIdGenerator[E <: BaseEntity: Manifest]( | |
fSequence: => Sequence[E#ID])( | |
implicit n: Numeric[E#ID], | |
ctx: ActivateContext) | |
extends IdGenerator[E] | |
abstract class SequencedIdGenerator[E <: BaseEntity: Manifest]( | |
val sequence: Sequence[E#ID]) | |
extends IdGenerator[E] { | |
def nextId = | |
sequence.nextValue(1) | |
``` | |
> SegmentedIdGenerator should be used if the id doesn't need to be strictly sequencial. | |
There are two built in sequences in `net.fwbrasil.activate.sequence`: | |
- IntSequenceEntity | |
- LongSequenceEntity | |
## Example | |
``` | |
class ModelIDSequence(name: String) | |
extends SequenceEntity[String](name, 1) { | |
def _nextValue = { | |
value += step | |
s"$name%05d" format value | |
} | |
} | |
object ModelIDSequence { | |
def apply(sequenceName: String, step: Int = 1) = { | |
transactional(requiresNew) { | |
select[ModelIDSequence].where(_.name :== sequenceName).headOption.getOrElse { | |
new ModelIDSequence(sequenceName) | |
} | |
} | |
} | |
} | |
class ModelSID extends SequencedIdGenerator[Model](ModelIDSequence("m")) | |
``` | |
> The above ModelSID will be bind to the Model Entity. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment