Java 动态替换嵌套对象的字段值
我正在尝试为我的Java 动态替换嵌套对象的字段值,java,scala,reflection,scalatest,Java,Scala,Reflection,Scalatest,我正在尝试为我的scala应用程序编写集成测试(使用akka http)。我遇到了一个问题,我找不到解决办法 我的案例分类如下: case class Employee(id:Long, name:String, departmentId:Long, createdDate:Timestamp) extends BaseEntity case class EmployeeContainer(employee:Employee, department:Department) extends Ba
scala
应用程序编写集成测试(使用akka http
)。我遇到了一个问题,我找不到解决办法
我的案例分类如下:
case class Employee(id:Long, name:String, departmentId:Long, createdDate:Timestamp) extends BaseEntity
case class EmployeeContainer(employee:Employee, department:Department) extends BaseEntity
我有一个这样的方法
trait BaseTrait[E<:BaseEntity, C <: BaseEntity]{
def getById(id:Long): Future[List[C]] = {
//query from db and return result.
}
def save(obj:E) = {
//set the createDate field to the current timestamp
//insert into database
}
}
trait-BaseTrait[E您可能需要为of使用一些。您的主要问题是您直接调用数据库:val-emp-Employee){
覆盖def getById(id:Long)={
为了{
瓦伦普
class MyDao extends BaseTrait[Employee, EmployeeContainer] {
override def getById(id:Long) = {
for {
val emp <- getFromDb(id)
val dept <- DeptDao.getFromDb(emp.departmentId)
val container = EmployeeContainer(emp,dept)
} yield(container)
}
}
class MyDao extends BaseTrait[Employee, EmployeeContainer](val dbCall: Long => Employee) {
override def getById(id:Long) = {
for {
val emp <- dbCall(id)
val dept <- DeptDao.getFromDb(emp.departmentId)
val container = EmployeeContainer(emp,dept)
} yield(container)
}
}