The Persistent Object pattern is used to enhance a POJO (plain old java object) or in our case, a POGO (plain old groovy object) and give it fields and methods which are conducive to supporting a data store environment.
For our purposes, our data model classes (those which will map to database tables) must extend the PersistentObject class, so there is uniformity across the application. That is, something like an 'id' should be on every object stored in the database.
Groovy;
package com.wookets.core.model
import java.util.Date
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.MappedSuperclass
import javax.persistence.Transient
import javax.validation.constraints.NotNull
/**
* A hibernate friendly abstract class which all of our objects (which will be stored) should extend.
* The id field of this class is used to uniquely id the object from other objects of the same type
* The active field can be used to turn on or off an object. This can be used in place of deleting.
* @author wooke
*
*/
@MappedSuperclass
abstract class PersistentObject {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id
@NotNull
Boolean active = true
Date modifiedOn
Date createdOn
@Transient
String getIdAsString() {
id?.toString()
}
@Transient
String toStringForDisplay() {
getClass().getName() + "@" + id;
}
@Transient
boolean isTransient() {
id == null
}
}
0 comments:
Post a Comment