· 8 years ago · Aug 20, 2018, 02:28 AM
1Why can I not get any results from this rather trivial query?
2public static LinkedList<String> getCategories(EntityManager entityManager) {
3 try {
4 Query query = entityManager.createQuery(
5 "SELECT DISTINCT i.category from ItemEntity i");
6 List resultList = query.getResultList();
7 if (!resultList.isEmpty()) {
8 return new LinkedList<String>(resultList);
9 }
10 } catch(Exception e) {
11 System.out.println(e);
12 } finally {
13 return new LinkedList<String>();
14 }
15}
16
17<body>
18 <h1>Store</h1>
19 <h2>Categories</h2>
20 <%
21 Context environmentContext
22 = (Context) new InitialContext().lookup("java:comp/env");
23 EntityManager entityManager
24 = (EntityManager) environmentContext.lookup("persistence/dbunit");
25
26 LinkedList<String> categories = DataBase.getCategories(entityManager);
27
28 ListIterator<String> categoryIterator = categories.listIterator();
29 String category = "";
30 %>
31
32 <form action="category.jsp">
33 <% while (categoryIterator.hasNext()) { %>
34 <% category = categoryIterator.next(); %>
35 <input type="submit" class="submitButtonAsLink" value="<%= category %>" name="<%= category %>" /><br />
36 <% } %>
37 </form>
38</body>
39
40@Entity
41public class ItemEntity implements Serializable {
42 private static final long serialVersionUID = 1L;
43
44@Id @GeneratedValue(strategy = GenerationType.AUTO)
45 private String id;
46
47 private String title;
48 private String longDescription;
49 private double cost;
50 private String category;
51
52 public String getId() {
53 return id;
54 }
55
56 public void setId(String id) {
57 this.id = id;
58 }
59
60 @Override
61 public int hashCode() {
62 int hash = 0;
63 hash += (id != null ? id.hashCode() : 0);
64 return hash;
65 }
66
67 @Override
68 public boolean equals(Object object) {
69 // TODO: Warning - this method won't work in the case the id fields are not set
70 if (!(object instanceof ItemEntity)) {
71 return false;
72 }
73 ItemEntity other = (ItemEntity) object;
74 if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
75 return false;
76 }
77 return true;
78 }
79
80 @Override
81 public String toString() {
82 return "store.model.entities.ItemTable[ id=" + id + " ]";
83 }
84
85 //getters and setters omitted.
86 //There are getters and setters for every field except id.
87}
88
89INFO: file:/Users/griffgeorge/Dropbox/school/design-arch/labs/Lab4Exercise/build/web/WEB-INF/classes/_Lab4ExercisePU logout successful
90INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
91INFO: EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
92INFO: file:/Users/griffgeorge/Dropbox/school/design-arch/labs/Lab4Exercise/build/web/WEB-INF/classes/_Lab4ExercisePU login successful
93WARNING: Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@1ed957d].
94WARNING: JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@1ed957d] from index [0]
95WARNING: JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@1a84f3c] from index [1]
96WARNING: 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'.
97WARNING: 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'.
98WARNING: 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'.
99INFO: WEB0671: Loading application [Lab4Exercise] at [/Lab4Exercise]
100
101public static LinkedList<String> getCategories(EntityManager entityManager) {
102 try {
103 Query query = entityManager.createQuery(
104 "SELECT DISTINCT i.category from ItemEntity i");
105 return new LinkedList<String>(query.getResultList());
106 } catch(Exception e) {
107 System.out.println(e);
108 return new LinkedList<String>();
109 }
110}
111
112public class TestFinally {
113 public static void main(String[] args) {
114 System.out.println(TestFinally.test());
115 }
116
117 public static int test() {
118 try {
119 return 1;
120 } finally {
121 return 0;
122 }
123 }
124}
125
1260