简体   繁体   中英

What is the best way of relating relational database tables into Java classes?

I wonder if anyone can advise me here; I have an application which has classes like this:

Code:

public class Order implements Serializable { 
    private int orderNo; 
    private int customerNo;    
    private date orderDate; 

    public int getOrderNo () { 
        return orderNo; 
    } 

    public int getCustomerNo () { 
        return customerNo; 
    } 

    public date getOrderDate () { 
        return orderDate; 
    } 

    // And so on with set methods. 

} 

public class OrderLine implements Serializable { 
    private int orderNo; 
    private int lineNo;    
    private int qty; 
    private int prodID; 

    // Get and set methods for each of the above. 
    public int getOrderNo () { 
        return orderNo; 
    } 

    public int getLineNo () { 
        return lineNo; 
    } 

    public int getQty () { 
        return qty; 
    } 

    public int prodID () { 
        return prodID; 
    } 
    // And so on with set methods. 
}

This translates directly into relational table:

Order: orderno, customerNo, orderDate OrderLine: orderno, lineNo, qty, prodID

So each class directly translates into a database table with get and set pairs for each attribute.

Now what I want to know is, if in an Java web application, should the classes be as they are above or more like this where the gets return objects:

Code:

public class Order implements Serializable { 
    private int orderNo; 
    private Customer;    
    private date orderDate; 
    private ArrayList<OrderLine>lineItems; 

    public int getOrderNo () { 
        return orderNo; 
    } 

    public Customer getCustomer () { 
        return Customer; 
    } 

    public date getOrderDate () { 
        return orderDate; 
    } 

    public ArrayList<OrderLine> getOrderLines () { 
        return lineItems; 
    } 

    public OrderLine[] getOrderLines () { 
        return lineItems; 
    } 
    // And so on with set methods.     
} 

public class OrderLine implements Serializable { 
    private int orderNo; 
    private int lineNo;    
    private int qty; 
    private Product; 

    // Get and set methods for each of the above. 

    public int getOrderNo () { 
        return orderNo; 
    } 

    public int getLineNo () { 
        return lineNo; 
    } 

    public int getQty () { 
        return qty; 
    } 

    public int getProduct () { 
        return Product; 
    } 
}

Which is the better approach? Or does it really matter which approach is taken as long as the classes processing the data do so correctly and the system operates efficiently?

Thanks

Mr Morgan

There is absolutely no reason to use the first form - it will be much harder to work with in the Java code, and there will be a lot of bugs resulting from that.

All decent O/R mappers (Hibernate being the most popular one) will translate from the DB schema into proper OO object graphs with no problems.

However, the downside is that having all this done for you can result in very bad performance when you leave everything to the OR mapper and it fetches too much or too little data from the DB, so you have to pay some attention to caching and whether your collections are lazy or eager loading .

If your business objects are as clean as this, I'd suggest maybe looking at an ORM mapper such as Hibernate or ActiveObjects.

If you'd rather build your own data access layer, I'd suggest making these classes as light-weight as possible. For instance in Order , I would have a CustomerID integer field which would represent the foreign key, instead of having a getCustomer() returning a Customer object in the Order business class.

Fetch methods such as getCustomer() would probably be better placed in a different class such as CustomerDAO etc which contains your data access functionality.

It won't really matter from an efficiency point of view which approach you go with, but it's always good practice to have loosely-coupled objects.

You should look at some Object Relational Mapping (ORM) mapping tool, like Hibernate or TopLink , or perhaps a simpler query mapping tool like iBatis .

I think you'd end up with something more like your second suggestion if you go down the ORM route.

The common way to do it is by using an Object-Relational Mapping library (ORM). The common ones are:

All these are open source libraries and implement the JPA (Java Persistence Architecture) standard.

I would also encourage you to look at an Object-Relational Mapping tool like Hibernate, or a simpler SQL mapping tool like iBatis. (I've had very good experience with iBatis.)

On the question of modelling the master-detail relationship between an Order and its OrderLines, at the Java object level they certainly are part of the same conceptual hierarchy, so I would choose to model them as an Order object that contains a List. That way I could pass complete Orders around my code and never worry about losing parts of them.

If you're using an SQL mapper like iBatis, you'd set up an Order table and an OrderLine table in your database, and give iBatis an XML map that tells it how to assemble your compound Java Order/OrderLine object hierarchy by joining the two SQL tables together. ORMs operate in reverse, generating the SQL tables based on your Java data model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM