如何使用hibernate获取OneToMany参考数据

时间:2014-03-20 09:31:49

标签: hibernate spring-mvc

我想使用SpringMvc + Spring + hibernate创建一个OrderMeal Web项目。 但是我发现在jsp页面上打印数据时遇到了一些问题。

当我调试项目时(对不起,我不能发布调试图片T_T)

我无法弄清楚为什么财产" mealOrders"总是空的...... 但我已经在表格中插入了许多数据。

这是我的实体和HQL查询

  

用户类

 @Entity
@Table(name="UserBaseInfo")
public class User implements Serializable  {

    private static final long serialVersionUID = 1L;

    private Long userID;//id
    private String userName;
    private String loginname;
    private String userPassword;
    private String cleartextPassword;
    private String postion;
    private String telePhone;
    private String email;
    private String userLocation;
    private Date createTime;
    private Date lastEditTime;
    private Date passwordEditTime;
    private int onlineTime;
    private String lastLoginIP;
    private int loginCount;
    private int userState;

    private Set<MealOrder> mealOrders = new HashSet<MealOrder>();
    @Id
    @GeneratedValue
    @Column(name = "ID")
    public Long getUserID() {
        return userID;
    }
    ...I hide some useless propertys
    @OneToMany(targetEntity=MealOrder.class, fetch=FetchType.LAZY, cascade={CascadeType.ALL}, mappedBy = "user")
    public Set<MealOrder> getMealOrders() {
        return mealOrders;
    }

    public void setMealOrders(Set<MealOrder> mealOrders) {
        this.mealOrders = mealOrders;
    }
  

MealOrder类

@Entity
@Table(name="MealOrder")
public class MealOrder  implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long mealOrderID;
    private int orderState;
    //private Long userID;
    private String userName;
    private String postion;
    private String telePhone;
    private String userLocation;
    private Date orderTime;
    private String restaurantName;
    private int restaurantID;
    private String supplyTimeType;
    private Date beginSupplyTime;
    private Date endSupplyTime;
    private Date beginSupplyDate;
    private Date endSupplyDate;
    private int mealMenuID;
    private String mealMenuName;
    private String mealPackageName;
    private int mealPackageID;
    private float mealPackagePrice;

    private User user;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public Long getMealOrderID() {
        return mealOrderID;
    }

    public void setMealOrderID(Long mealOrderID) {
        this.mealOrderID = mealOrderID;
    }
    ...I hide some useless propertys
    @ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name = "UserID")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

和下面的HQL查询

@SuppressWarnings("unchecked")
@Override
public List<User> FindAllUsers() {
    Session session = sessionFactory.openSession();
    org.hibernate.Query query = session.createQuery("from User u left join u.mealOrders");
    return (List<User>) query.list();
}

和我的jsp页面(它无法打印数据)

<c:forEach items="${users}" var="user">
            <tr>
                <td>${user.userName}</td>
                <td>${user.userID}</td>
            </tr>
        </c:forEach>

如何在jsp页面上打印数据?

2 个答案:

答案 0 :(得分:1)

  1. 您可以使获取类型EAGER强制用户始终检索它的子项(如果您不总是需要所有数据,则不需要性能)。
  2. 您可以在交易仍处于打开状态时“获取”子项,这将导致对数据库的另一个查询。
  3. 您可以将查询更改为“left join fetch ”以强制创建对象。

答案 1 :(得分:0)

我不明白为什么在已经有映射的情况下需要此查询。如果你给出简单的select语句,那可能有用。避免加入查询。