Ebean没有坚持外键

时间:2014-12-31 14:05:30

标签: scala playframework ebean

我有一个简单的实体类,如;

case class Place(var name: String) extends Model with GeoLocPoint with HasGeoLoc with ContainsTime with HasId {

  var number: String = _ // Building number on the street.
  var placeGroupId: Long = _
  var chainId: Long = _
  @OneToOne
  @JoinColumn(name = "logo_id")
  var logo: Image = _
  @OneToOne
  @JoinColumn(name = "cover_id")
  var cover: Image = _
...
}

图像类是;

package models.images

import javax.persistence.Entity

import models.HasId
import models.places.placegroups.places.{Place, LinkedToPlace}
import play.api.libs.json._
import play.db.ebean.Model
import play.db.ebean.Model.Finder

/**
 * Created by asheshambasta on 31/12/14.
 */
@Entity
case class Image(
                  filePath: String,
                  src: String,
                  format: String,
                  role: String // C: cover, L: logo, G: gallery
                  ) extends Model with HasId with LinkedToPlace {
  var caption: String = _
}

现在,当创建一个地点时,我会附加一个默认图像;

  newPlace.save
  val imgDir = Play.current.configuration.getString("static.dir").getOrElse("/tmp")
  val imgUrl = Play.current.configuration.getString("static.url.img").getOrElse("/static/img")
  val imgType = Play.current.configuration.getString("img.default.type").getOrElse("image/jpeg")
  val coverFile = Play.current.configuration.getString("img.default.cover.file").getOrElse("default-cover.jpg")
  val logoFile = Play.current.configuration.getString("img.default.cover.file").getOrElse("default-cover.jpg")
  val cover = new Image(imgDir + "/" + coverFile, imgUrl + "/" + coverFile, imgType, "C")
  cover.place = newPlace
  cover.save
  val logo = new Image(imgDir + "/" + logoFile, imgUrl + "/" + logoFile, imgType, "L")
  logo.place = newPlace
  logo.save
  newPlace.cover = cover
  newPlace.logo = logo
  newPlace.update

但是每次,我都会看到图片使用正确的place_id正确保留,但地点标识和封面不是。他们保持无效。

这似乎是直截了当的,但它并没有奏效。有没有人指出原因?

1 个答案:

答案 0 :(得分:1)

提供的代码中有一些不确定性。有

logo.place = newPlace

place课程中没有Image字段。

还提到了place_id列,但我在代码中看不到它。但是有logo_idcover_id列。当您执行一对一关系时,您应该仅在此关系的一侧添加join-column。因此,logo_id表上应该只有cover_idPlace列。在place_id表上仅添加Image列将不起作用,因为Image表中有两行具有相同的place_id,这将无法区分应映射的哪一行到logo字段到cover字段。

我做了一些修正和简化,以使这段代码编译和工作。

Place.scala:

@Entity
class Place( var aId: Int, var aNumber: String) extends Model{

    @Id
    val id:Int=aId

    @Column(name="number")  
    var number: String = aNumber

    @OneToOne
    @JoinColumn (name = "logo_id") 
    var logo: Image = _

    @OneToOne
    @JoinColumn(name = "cover_id")
    var cover: Image = _
}

Image.class:

@Entity
@Table(name = "image")
class Image(var aId: Int, var aCaption: String) extends Model {

    @Id
    val id:Int=aId

    @Column(name="caption")
    var caption: String = aCaption
}

测试方法:

"EbeanTest" should {
    "placeTest" in new WithApplication {

        val newPlace = new Place(1, "aaa")
        newPlace.save

        val logo: Image  = new Image(1, "111");
        logo.save

        val cover: Image  = new Image(2, "222");
        cover.save

        newPlace.logo=logo
        newPlace.cover=cover
        newPlace.update


        val cList = Ebean.find(classOf[Place]).findList()
        for (
            element <- cList
        ) println(element.id+" "+element.number+" "+element.logo.id+" "+element.cover.id)           
    }
}