2014-12-31 95 views
1

我有一個簡單的實體類像;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的地方,而是標誌和蓋正確地堅持不要。他們保持空。

這看起來很簡單,但它不起作用。有沒有人有任何指向爲什麼?

+0

是我的回答有幫助嗎?我沒有收到你的任何反饋。 – rtruszk

回答

1

提供的代碼中有一些innacuracies。有

logo.place = newPlace 

但在Image類沒有place場。

還有place_id列提到,但我不能在代碼中看到它。但有logo_idcover_id列。當你做一對一的關係時,你應該只在這個關係的一邊添加連接列。因此Place表中只有logo_idcover_id列。僅在Image表中添加place_id列將不起作用,因爲在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)   
    } 
}