Re: Relationships Not Propagated

From: Jamie (jami..timulussoft.com)
Date: Fri Sep 18 2009 - 10:07:05 EDT

  • Next message: John Armstrong: "Re: Cayenne-3: Core JAR Files"

    Hi Michael

    See below:

    Michael Gentry wrote:
    > Just do:
    >
    > partner.addToCustomers(customer);
    >
    > You don't need to do both lines. How did you create partner and
    > customer?
    Partner partner =
    ((BusinessServicesPrincipal)request.getUserPrincipal()).getPartner();
    DataContext context =
    ((BusinessServicesPrincipal)request.getUserPrincipal()).getDataContext();
    Customer customer = (Customer) context.newObject(Customer.class);

    Actually, I have a Principal object that stores the DataContext and i
    use that through out. The context is only created once:

    DataContext context = DataContext.createDataContext();

    > Did you create partner in a DataContext or fetch it from
    > the DB?

    The Partner is obtained from the DB at the point where the partner logs in:

    List<Partner> partners = context.performQuery(select);
               if (partners.size()>0) {
                   for (Partner partner : partners) {
                       String pass = partner.getPassword();
                       if (partner.getContactEmail()==username &&
    (partner.getPassword()==null || partner.getPassword().equals(password))) {
                           return new
    BusinessServicesPrincipal("partner",partner,context);
                       }
                   }
               }
               return null;

    > Is customer created in the same DataContext as partner?
    >
    Yes. I ran into this error before. Now I DataContext everything in the
    Principal and access the Principal from the JSP.

    See attached for source and config files.
    > mrg
    >
    >
    > On Fri, Sep 18, 2009 at 3:59 AM, Jamie <jami..timulussoft.com> wrote:
    >
    >> Hi Michael
    >>
    >> Thanks for the help. Next question. I have a one to many Partner -> Customer
    >> relationship defined.
    >> When creating a new customer, I try to setup the relationship as follows:
    >>
    >> partner.addToCustomers(customer);
    >> customer.setPartner(partner);
    >>
    >> I get a NullPointer, even though customer and partner are not null.
    >>
    >> SEVERE: Servlet.service() for servlet jsp threw exception
    >> java.lang.NullPointerException
    >> at
    >> org.apache.cayenne.CayenneDataObject.setReverseRelationship(CayenneDataObject.java:385)
    >> at
    >> org.apache.cayenne.CayenneDataObject.setToOneTarget(CayenneDataObject.java:339)
    >> at com.stimulus.support.auto._Customer.setPartner(_Customer.java:97)
    >> at
    >> org.apache.jsp.customerdetail_jsp._jspService(customerdetail_jsp.java:94)
    >> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    >> at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    >> at
    >> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    >> at
    >> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    >> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    >> at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    >> at
    >> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    >> at
    >> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    >> at
    >> org.apache.cayenne.conf.WebApplicationContextFilter.doFilter(WebApplicationContextFilter.java:91)
    >> at
    >> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    >> at
    >> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    >> at
    >> org.securityfilter.filter.SecurityFilter.doFilter(SecurityFilter.java:188)
    >> at
    >> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    >> at
    >> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    >> at
    >> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    >> at
    >> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    >> at
    >> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    >> at
    >> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    >> at
    >> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    >> at
    >> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    >> at
    >> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    >> at
    >> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    >> at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    >> at java.lang.Thread.run(Thread.java:619)
    >>
    >> Any ideas?
    >>
    >> Jamie
    >>
    >> Michael Gentry wrote:
    >>
    >>> When you create a brand new ObjEntity from a DBEntity, it propagates
    >>> all attributes and relationships defined in the DBEntity except for
    >>> the primary key (Cayenne Modeler assumes you don't need that exposed).
    >>> Once you've created the ObjEntity, though, adding a new relationship
    >>> to the DBEntity will not be automatically synced to the ObjEntity.
    >>> You have to sync it manually (click the double-arrow icon in either
    >>> the DBEntity or ObjEntity toolbar or use the Project menu or keyboard
    >>> shortcut). I'm talking mainly about 3.0M6 since I no longer use the
    >>> 2.x modeler and don't remember the exact specifics of how it behaves.
    >>>
    >>> mrg
    >>>
    >>>
    >>> On Thu, Sep 17, 2009 at 3:46 AM, Jamie <jami..timulussoft.com> wrote:
    >>>
    >>>
    >>>> Hi Everyone
    >>>>
    >>>> I am really struggling to use the Cayenne Modelling tool. It seems
    >>>> whenever
    >>>> I create an ObjectEntity from the DBEntity the relationships are not
    >>>> always
    >>>> propagated. This happens even when I follow the ARTIST tutorial. I tried
    >>>> both Cayenne 3.0M6 and Cayenne 2.0.4 and the same behaviour is observed.
    >>>> Is
    >>>> this a known bug or am I likely doing something horribly wrong?
    >>>>
    >>>> Thank in advance
    >>>>
    >>>>
    >>>> Jamie
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>

    -- 
    Stimulus Software - MailArchiva
    Email Archiving And Compliance
    USA Tel: +1-713-343-8824 ext 100
    UK Tel: +44-20-80991035 ext 100
    Email:  jami..timulussoft.com
    Web: http://www.mailarchiva.com
    To receive MailArchiva Enterprise Edition product announcements, send a message to: <mailarchiva-enterprise-edition-subscrib..timulussoft.com>
    




    <%@ page language="java" contentType="text/html" %> <%@ page import="org.apache.cayenne.*" %> <%@ page import="org.apache.cayenne.access.*" %> <%@ page import="java.util.*" %> <%@ page import="java.text.*" %> <%@ page import="com.stimulus.support.*" %> <%@ page import="com.stimulus.support.realm.*"%> <% Partner partner = ((BusinessServicesPrincipal)request.getUserPrincipal()).getPartner(); out.println(partner.getCompanyName()); DataContext context = ((BusinessServicesPrincipal)request.getUserPrincipal()).getDataContext(); String id = request.getParameter("id"); Customer customer = null; if(id != null && id.trim().length() > 0) { customer = (Customer) DataObjectUtils.objectForPK(context, Customer.class, Integer.parseInt(id)); }

    if("POST".equals(request.getMethod())) { if (request.getParameter("delete")!=null) { out.println(request.getParameter("delete")); customer = (Customer) DataObjectUtils.objectForPK(context, Customer.class, Integer.parseInt(id)); partner.removeFromCustomers(customer); context.deleteObject(customer); } else { if(customer == null) { customer = (Customer) context.newObject(Customer.class); } customer.setCompanyName(request.getParameter("companyName")); customer.setTechnicalContactEmail(request.getParameter("technicalContactEmail")); customer.setTechnicalContactName(request.getParameter("technicalContactName")); customer.setTechnicalContactTel(request.getParameter("technicalContactTel")); customer.setFinanceContactEmail(request.getParameter("financeContactEmail")); customer.setFinanceContactName(request.getParameter("financeContactName")); customer.setFinanceContactTel(request.getParameter("financeContactTel")); partner.addToCustomers(customer); } context.commitChanges(); response.sendRedirect("managecustomers.jsp"); }

    if(customer == null) { customer = new Customer(); }

    String companyName = customer.getCompanyName()==null?"":customer.getCompanyName(); String technicalContactEmail = customer.getTechnicalContactEmail() ==null? "":customer.getTechnicalContactEmail(); String technicalContactName = customer.getTechnicalContactName()==null ? "":customer.getTechnicalContactName(); String technicalContactTel = customer.getTechnicalContactTel()==null ? "":customer.getTechnicalContactTel(); String financeContactEmail = customer.getFinanceContactEmail() ==null? "":customer.getFinanceContactEmail(); String financeContactName = customer.getFinanceContactName()==null ? "":customer.getFinanceContactName(); String financeContactTel = customer.getFinanceContactTel()==null ? "":customer.getFinanceContactTel(); %> <html> <head> <title>Customer Details</title> </head> <body> <img src="images/mailarchiva_logo.jpg" alt="MailArchiva" /> <h2>Customer Details</h2> <form name="EditCustomer" action="customerdetail.jsp" method="POST"> <input type="hidden" name="id" value="<%= id != null ? id : "" %>" /> <table border="0"> <tr> <td>Company Name:</td> <td><input type="text" name="companyName" value="<%= companyName %>"/></td> </tr> <tr> <td>Technical Contact Name:</td> <td><input type="text" name="technicalContactName" value="<%= technicalContactName %>"/></td> </tr> <tr> <td>Technical Contact Email:</td> <td><input type="text" name="technicalContactEmail" value="<%= technicalContactEmail %>"/></td> </tr> <tr> <td>Technical Contact Tel.:</td> <td><input type="text" name="technicalContactTel" value="<%= technicalContactTel %>"/></td> </tr> <tr> <td>Finance Contact Name:</td> <td><input type="text" name="financeContactName" value="<%= financeContactName %>"/></td> </tr> <tr> <td>Finance Contact Email:</td> <td><input type="text" name="financeContactEmail" value="<%= financeContactEmail %>"/></td> </tr> <tr> <td>Finance Contact Tel.:</td> <td><input type="text" name="financeContactTel" value="<%= financeContactTel %>"/></td> </tr> <tr> <td></td> <td align="right"><input type="submit" name="save" value="Save" /></td> <td align="right"><input type="submit" name="delete" value="Delete" /></td> </tr> </table> </form> <p><a href="managecustomers.jsp">Back...</a></p> </body> </html>



    This archive was generated by hypermail 2.0.0 : Fri Sep 18 2009 - 10:07:48 EDT