JavaPersistenceWithHibernate读书笔记(2)
关键字: 1.2 the paradigm mismatch:granularityJavaPersistenceWithHibernate读书笔记(2)
1.2 The paradigm mismatch
can be broken into several parts-->we will examine one at a time-->start with a example that is problem free --> mismatch appear.
User: represent information about a user of the system.
BillingDetails: represent information about the user's billing details.
public class User {
private String username;
private String name;
private String address;
private Set billingDetails;
// Accessor methods (getter/setter), business methods, etc.
...
}
public class BillingDetails {
private String accountNumber;
private String accountName;
private String accountType;
private User user;
// Accessor methods (getter/setter), business methods, etc.
...
}
with the following SQL schema:
create table USERS (
USERNAME varchar(15) not null primary key,
NAME varchar(50) not null,
ADDRESS varchar(100)
)
create table BILLING_DETAILS (
ACCOUNT_NUMBER varchar(10) not null primary key,
ACCOUNT_NAME varchar(50) not null,
ACCOUNT_TYPE varchar(2) not null,
USERNAME varchar(15) foreign key references user
)
relationship between the two entities is represented as the foreign key, USERNAME,in BILLING_DETAILS.
now it's straightforward to write JDBC code to insert,update,and delete information about users billing details.
make paradigm mismatch visible --> since address as a simple String,other classes will also carry address info -->create a separate Address class.
Should we also add an ADDRESS table?Not necessarily.It's common to keep address info in the USERS table(基于什么考虑要把这个ADDRESS也放进Users表里?a table join isnt needed),in individual columns.
Basically, we have the choice of adding either several columns or a single column (of a new SQL datatype). This is clearly a problem of granularity.
1.2.1 The problem of granularity
Granularity refers to the relative size of the types you’re working with.
For these and whatever other reasons, use of UDTs or Java types inside an SQL database isn’t common practice in the industry at this time.
Our pragmatic solution for this problem has several columns of built-in vendor-defined SQL types (such as boolean, numeric, and string datatypes). TheUSERS table is usually defined as follows:
create table USERS (
USERNAME varchar(15) not null primary key,
NAME varchar(50) not null,
ADDRESS_STREET varchar(50),
ADDRESS_CITY varchar(15),
ADDRESS_STATE varchar(15),
ADDRESS_ZIPCODE varchar(5),
ADDRESS_COUNTRY varchar(15)
)
Classes in our domain model come in a range of different levels of granularity-- from coarse-grained entity classes like User, to finer-grained classes like Address, down to simple String-valued properties such as zipcode. In contrast, just two levels of granularity are visible at the level of the SQL database: tables such as USERS, and columns such as ADDRESS_ZIPCODE.
1.2.2 The problem of subtypes
- 19:24
- 浏览 (200)
- 评论 (0)
- 分类: 我的读书笔记
- 进入论坛
- 发布在 Database圈子 圈子
- 相关推荐
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 29420 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
链接
最新评论
-
对浏览器中的context menu ...
Drag dropper
-- by whqida -
Spring中的load-time weav ...
没啥,有些class不在spring的管理之中,通过 load-time wea ...
-- by ray_linn -
对浏览器中的context menu ...
别搞笑了, 这是浏览器的 Context Menu? 这只不过是响应了鼠标右击事 ...
-- by fcoffee -
IE tab在Firefox里开发时 ...
firebug只能用于ff, 并不能用于ff下的ietab, 原因显而易见.如果 ...
-- by fcoffee -
IE tab在Firefox里开发时 ...
还网告知 该如何操作呢 我还是要到ie下再调试啊
-- by jianfeng008cn






评论排行榜