Java 光滑介质中的瞬态等效

Java 光滑介质中的瞬态等效,java,scala,slick,case-class,transient,Java,Scala,Slick,Case Class,Transient,我正在使用Slick将案例类映射到表。在某些情况下,我需要一个数据库表中没有的附加字段。此字段值将根据某些业务条件进行更新。 但是,我不能这样做,因为case类和slick表需要具有相同的字段 在使用Hibernate的java中,使用@Transient注释实现了同样的效果。我如何解决这个问题?我通过在case类字段中添加@transient注释来绑定。但是,光滑的映射显示了编译错误。您尝试过这个吗 case class Container(name: String, width: Float

我正在使用
Slick
将案例类映射到表。在某些情况下,我需要一个数据库表中没有的附加字段。此字段值将根据某些业务条件进行更新。 但是,我不能这样做,因为case类和slick表需要具有相同的字段

在使用
Hibernate
java
中,使用
@Transient
注释实现了同样的效果。我如何解决这个问题?我通过在case类字段中添加
@transient
注释来绑定。但是,光滑的映射显示了编译错误。

您尝试过这个吗

case class Container(name: String, width: Float, height: Float, condition: Boolean)

class Containers(tag: Tag) extends Table[Container](tag, "CONTAINERS") {
  def name = column[String]("NAME", O.PrimaryKey)
  def width = column[Float]("WIDTH")
  def height = column[Float]("HEIGHT")

  def applyBusinessCondition(): Boolean = ...

  def * = (name, width, height, applyBusinessCondition()) <> (Container.tupled, Container.unapply)
}
case类容器(名称:String,宽度:Float,高度:Float,条件:Boolean)
类容器(tag:tag)扩展表[Container](tag,“Containers”){
def name=列[字符串](“名称”,O.PrimaryKey)
定义宽度=列[浮动](“宽度”)
def高度=列[浮动](“高度”)
def applyBusinessCondition():布尔=。。。
def*=(名称、宽度、高度、applyBusinessCondition())(Container.tuple、Container.unapply)
}
我认为可以在类容器内部或外部使用函数,并在表的*投影中调用它。您也可以在表定义中将条件设置为False,然后再进行更改。

您尝试过吗

case class Container(name: String, width: Float, height: Float, condition: Boolean)

class Containers(tag: Tag) extends Table[Container](tag, "CONTAINERS") {
  def name = column[String]("NAME", O.PrimaryKey)
  def width = column[Float]("WIDTH")
  def height = column[Float]("HEIGHT")

  def applyBusinessCondition(): Boolean = ...

  def * = (name, width, height, applyBusinessCondition()) <> (Container.tupled, Container.unapply)
}
case类容器(名称:String,宽度:Float,高度:Float,条件:Boolean)
类容器(tag:tag)扩展表[Container](tag,“Containers”){
def name=列[字符串](“名称”,O.PrimaryKey)
定义宽度=列[浮动](“宽度”)
def高度=列[浮动](“高度”)
def applyBusinessCondition():布尔=。。。
def*=(名称、宽度、高度、applyBusinessCondition())(Container.tuple、Container.unapply)
}

我认为可以在类容器内部或外部使用函数,并在表的*投影中调用它。您也可以在表定义中将条件设置为False,然后再进行更改。

我还没有尝试@jvican解决方案。我试试看。但我用另一种方法解决了这个问题,没有改变case类和光滑映射表的字段

我创造了一种特质

trait Approvable[TId <:AnyVal]{
  var isApproved: Boolean = false
}

trait Approvable[TId我还没有尝试@jvican解决方案。我会尝试一下。但是我用另一种方法解决了它,没有改变case类和光滑映射表的字段

我创造了一种特质

trait Approvable[TId <:AnyVal]{
  var isApproved: Boolean = false
}
trait Approvable[TId这个怎么样

case class Container(name: String, width: Float, height: Float, isApproved: Boolean)

val c: Container = ...
val approvedContainer = c.copy(isApproved=true)
在光滑的

...
def * = (name, width, height, false) <> (Container.tupled, Container.unapply)
...
...
def * = (name, width, height, false) <> (Container.createNonApproved, Container.unapply)
...
在光滑的

...
def * = (name, width, height, false) <> (Container.tupled, Container.unapply)
...
...
def * = (name, width, height, false) <> (Container.createNonApproved, Container.unapply)
...
注意这一点怎么样

case class Container(name: String, width: Float, height: Float, isApproved: Boolean)

val c: Container = ...
val approvedContainer = c.copy(isApproved=true)
在光滑的

...
def * = (name, width, height, false) <> (Container.tupled, Container.unapply)
...
...
def * = (name, width, height, false) <> (Container.createNonApproved, Container.unapply)
...
在光滑的

...
def * = (name, width, height, false) <> (Container.tupled, Container.unapply)
...
...
def * = (name, width, height, false) <> (Container.createNonApproved, Container.unapply)
...

请注意

有相同的问题,请使用以下代码解决:

case class Container(name: String, width: Float, height: Float, isApproved: Boolean)

class Containers(tag: Tag) extends Table[Container](tag, "CONTAINERS") {

  def name = column[String]("NAME", O.PrimaryKey)
  def width = column[Float]("WIDTH")
  def height = column[Float]("HEIGHT")

  def create = (name: String, width: Float, height: Float) =>
    Container(name, width, height, isApproved = false)

  def destroy(container: Container) =
    Some((container.name, container.width, container.height))

  def * = (name, width, height) <> (create.tupled, destroy)
}
case类容器(名称:String,宽度:Float,高度:Float,isApproved:Boolean)
类容器(tag:tag)扩展表[Container](tag,“Containers”){
def name=列[字符串](“名称”,O.PrimaryKey)
定义宽度=列[浮动](“宽度”)
def高度=列[浮动](“高度”)
def create=(名称:字符串,宽度:浮点,高度:浮点)=>
容器(名称、宽度、高度,isApproved=false)
def销毁(容器:容器)=
一些((container.name、container.width、container.height))
def*=(名称、宽度、高度)(create.tuple、destroy)
}

有相同的问题,使用此代码解决:

case class Container(name: String, width: Float, height: Float, isApproved: Boolean)

class Containers(tag: Tag) extends Table[Container](tag, "CONTAINERS") {

  def name = column[String]("NAME", O.PrimaryKey)
  def width = column[Float]("WIDTH")
  def height = column[Float]("HEIGHT")

  def create = (name: String, width: Float, height: Float) =>
    Container(name, width, height, isApproved = false)

  def destroy(container: Container) =
    Some((container.name, container.width, container.height))

  def * = (name, width, height) <> (create.tupled, destroy)
}
case类容器(名称:String,宽度:Float,高度:Float,isApproved:Boolean)
类容器(tag:tag)扩展表[Container](tag,“Containers”){
def name=列[字符串](“名称”,O.PrimaryKey)
定义宽度=列[浮动](“宽度”)
def高度=列[浮动](“高度”)
def create=(名称:字符串,宽度:浮点,高度:浮点)=>
容器(名称、宽度、高度,isApproved=false)
def销毁(容器:容器)=
一些((container.name、container.width、container.height))
def*=(名称、宽度、高度)(create.tuple、destroy)
}

谢谢你的帮助。但是,我没有尝试这个。我用另一种方式解决了它。谢谢你的帮助。但是,我没有尝试这个。我用另一种方式解决了它。将来交叉链接你的双帖子!将来交叉链接你的双帖子!var成员使你的程序更难理解,更容易出错(即有bug).看我的答案。是的,我知道使用var可能会产生问题,因为它会使变量可变。但是,我迫切需要解决这个问题。var成员会使您的程序更难理解,更容易出错(例如,出现错误).看我的答案。是的,我知道使用var可能会产生问题,因为它会使变量可变。但是,我迫切需要一个解决方案。我需要一个快速解决问题的方法。我希望你的答案有效。我会按照你的建议尝试解决方案。感谢你提供的详细答案。感谢。:)我需要一个快速解决问题的方法。我希望你你的答案很有效。我会按照你的建议尝试解决方案。谢谢你的详细答案。非常感谢。:)