Catalin Morosan
Catalin Morosan

Reputation: 7937

2 foreign fields to the same class when using ormlite

Using ormlite, I have 2 classes: Menu and MenuItem. I want to have 2 foreign fields inside MenuItem that both point to a Menu class. However, this doesn't seem to work. Using only one foreign field for the same class does work. How can I achieve what I want?

@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;
}

Upvotes: 0

Views: 444

Answers (1)

Catalin Morosan
Catalin Morosan

Reputation: 7937

I figured out the problem. I had used in the Menu class a ForeignCollectionField but because I had 2 foreign keys in MenuItem, it didn't work as expected. The solution was to use foreignColumnName.

@DatabaseTable
public class Menu {

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

Upvotes: 2

Related Questions