- This topic has 1 reply, 2 voices, and was last updated 17 years, 6 months ago by Riyad Kalla.
-
AuthorPosts
-
tommyc7MemberHi. I’m trying to use the MyEclipse IDE to create a JSP page that will insert, update and delete data in my DB. Insert and delete are working fine. However, the update is behaving as an insert operation. Here are the details, hopeful this is enough. My installation informationm is at the bottom.
Here is the table:
CREATE TABLE Category ( id int(11) NOT NULL auto_increment, description varchar(50) NOT NULL default '', PRIMARY KEY (id) ) Engine=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
Here is the CategoryDAO class (auto-generated by MyEclipse):
public class CategoryDAO { // property constants public static final String DESCRIPTION = "description"; private EntityManager getEntityManager() { return EntityManagerHelper.getEntityManager(); } public void save(Category transientInstance) { EntityManagerHelper.log("saving Category instance", Level.INFO, null); try { getEntityManager().persist(transientInstance); EntityManagerHelper.log("save successful", Level.INFO, null); } catch (RuntimeException re) { EntityManagerHelper.log("save failed", Level.SEVERE, re); throw re; } } public void delete(Category persistentInstance) { EntityManagerHelper.log("deleting Category instance", Level.INFO, null); try { getEntityManager().remove(persistentInstance); EntityManagerHelper.log("delete successful", Level.INFO, null); } catch (RuntimeException re) { EntityManagerHelper.log("delete failed", Level.SEVERE, re); throw re; } } public Category update(Category detachedInstance) { EntityManagerHelper.log("updating Category instance", Level.INFO, null); try { Category result = getEntityManager().merge(detachedInstance); EntityManagerHelper.log("update successful", Level.INFO, null); return result; } catch (RuntimeException re) { EntityManagerHelper.log("update failed", Level.SEVERE, re); throw re; } } public Category findById(Integer id) { EntityManagerHelper.log("finding Category instance with id: " + id, Level.INFO, null); try { Category instance = getEntityManager().find(Category.class, id); return instance; } catch (RuntimeException re) { EntityManagerHelper.log("find failed", Level.SEVERE, re); throw re; } } @SuppressWarnings("unchecked") public List<Category> findByProperty(String propertyName, Object value) { EntityManagerHelper.log("finding Category instance with property: " + propertyName + ", value: " + value, Level.INFO, null); try { String queryString = "select model from Category model where model." + propertyName + "= :propertyValue"; return getEntityManager().createQuery(queryString).setParameter( "propertyValue", value).getResultList(); } catch (RuntimeException re) { EntityManagerHelper.log("find by property name failed", Level.SEVERE, re); throw re; } } public List<Category> findByDescription(Object description) { return findByProperty(DESCRIPTION, description); } @SuppressWarnings("unchecked") public List<Category> findAll() { EntityManagerHelper.log("finding all Category instances", Level.INFO, null); try { String queryString = "select model from Category model"; return getEntityManager().createQuery(queryString).getResultList(); } catch (RuntimeException re) { EntityManagerHelper.log("find all failed", Level.SEVERE, re); throw re; } } }
Here’s a snippet of category.jsp (the intial list of data that can be maintained):
<h1>Category Maintenance</h1> <p class="intro">This page is for maintaining categories.</p> <br /> <% CategoryHtml html = new CategoryHtml(); String ident = (String)request.getParameter("ident"); %> <%= html.getUpdateDeleteForm() %> <br /><br /><br /> <h1>Upddate a Category:</h1> <jsp:useBean id="catbean" class="com.blah.data.Category" scope="session"/> <jsp:setProperty name="catbean" property="*"/> <% if (ident != null) { CategoryDAO dao = new CategoryDAO(); catbean = dao.findById(Integer.parseInt(ident)); %> <FORM METHOD=POST Action=update.jsp?type=<%= HtmlElement.CATEGORY %>> <% } else { %> <FORM METHOD=POST Action=add.jsp?type=<%= HtmlElement.CATEGORY %>> <% } %> <TABLE> <TR> <TD>Description</TD> <TD><INPUT TYPE=TEXT SIZE=25 Name=description VALUE="<%= catbean.getDescription()%>"></TD> </TR> </TABLE> <INPUT TYPE=SUBMIT value="Submit"> </FORM>
And finally update.jsp:
<% EntityManagerHelper.beginTransaction(); int type = Integer.parseInt(request.getParameter("type")); switch (type) { case HtmlElement.CATEGORY: %> <jsp:useBean id="catbean" class="com.blah.data.Category" scope="page"/> <jsp:setProperty name="catbean" property="*"/> <% CategoryDAO dao = new CategoryDAO(); dao.save(catbean); break; } EntityManagerHelper.commit(); EntityManagerHelper.closeEntityManager(); %>
I’ve tried the save method (as used in the tutorial) and just for fun, the update method. I get the same results with both.
Am I doing something wrong? Could this be related to the table being auto-incrememented? If so, is there another way that I should be doing auto-increment? Or is there a bug that I’m not seeing?
Thanks!
Tom*** Date:
Sunday, July 22, 2007 7:33:44 PM EDT** System properties:
OS=WindowsXP
OS version=5.1
Java version=1.5.0_11*** MyEclipse details:
MyEclipse Enterprise Workbench
Version: 5.5.1 GA
Build id: 20070521-5.5.1-GA*** Eclipse details:
MyEclipse Enterprise WorkbenchVersion: 5.5.1 GA
Build id: 20070521-5.5.1-GAEclipse Graphical Editing Framework
Version: 3.2.2.v20070208
Build id: 20070208-1315Eclipse Platform
Version: 3.2.2.r322_v20070119-RQghndJN8IM0MsK
Build id: M20070212-1330Eclipse RCP
Version: 3.2.2.r322_v20070104-8pcviKVqd8J7C1U
Build id: M20070212-1330Eclipse Java Development Tools
Version: 3.2.2.r322_v20070104-R4CR0Znkvtfjv9-
Build id: M20070212-1330Eclipse Plug-in Development Environment
Version: 3.2.1.r321_v20060823-6vYLLdQ3Nk8DrFG
Build id: M20070212-1330Eclipse Project SDK
Version: 3.2.2.r322_v20070104-dCGKm0Ln38lm-8s
Build id: M20070212-1330Eclipse startup command=-os
win32
-ws
win32
-arch
x86
-launcher
C:\Tools\MyEclipse 5.5.1 GA\eclipse\eclipse.exe
-name
Eclipse
-showsplash
600
-exitdata
f3c_50
-vm
C:\Tools\MyEclipse 5.5.1 GA\jre\bin\javaw.exe
Riyad KallaMemberIf you want to update an entity you have to make sure the ID for it is set, otherwise JPA sees it as a new object to insert.
-
AuthorPosts