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 <[email protected]> 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 <[email protected]> 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: [email protected]
Web: http://www.mailarchiva.com
To receive MailArchiva Enterprise Edition product announcements, send a message to:
<[email protected]>
<?xml version="1.0" encoding="utf-8"?>
<domains project-version="3.0">
<domain name="Stimulus">
<map name="StimulusMap" location="StimulusMap.map.xml"/>
<node name="StimulusNode"
datasource="StimulusNode.driver.xml"
factory="org.apache.cayenne.conf.DriverDataSourceFactory">
<map-ref name="StimulusMap"/>
</node>
</domain>
</domains>
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/3.0/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/3.0/modelMap http://cayenne.apache.org/schema/3.0/modelMap"
project-version="3.0">
<property name="defaultPackage" value="com.stimulus.support"/>
<db-entity name="Customer">
<db-attribute name="comment" type="VARCHAR" length="200"/>
<db-attribute name="companyName" type="VARCHAR" length="200"/>
<db-attribute name="createDate" type="DATE"/>
<db-attribute name="financeContactEmail" type="VARCHAR" length="200"/>
<db-attribute name="financeContactName" type="VARCHAR" length="200"/>
<db-attribute name="financeContactTel" type="VARCHAR" length="200"/>
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
<db-attribute name="partnerId" type="INTEGER"/>
<db-attribute name="technicalContactEmail" type="VARCHAR" length="200"/>
<db-attribute name="technicalContactName" type="VARCHAR" length="200"/>
<db-attribute name="technicalContactTel" type="VARCHAR" length="200"/>
</db-entity>
<db-entity name="Partner">
<db-attribute name="commission" type="INTEGER"/>
<db-attribute name="companyName" type="VARCHAR" length="200"/>
<db-attribute name="contactEmail" type="VARCHAR" length="200"/>
<db-attribute name="contactName" type="VARCHAR" length="200"/>
<db-attribute name="contactTel" type="VARCHAR" length="200"/>
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
<db-attribute name="isEnterprise" type="BOOLEAN"/>
<db-attribute name="isISP" type="BOOLEAN"/>
<db-attribute name="password" type="VARCHAR" length="200"/>
</db-entity>
<db-entity name="Purchase">
<db-attribute name="commissionAmount" type="FLOAT"/>
<db-attribute name="customerId" type="INTEGER"/>
<db-attribute name="date" type="DATE"/>
<db-attribute name="description" type="VARCHAR" length="200"/>
<db-attribute name="expiryDate" type="DATE"/>
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
<db-attribute name="license" type="VARBINARY" length="6000"/>
<db-attribute name="productCode" type="INTEGER"/>
<db-attribute name="quantity" type="INTEGER"/>
<db-attribute name="totalAmount" type="FLOAT"/>
</db-entity>
<obj-entity name="Customer" className="com.stimulus.support.Customer" dbEntityName="Customer">
<obj-attribute name="comment" type="java.lang.String" db-attribute-path="comment"/>
<obj-attribute name="companyName" type="java.lang.String" db-attribute-path="companyName"/>
<obj-attribute name="createDate" type="java.util.Date" db-attribute-path="createDate"/>
<obj-attribute name="financeContactEmail" type="java.lang.String" db-attribute-path="financeContactEmail"/>
<obj-attribute name="financeContactName" type="java.lang.String" db-attribute-path="financeContactName"/>
<obj-attribute name="financeContactTel" type="java.lang.String" db-attribute-path="financeContactTel"/>
<obj-attribute name="technicalContactEmail" type="java.lang.String" db-attribute-path="technicalContactEmail"/>
<obj-attribute name="technicalContactName" type="java.lang.String" db-attribute-path="technicalContactName"/>
<obj-attribute name="technicalContactTel" type="java.lang.String" db-attribute-path="technicalContactTel"/>
</obj-entity>
<obj-entity name="Partner" className="com.stimulus.support.Partner" dbEntityName="Partner">
<obj-attribute name="commission" type="java.lang.Integer" db-attribute-path="commission"/>
<obj-attribute name="companyName" type="java.lang.String" db-attribute-path="companyName"/>
<obj-attribute name="contactEmail" type="java.lang.String" db-attribute-path="contactEmail"/>
<obj-attribute name="contactName" type="java.lang.String" db-attribute-path="contactName"/>
<obj-attribute name="contactTel" type="java.lang.String" db-attribute-path="contactTel"/>
<obj-attribute name="isEnterprise" type="java.lang.Boolean" db-attribute-path="isEnterprise"/>
<obj-attribute name="isISP" type="java.lang.Boolean" db-attribute-path="isISP"/>
<obj-attribute name="password" type="java.lang.String" db-attribute-path="password"/>
</obj-entity>
<obj-entity name="Purchase" className="com.stimulus.support.Purchase" dbEntityName="Purchase">
<obj-attribute name="commissionAmount" type="java.lang.Float" db-attribute-path="commissionAmount"/>
<obj-attribute name="date" type="java.util.Date" db-attribute-path="date"/>
<obj-attribute name="description" type="java.lang.String" db-attribute-path="description"/>
<obj-attribute name="expiryDate" type="java.util.Date" db-attribute-path="expiryDate"/>
<obj-attribute name="license" type="byte[]" db-attribute-path="license"/>
<obj-attribute name="productCode" type="java.lang.Integer" db-attribute-path="productCode"/>
<obj-attribute name="quantity" type="java.lang.Integer" db-attribute-path="quantity"/>
<obj-attribute name="totalAmount" type="java.lang.Float" db-attribute-path="totalAmount"/>
</obj-entity>
<db-relationship name="partner" source="Customer" target="Partner" toMany="false">
<db-attribute-pair source="partnerId" target="id"/>
</db-relationship>
<db-relationship name="purchases" source="Customer" target="Purchase" toMany="true">
<db-attribute-pair source="id" target="customerId"/>
</db-relationship>
<db-relationship name="customers" source="Partner" target="Customer" toMany="true">
<db-attribute-pair source="id" target="partnerId"/>
</db-relationship>
<db-relationship name="customer" source="Purchase" target="Customer" toMany="false">
<db-attribute-pair source="customerId" target="id"/>
</db-relationship>
<obj-relationship name="partner" source="Customer" target="Partner" deleteRule="Nullify" db-relationship-path="partner"/>
<obj-relationship name="purchases" source="Customer" target="Purchase" deleteRule="Deny" db-relationship-path="purchases"/>
<obj-relationship name="customers" source="Partner" target="Customer" deleteRule="Deny" db-relationship-path="customers"/>
<obj-relationship name="customer" source="Purchase" target="Customer" deleteRule="Nullify" db-relationship-path="customer"/>
</data-map>
<?xml version="1.0" encoding="utf-8"?>
<driver project-version="3.0" class="org.apache.derby.jdbc.EmbeddedDriver">
<url value="jdbc:derby:/home/jamie/business.db;create=true"/>
<connectionPool min="1" max="1"/>
<login/>
</driver>
<%@ 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>