home  |  suche  |  kontakt/johner  |  institut 
studierende  |  tech-docs  |  mindmailer 

Introduction

We want to create invoices that consist of line items. A line item itself is defined by the number of products, e.g. "2 PCs".

The line items should be added to the invoice as map, whereas the id of the product (field "productId") should be the key in this map and the line item itself the value.

JDO

<?xml version="1.0"?>
<!--<!DOCTYPE jdo SYSTEM jdo_2_0.dtd">-->
<!DOCTYPE jdo PUBLIC "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN" "http://java.sun.com/dtd/jdo_2_0.dtd">
<jdo>
  <package name="org.johner.jpoxtest">
    <class name="Invoice" identity-type="datastore">
      <field name="date" persistence-modifier="persistent">
        <column jdbc-type="DATE"/>
      </field>
      <field name="lineItems" persistence-modifier="persistent" mapped-by="invoice">
        <map key-type="Product" value-type="org.johner.jpoxtest.LineItem">
          <extension vendor-name="jpox" key="key-field" value="product"/>
        </map>
      </field>
    </class>
    <class name="LineItem" identity-type="datastore">
      <field name="quantity" persistence-modifier="persistent">
        <column name="quantity"/>
      </field>
      <field name="product" persistence-modifier="persistent"/>
      <field name="invoice" persistence-modifier="persistent"/>
    </class>
    <class name="Product" identity-type="datastore">
      <field name="productId" persistence-modifier="persistent">
        <column name="productid"/>
      </field>
      <field name="name" persistence-modifier="persistent">
        <column length="100" jdbc-type="VARCHAR"/>
      </field>
    </class>
  </package>

Class Invoice

Achtung: Dieser Code enthält noch Fehler!

     

  • Zeilen 17/18 müssten so geändert werden, dass das lineItem mit dem Produkt als Schlüssel hinzugefügt wird:

    public void addLineItem(LineItem lineItem) {
       Product product = lineItem.getProduct();
       lineItems.put(product, lineItem);
    }
  • In Zeilen 21/22 müsste nicht die Produkt-Id, sondern das Produkt übergeben werden:

    public LineItem getLineItem(Product product) { 
       return (LineItem) lineItems.get(product);
    }
01 package org.johner.jpoxtest;
02 
03 import java.util.Date;
04 import java.util.Hashtable;
05 
06 public class Invoice {
07   private Date date = null;
08   private Hashtable lineItems = new Hashtable();
09 
10   public Invoice(Date date) {
11     this.date = date;
12   }
13 
14   private Invoice() {}
15 
16   public void addLineItem(LineItem lineItem) {
17     Integer productId = lineItem.getProduct().getProductId();
18     lineItems.put(productId, lineItem);
19   }
20 
21   public LineItem getLineItem(Integer productId) {
22     return (LineItemlineItems.get(productId);
23   }
24 
25   public Date getDate() {
26     return date;
27   }
28 
29   public void setDate(Date date) {
30     this.date = date;
31   }
32 }

Class LineItem

01 package org.johner.jpoxtest;
02 
03 public class LineItem {
04   private int quantity;
05   private Product product = null;
06   private Invoice invoice = null;
07 
08   public LineItem(Invoice invoice, int quantity, Product product) {
09     this.invoice = invoice;
10     this.quantity = quantity;
11     this.product = product;
12 
13   }
14 
15   private LineItem() { }
16 
17   public int getQuantity() {
18     return quantity;
19   }
20 
21   public void setQuantity(int quantity) {
22     this.quantity = quantity;
23   }
24 
25   public Product getProduct() {
26     return product;
27   }
28 
29   public void setProduct(Product product) {
30     this.product = product;
31   }
32 
33   public Invoice getInvoice() {
34     return invoice;
35   }
36 
37   public void setInvoice(Invoice invoice) {
38     this.invoice = invoice;
39   }
40 }

Class Product

01 package org.johner.jpoxtest;
02 
03 public class Product {
04   private Integer productId;
05   private String name = null;
06 
07   public Product(Integer productId, String name) {
08     this.productId = productId;
09     this.name = name;
10   }
11 
12   private Product() { }
13 
14   public Integer getProductId() {
15     return productId;
16   }
17 
18   public void setProductId(Integer id) {
19     this.productId = id;
20   }
21 
22   public String getName() {
23     return name;
24   }
25 
26   public void setName(String name) {
27     this.name = name;
28   }
29 }

Class MainTest

01 package org.johner.jpoxtest;
02 
03 import java.io.IOException;
04 import java.util.Date;
05 
06 import javax.jdo.PersistenceManager;
07 import javax.jdo.Transaction; 
08 
09 public class MainTest {
10 
11   public static void main(String[] argsthrows IOException {
12     PersistenceManager pm = PM.createPersistenceManager();
13     Transaction tx = pm.currentTransaction();
14     try {
15       tx.begin();
16 
17       // 1. create products
18       Product computer = new Product(new Integer(123)"PC");
19       Product monitor = new Product(new Integer(42)"Monitor");
20       pm.makePersistent(monitor);
21       pm.makePersistent(computer);
22 
23       // 2. Create invoice and add line items (1 computere and 2 monitors)
24       Invoice invoice = new Invoice(new Date());
25       pm.makePersistent(invoice)
26       LineItem line1 = new LineItem(invoice, 1, computer);
27       LineItem line2 = new LineItem(invoice, 2, monitor);
28       invoice.addLineItem(line1);// <== here it crashes
29       invoice.addLineItem(line2);
30 
31       pm.makePersistent(invoice);
32       pm.makePersistent(line1);
33       pm.makePersistent(line2);
34 
35       tx.commit();
36     finally {
37       if (tx.isActive()) {
38         tx.rollback();
39       }
40     }
41   }
42 }

Persistence manager

01 package org.johner.jpoxtest;
02 
03 import java.io.IOException;
04 import java.util.Properties;
05 
06 import javax.jdo.JDOHelper;
07 import javax.jdo.PersistenceManager;
08 import javax.jdo.PersistenceManagerFactory;
09 
10 public class PM {
11   public static PersistenceManager createPersistenceManager() throws IOException
12   {
13     Properties properties = new Properties();
14     properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",  
15         "org.jpox.PersistenceManagerFactoryImpl");
16     properties.setProperty("javax.jdo.option.ConnectionDriverName"
17         "com.mysql.jdbc.Driver");
18     properties.setProperty("javax.jdo.option.ConnectionURL",
19         "jdbc:mysql://localhost/jdotest");
20     properties.setProperty("javax.jdo.option.ConnectionUserName","root");
21     properties.setProperty("javax.jdo.option.ConnectionPassword","");
22     properties.setProperty("org.jpox.autoCreateSchema","true");
23     properties.setProperty("org.jpox.validateTables","false");
24     properties.setProperty("org.jpox.validateConstraints","false");        
25     PersistenceManagerFactory pmfactory = 
26       JDOHelper.getPersistenceManagerFactory(properties);
27     PersistenceManager pm = pmfactory.getPersistenceManager();
28     
29     return pm;
30   }  
31 }