如何在表单中显示列表中的值(来自数据库的值)?

时间:2015-01-25 23:59:41

标签: hibernate list jsp spring-mvc createquery

在我的数据库中,我有3个表,显示在图像上。我不想从商店表中获取数据并以表格形式显示。但我的列表有问题。在数据库中查询工作很好,并且在程序中也得到了对象,但是不希望在表单上显示。什么问题??

实体商店

@Entity
@Table(name = "store")
public class Store{

private int id;
private String name;
private String address;

public Store(){
    name = null;
}
public Store(Store store){
    name = store.getName();
}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")

@Column(name = "id")
public int getId(){
    return id;
}

@Column(name = "name")
public String getName(){
    return name;
}

@Column(name = "address")
public String getAddress(){
    return address;
}

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

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

public void setAddress(String address) {
    this.address = address;
}

//refer Store[idDirector] -> User[id]
//@Column(name = "idDirector")
private User user;
@ManyToOne//(fetch = FetchType.LAZY)
@JoinColumn(name = "idDirector")
public User getUser(){
    return user;
}
public void setUser(User user){
    this.user = user;
}

//refer Store[idType] -> StoreType[id]
private StoreType storeType;
@ManyToOne//(fetch = FetchType.LAZY)
@JoinColumn(name = "idType")
public StoreType getStoreType(){
    return storeType;
}
public void setStoreType(StoreType storeType){
    this.storeType = storeType;
}
//\refer


//refer Store[id] -> CatalogStore[idStore]
private Set<CatalogStore> catalogStores = new HashSet<CatalogStore>(
        0);
@OneToMany//(fetch = FetchType.LAZY)
public Set<CatalogStore> getCatalogStore(){
    return this.catalogStores;
}
public void setCatalogStore(Set<CatalogStore> catalogStores){
    this.catalogStores = catalogStores;
}
//\refer

}

DAO获取列表方法

@Override
public List<Store> getAllStoresWithoutCurrentGoods(int userId, int goodsId) throws SQLException {
    Session session = null;
    Transaction tx = null;
    List<Store> stores = new ArrayList<Store>();
    try {
        session = this.sessionFactory.openSession();
        tx = session.beginTransaction();
        stores = session.createSQLQuery("select t1.id, t1.name, t1.idType, t1.idDirector " +
                            "from Store t1 " +
                            "join store_catalog t2 on(t1.id = t2.idStore) " +
                            "where t1.idDirector = " + userId + " and t2.idGoodsOnFirm <> " + goodsId + " group by t1.id").list();

        //alternative in hql
        /*stores=session.createQuery("from Store u, CatalogStore u1 " +
                                    "where (u.user.id = :userId) and " +
                                    "(u1.catalogCompany.id != :goodsId)" +
                                    "group by u.id")
                                    .setParameter("goodsId", goodsId).setParameter("userId", userId).list();*/
        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        if (session != null && session.isOpen())
            session.close();
    }
    return stores;
}

查看

<c:if test="${!empty storeList}">
<table>
    <c:forEach items="${storeList}" var="store">
        <tr class="editField">

            <td><c:out value="${store.id}" />
            </td>
            <td><c:out value="${store.name}" />
            </td><!--
            <td><//c:out value="${store.storeType.name}" />
            </td>
            <td><//c:out value="${store.address}" />
            </td>-->
            <td><input type="button" id = 'addInThisStore' value="Add"></td>
        </tr>
    </c:forEach>


</table>

栈跟踪

58:         <c:forEach items="${storeList}" var="store">
59:             <tr class="editField">
60: 
61:                 <td><c:out value="${store.id}" />
62:                 </td>
63:                 <td><c:out value="${store.name}" />
64:                 </td><!--


Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "id"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:158)
...

enter image description here

1 个答案:

答案 0 :(得分:0)

解决了:

  @Override
public List<Store> getAllStoresWithoutCurrentGoods(int userId, int goodsId) throws SQLException {
    Session session = null;
    List<Store> stores = new ArrayList<Store>();
    try {
        session = this.sessionFactory.openSession();
        stores = session.createQuery("from Store u where (u.user.id = :userId) and" +
                                    " not exists(from CatalogStore u1 where" +
                                    " (u1.store.id = u.id) and (u1.catalogCompany.id = :goodsId))")
                .setParameter("goodsId", goodsId).setParameter("userId", userId).list();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null && session.isOpen())
            session.close();
    }
    return stores;
}
相关问题