我正在尝试从一个相当简单的查询中获取结果,并在jsp页面上写出这些结果。使用Netbeans运行Glassfish 3.1。当我运行项目时,我从例程返回一个空列表。
当我右键单击“服务”中的表并点击“查看数据”时,我可以看到该表已填充,当我将查询复制并通过SQL命令窗口并运行它时,它会给出长度为2的预期列表。
有一些类似的问题(如this one),其中没有一个似乎有太多帮助。我是新手,所以我可能没有理解其他问题的解决方案。
这里有很多因素在起作用,我不确定要包括什么。我已经包含了执行查询的例程,调用该例程的JSP代码,我正在使用的实体类,以及我在项目运行时获得的服务器日志的输出。如果需要更多信息,请告诉我,我会把它提出来。
我真的很感激任何帮助。
格里夫
常规执行查询:
public static LinkedList<String> getCategories(EntityManager entityManager) {
try {
Query query = entityManager.createQuery(
"SELECT DISTINCT i.category from ItemEntity i");
List resultList = query.getResultList();
if (!resultList.isEmpty()) {
return new LinkedList<String>(resultList);
}
} catch(Exception e) {
System.out.println(e);
} finally {
return new LinkedList<String>();
}
}
调用例程的JSP代码:
<body>
<h1>Store</h1>
<h2>Categories</h2>
<%
Context environmentContext
= (Context) new InitialContext().lookup("java:comp/env");
EntityManager entityManager
= (EntityManager) environmentContext.lookup("persistence/dbunit");
LinkedList<String> categories = DataBase.getCategories(entityManager);
ListIterator<String> categoryIterator = categories.listIterator();
String category = "";
%>
<form action="category.jsp">
<% while (categoryIterator.hasNext()) { %>
<% category = categoryIterator.next(); %>
<input type="submit" class="submitButtonAsLink" value="<%= category %>" name="<%= category %>" /><br />
<% } %>
</form>
</body>
ItemEntity.java:
@Entity
public class ItemEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private String title;
private String longDescription;
private double cost;
private String category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof ItemEntity)) {
return false;
}
ItemEntity other = (ItemEntity) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "store.model.entities.ItemTable[ id=" + id + " ]";
}
//getters and setters omitted.
//There are getters and setters for every field except id.
}
服务器日志:
INFO: file:/Users/griffgeorge/Dropbox/school/design-arch/labs/Lab4Exercise/build/web/WEB-INF/classes/_Lab4ExercisePU logout successful
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
INFO: EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
INFO: file:/Users/griffgeorge/Dropbox/school/design-arch/labs/Lab4Exercise/build/web/WEB-INF/classes/_Lab4ExercisePU login successful
WARNING: Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@1ed957d].
WARNING: JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@1ed957d] from index [0]
WARNING: JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@1a84f3c] from index [1]
WARNING: PER01000: Got SQLException executing statement "CREATE TABLE ITEMENTITY (ID VARCHAR(255) NOT NULL, CATEGORY VARCHAR(255), COST FLOAT, LONGDESCRIPTION VARCHAR(255), TITLE VARCHAR(255), PRIMARY KEY (ID))": java.sql.SQLException: Table/View 'ITEMENTITY' already exists in Schema 'APP'.
WARNING: PER01000: Got SQLException executing statement "CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))": java.sql.SQLException: Table/View 'SEQUENCE' already exists in Schema 'APP'.
WARNING: PER01000: Got SQLException executing statement "INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0)": java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL111012114449870' defined on 'SEQUENCE'.
INFO: WEB0671: Loading application [Lab4Exercise] at [/Lab4Exercise]
答案 0 :(得分:3)
您的getCategories()
方法始终会返回一个空列表,因为finally
中的返回始终会运行(即使在第一个return
之后)。
根本不需要finally
条款。你可以简化到这个
public static LinkedList<String> getCategories(EntityManager entityManager) {
try {
Query query = entityManager.createQuery(
"SELECT DISTINCT i.category from ItemEntity i");
return new LinkedList<String>(query.getResultList());
} catch(Exception e) {
System.out.println(e);
return new LinkedList<String>();
}
}
如果query.getResultList()
不为空,则返回非空字符串链接列表。
如果query.getResultList()
为空,则返回一个空的字符串链表。
如果发生异常,则返回空列表。 正如您之前所做的那样,即使您的查询返回数据,也始终返回一个空列表。
要证明finally
中的回报是通过的,请看一下
public class TestFinally {
public static void main(String[] args) {
System.out.println(TestFinally.test());
}
public static int test() {
try {
return 1;
} finally {
return 0;
}
}
}
返回
0
答案 1 :(得分:-1)
你的persistence.xml怎么样?是指向您期望的数据源吗?然后是指向您期望的连接池的数据源吗?