0

We have following two tables. Item: item_id, name

ItemInfo: id, item_id, info_id, item_info

Item table can have multiple entries in ItemInfo.

I want to join above two tables at the same time I want to get the entries from the Item table which may not have any associated entry in ItemInfo. How can I do achieve this with the Hibernate criteria?

public class Item {
    private long item_id;
    private String name;

    public long getItem_id() {
        return item_id;
    }

    public void setItem_id(long item_id) {
        this.item_id = item_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class ItemInfo {
    private long id;
    private long item_id;
    private String item_info;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getItem_id() {
        return item_id;
    }

    public void setItem_id(long item_id) {
        this.item_id = item_id;
    }   


    public String getItem_info() {
        return item_info;
    }

    public void setItem_info(String item_info) {
        this.item_info = item_info;
    }
}
4

1 回答 1

1

First let me correct your POJOs.

public class ItemInfo {
  private long id;
  private Item item_id;
  private long info_id;
  private String item_info;

 //getter-setter of all
} 

Look for the change in datatype of item_id. Change the annotation/ mapping file accordingly.

Then try executing the following criteria.

Criteria crit = session.createCriteria(Item.class);
crit.setFetchMode("item_id", FetchMode.JOIN);
List<Item> itemList = criteria.list();
于 2013-08-24T11:49:30.853 回答