tags:

views:

26

answers:

3

Dear All:

I am quite interested in a Hibernate mapping such as the Order/Product/LineItem described here: http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-mappings.html#example-mappings-customerorderproduct

The documentation seems quite thorough, but I am a bit unclear on the semantics of the Java classes that one would create...

Any hints much appreciated.

Thank you!

Misha

A: 

They show the UML and the class members in the diagram.

A Customer can have zero to many Order objects (Set).

An Order has at least one to many LineItem objects. {List).

A LineItem entry corresponds to a Product. So LineItem has exactly one Product object.

Not sure what your question is?

Sisyphus
I would just add that the relation between Customer and Order is bidirectional. But well, it's so much easier to use UML than text for that :)
Pascal Thivent
A: 

Using that example, you would have classes that looked like the following:

import java.util.HashSet;
import java.util.Set;

public class Customer {

    private String name = null;
    private Set<Order> orders = new HashSet<Order>();
    private long id = 0;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }
}

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Order {
    private long id = 0;
    private Date date = null;
    private Customer customer = null;
    private List<LineItem> lineItems = new ArrayList<LineItem>();

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }
}

public class LineItem {
    private int quantity = 0;
    private Product product = null;

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
}

public class Product {
    private long id = 0;
    private String serialNumber = null;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }
}

If you create the tables as per the structure in the example, that should set you up.

mlschechter
A: 

You can use EJB3 annotations coming from Hibernate directly with UML 2.3.

Java annotations for getters are automatically reversed if hand coded or added in the code if you select a mapping association.

I let you find out the name of the tool but it is pretty easy to recognize it :-)

alt text alt text

Wow that's pretty cool... looks somewhat like http://www.tutorial-omondo.com/jdo/classdiagram.html is that the right tool?
Misha Koshelev