将关系数据库表关联到 Java 类的最佳方法是什么?

发布于 2024-07-17 06:16:27 字数 2326 浏览 7 评论 0 原文

我想知道是否有人可以在这里给我建议; 我有一个具有如下类的应用程序:

代码:

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. 
}

这直接转换为关系表:

订单:orderno,customerNo,orderDate OrderLine: orderno, lineNo, qty, prodID

因此每个类直接转换为数据库表,每个属性都有 get 和 set 对。

现在我想知道的是,如果在 Java Web 应用程序中,这些类应该是上面的类还是更像这样,其中获取返回对象:

代码:

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; 
    } 
}

哪种方法更好? 或者只要处理数据的类正确并且系统高效运行,采用哪种方法真的很重要吗?

谢谢

摩根先生

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦魇绽荼蘼 2024-07-24 06:16:27

绝对没有理由使用第一种形式——在 Java 代码中使用它会更加困难,并且会产生很多错误。

所有不错的 O/R 映射器(Hibernate 是最流行的)都会毫无问题地从 DB 模式转换为正确的 OO 对象图。

然而,缺点是,为您完成所有这些可能会导致 当您将所有内容留给 OR 映射器并且它从数据库获取太多或太少的数据时,性能非常糟糕,因此您必须注意缓存以及您的集合是否< a href="http://powerdream5.wordpress.com/2007/11/09/eager-loading-in-hibernate/" rel="nofollow noreferrer">延迟加载或急切加载

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.

得不到的就毁灭 2024-07-24 06:16:27

如果您的业务对象像这样干净,我建议您考虑一下 ORM 映射器,例如 Hibernate 或 ActiveObjects。

如果您宁愿构建自己的数据访问层,我建议使这些类尽可能轻量。 例如,在 Order 中,我将有一个 CustomerID 整数字段来表示外键,而不是让 getCustomer() 返回 < Order 业务类中的 code>Customer 对象。

诸如 getCustomer() 之类的获取方法可能最好放置在不同的类中,例如包含数据访问功能的 CustomerDAO 等。

从效率的角度来看,采用哪种方法并不重要,但拥有松散耦合的对象始终是一个好习惯。

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.

情释 2024-07-24 06:16:27

您应该查看一些对象关系映射(ORM)映射工具,例如 HibernateTopLink,或者可能是更简单的查询映射工具,例如 iBatis

我认为如果您走 ORM 路线,您最终会得到更像您的第二个建议的东西。

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.

两个我 2024-07-24 06:16:27

常见的方法是使用对象关系映射库 (ORM)。 常见的是:

所有这些都是开源库并实现了 JPA ( Java 持久性架构)标准。

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.

雾里花 2024-07-24 06:16:27

我还鼓励您查看像 Hibernate 这样的对象关系映射工具,或者像 iBatis 这样更简单的 SQL 映射工具。 (我在 iBatis 方面拥有非常好的经验。)

关于对 Order 及其 OrderLine 之间的主从关系进行建模的问题,在 Java 对象级别,它们当然是同一概念层次结构的一部分,因此我会选择建模它们作为包含列表的 Order 对象。 这样我就可以在我的代码中传递完整的订单,而不必担心丢失其中的一部分。

如果您使用像 iBatis 这样的 SQL 映射器,您需要在数据库中设置一个 Order 表和一个 OrderLine 表,并为 iBatis 提供一个 XML 映射,告诉它如何通过连接来组装复合 Java Order/OrderLine 对象层次结构。两个 SQL 表在一起。 ORM 反向操作,根据 Java 数据模型生成 SQL 表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文