As i have not got farer in this matter i started writing tests (obv. should have done this before, but, well :D).
However, this is my class so far:
class Task extends Record[Long, Task] with IdentityGenerator[Long, Task] {
def this(name: String, description: String, done: Boolean, dueTo: Date, id: Long) = {
this()
this.name := name.trim()
this.description := description.trim()
this.dueTo := dueTo
this.done := done
if(id == 0) this.id.setNull else
this.id := id
}
def PRIMARY_KEY = id
def relation = Task
val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
val name = "name".VARCHAR(255)
val description = "description".TEXT
val createdAt = "created_at".TIMESTAMP.DEFAULT("current_timestamp")
val dueTo = "due_to".DATE
val done = "done".BOOLEAN.NOT_NULL(false)
}
object Task extends Task with Table[Long, Task] {
def apply(name: String, description: String, done: String, dueTo: Option[Date], id: Long) = {
new Task(name, description, done == "on", dueTo getOrElse null, id)}
// dummy unapply so that we can provide something for the form
def unapply(t : Task) = Option(
t.name(), t.description(), t.done().toString, null,
t.id())
}
Now, if i do create a new task with:
val task = new Task("name", "description", true, null, 0)
save, refresh, change the name, save (which updates the record) and refresh again, the timestamp does not change.
But, if i use the apply method from the object in a similar way:
val taskApply = Task.apply("name", "description", "on", None, 0)
save, refresh and:
val taskApplyTwo = Task.apply("name", "description", "on", None, taskApply.id())
then save and refresh the second one too, i get two different timestamps.
And i just cannot see why this is?
I will add the two different tests for convenience:
"Task update" should {
"not change the created_at timestamp" in {
Context.executeInNew { ctx =>
val task = new Task("name", "description", true, null, 0)
task.save
task.refresh
createdTasks += task
val timeStamp = task.createdAt()
Thread.sleep(1500)
task.description := "changed desc"
task.save
task.refresh
task.createdAt() must equalTo(timeStamp)
}
}
}
"Task apply, save and update" should {
"keep the same created_at timestamp in database" in {
Context.executeInNew { ctx =>
val taskApply = Task.apply("name", "description", "on", None, 0)
taskApply.save
taskApply.refresh
Thread.sleep(1500)
val taskApplyTwo = Task.apply("name", "description", "on", None, taskApply.id())
taskApplyTwo.save
taskApplyTwo.refresh
taskApply.createdAt() must equalTo(taskApplyTwo.createdAt())
}
}
}
Best regards,
Sven