开发者

hibernate many-to-many table mapping with extra fields as a list - Java classes?

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 Jav开发者_JAVA百科a classes that one would create...

Any hints much appreciated.

Thank you!

Misha


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜