2013-05-11 63 views
1

我一直在這個問題上徘徊數小時。我有兩個型號:Play Framework 2.1和Ebean:模型Finder不返回任何數據

@Entity 
@Table(name = "app_user") 
public class AppUser extends Model { 

    @Id 
    Long id; 

    … 

    @Constraints.Required 
    @Valid 
    @OneToOne(cascade = CascadeType.ALL, optional = false) 
    public LocationAddress address; 

    @Valid 
    @OneToOne(cascade = CascadeType.ALL, optional = true) 
    public LocationAddress addressBilling; 

    … 

} 

@Entity 
@Table(name = "location_address") 
public class LocationAddress extends Model { 

    @Id 
    Long id; 

    @Constraints.Required 
    @Constraints.MaxLength(TextSize.DEFAULT) 
    @Column(length = TextSize.DEFAULT, nullable = false) 
    public String street; 

    @Constraints.MaxLength(TextSize.TINY) 
    @Column(length = TextSize.TINY) 
    public String streetNo; 

    @ManyToOne(cascade = CascadeType.PERSIST, optional = false) 
    public Country country; 

    … 

    @OneToOne(mappedBy = "address") 
    public AppUser userAddress; 

    @OneToOne(mappedBy = "addressBilling") 
    public AppUser userAddressBilling; 

    @OneToOne(mappedBy = "address") 
    public AdvertisingLocation advertisingLocationAddress; 

    // -- Queries 

    public static Finder<Long, LocationAddress> find = new Finder<Long, LocationAddress>(Long.class, LocationAddress.class); 

    public static List<LocationAddress> all() { 
     return find.all(); 
    } 

    public static LocationAddress findById(long id) { 
     return find.byId(id); 
    } 

} 

的問題是,LocationAddress.all()回報什麼,從而AppUser.findById(1).address.street拋出EntityNotFoundException: Bean has been deleted - lazy loading failed。不用說,數據庫表不是空的。

有趣的是,Ebean.find(LocationAddress.class).findRowCount()返回3(這是正確的)。

任何人看到可能是什麼問題?謝謝。

回答

0

經過很多小時我終於找到了問題的根源。如果爲null,則不需要LocationAddress中的@OneToOne字段,並導致實體不能返回。

+1

我不明白答案。你是如何解決問題的?我看到同樣的事情。 – 2013-09-10 14:37:25

+0

如果它是一個雙向關係,則關係的兩邊不得爲null,以便返回的對象,如此處所示。我將其改爲單向關係,因此即使一方爲空也會返回對象。 – KristijanBambir 2013-09-16 06:15:57

+0

啊。你從類中刪除了所有'@ OneToOne'字段,然後'find.all'worked。我現在明白了。但在我看來,更需要挖掘它。在你的情況下,你不需要它們。其他人可能需要他們。你可以發佈你的數據庫,這樣我可以重現這一點? – 2013-09-17 04:30:18

0

我遇到了這個問題幾次。 (玩1也是。)

我解決這個問題的方法是將字段設置爲private並用getter/setter包裝它。

相關問題