【问题标题】:2 foreign fields to the same class when using ormlite使用 ormlite 时同一个类的 2 个外部字段
【发布时间】:2012-03-14 09:58:42
【问题描述】:

使用 ormlite,我有 2 个类:Menu 和 MenuItem。我想在 MenuItem 中有两个指向 Menu 类的外部字段。但是,这似乎不起作用。对同一类只使用一个外部字段确实有效。我怎样才能实现我想要的?

@DatabaseTable
public class MenuItem {

   /* ... */

   @DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "destination_submenu")
    public Menu destinationSubmenu;

    @DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "menu")
    public Menu menu;
}

【问题讨论】:

    标签: foreign-keys ormlite


    【解决方案1】:

    我发现了问题所在。我在 Menu 类中使用了 ForeignCollectionField,但因为我在 MenuItem 中有 2 个外键,所以它没有按预期工作。解决方案是使用 foreignColumnName。

    @DatabaseTable
    public class Menu {
    
    /* ... */
        @ForeignCollectionField(foreignColumnName = "menu")
        @JsonIgnore
        private ForeignCollection<MenuItem> items;
    }
    

    【讨论】:

    • 留给后代:foreignColumnName 在 4.36 版本中,目前(2012 年 3 月)处于 SNAPSHOT 状态。
    猜你喜欢
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多