So I have come to an hold, and need help. I don't know how to continue with this and figuerd I should ask for help. I've look up some tutorials over youtube and over the internet on how to fix my problem but no one has helpt. I've tried numerous solutions but no one has worked.
The thing I'm trying to do is a Case Managment system(Simple dummy cms not a big one), i'm trying to expand my knowledge in java coding. The csm should run over a webapplication that will connect to MySQL, over the webapplication you should be able to add & edit. I went for creating it simply in JSP and Servlet.
I belive I've done right when it comes to the code but I'm not 100% sure since i'm not managing to run the webapp. My problem is that i'm getting an error fram Glassfish "Error compiling the query [Ärende.getAll: SELECT e FROM cases e]. Unknown entity type [cases]". I've searched the net and found that it's the SQL query that is wrong. But whatever I trie I cant get it to work.
I'm trying to access the database (which is mydb) through persistence.xml.
Here is my code
First we have setter and getter (Also some words are written in Swedish so I apologize if it's getting a little difficult to understand)
@Entity
@Table
@NamedQueries(@NamedQuery(name="Ärende.getAll",query = "SELECT e FROM cases e"))
The Query part is the part which I belive is wrong but even if I write "Ärende e" instead it does not workpublic class Ärende implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column private int idcases; @Column private String Namn; @Column private String Prioritet; @Column private String Status; public int getIdcases() { return idcases; } public void setIdcases(int idcases) { this.idcases = idcases; } public String getNamn() { return Namn; } public void setNamn(String Namn) { this.Namn = Namn; } public String getPrioritet() { return Prioritet; } public void setPrioritet(String Prioritet) { this.Prioritet = Prioritet; } public String getStatus() { return Status; } public void setStatus(String Status) { this.Status = Status; } public Ärende(int idcases, String Namn, String Prioritet, String Status) { this.idcases = idcases; this.Namn = Namn; this.Prioritet = Prioritet; this.Status = Status; } public Ärende(){} }
The Second thing I did was create "Ärende"Dao and Local
@Stateless public class ÄrendeDao implements ÄrendeDaoLocal { @PersistenceContext private EntityManager em; @Override public void addÄrende(Ärende ärende) { em.persist(ärende); } @Override public void editÄrende(Ärende ärende) { em.merge(ärende); } @Override public void deleteÄrende(int idcases) { em.remove(getÄrende(idcases)); } @Override public Ärende getÄrende(int idcases) { return em.find(Ärende.class, idcases); } @Override public List<Ärende> getAllÄrende() { return em.createNamedQuery("Ärende.getAll").getResultList(); } }
The Local
@Local public interface ÄrendeDaoLocal { void addÄrende(Ärende ärende); void editÄrende(Ärende ärende); void deleteÄrende(int idcases); Ärende getÄrende(int idcases); List<Ärende> getAllÄrende(); }
Alright so after that hard process I finaly created the Servlet and then the JSP
Servlet:
@WebServlet(name = "ÄrendeServlet", urlPatterns = {"/_rendeServlet"}) public class ÄrendeServlet extends HttpServlet { @EJB private ÄrendeDaoLocal ärendeDao;
protected void processRequest(HttpServletRequest request, HttpServletResponse >Response) throws ServletException, IOException { String action = request.getParameter("action"); String idcasesStr = request.getParameter("idcases"); int idcases=0; if(idcasesStr!=null && !idcasesStr.equals("")) idcases=Integer.parseInt(idcasesStr); String Namn = request.getParameter("Namn"); String Prioritet = request.getParameter("Prioritet"); String Status = request.getParameter("Status"); Ärende ärende = new Ärende(idcases, Namn, Prioritet, Status); if("Add".equalsIgnoreCase(action)){ ärendeDao.addÄrende(ärende); }else if("Edit".equalsIgnoreCase(action)){ ärendeDao.editÄrende(ärende); }else if("Delete".equalsIgnoreCase(action)){ ärendeDao.deleteÄrende(idcases); }else if("Search".equalsIgnoreCase(action)){ ärende = ärendeDao.getÄrende(idcases); } request.setAttribute("ärende", ärende); request.setAttribute("allÄrende", ärendeDao.getAllÄrende()); request.getRequestDispatcher("ärendeinfo.jsp").forward(request,response); }
JSP
<form action="./ÄrendeServlet" method="POST"> <td>Ärende ID</td> <td><input type="text" name="idcases" value="${ärende.idcases}" /></td> <td>Namn</td> <td><input type="text" name="Namn" value="${ärende.Namn}" /></td> <td>Prioritet</td> <td><input type="text" name="Prioritet" value="${ärende.Prioritet}" /> <td><input type="text" name="Status" value="${ärende.Status}" /></td> <input type="submit" name="action" value="Add" /> <input type="submit" name="action" value="Edit" /> <input type="submit" name="action" value="Delete" /> <input type="submit" name="action" value="Search" />
--- Same JSP this is just going to list all the cases below ---
<th>Ärende ID</th> <th>Namn</th> <th>Prioritet</th> <th>Status</th> <c:forEach items="${allÄrende}" var="stud"> <tr> <td>${stud.idcases}</td> <td>${stud.Namn}</td> <td>${stud.Prioritet}</td> <td>${stud.Status}</td> </tr> </c:forEach>
well that is all for the code part, and now When I try to runt it I get the error, my database is connected over glassfish JDBC and it does PING correctly.
Information about the database: the database tabel name that i'm trying to connect to is called: cases the database name is: mydb the hostname: root password is:vedo
I'm also whondering if I should do anything more with the persistence.xml, because I have not writen anything in the persistence.xml.
Can someone help me out with this? Sorry for throwing a lot of information in just one post but wanted to show evrething so that someone can throw me in the right direction.
Sorry for any misspelling.