本文作者:蘇生米沿
本文地址:[http://blog.csdn.net/sushengmiyan/article/details/50414652](http://blog.csdn.net/sushengmiyan/article/details/50414652)
In this chapter, we focus on the most important part of your system: the databaseschema, where your collection of integrity rules resides—the model of the realworld that you’ve created.
If your application can auction an item only once in the?real world, your database schema should guarantee that. If an auction always has a?starting price, your database model should include an appropriate constraint.
If?data satisfies all integrity rules, the data is consistent, a term you’ll meet again in section11.1.
We also assume that consistent data is correct: everything the database states, either?explicitly or implicitly, is true; everything else is false.
If you want to know more aboutthe theory behind this approach, look up the closed-world assumption (CWA).
本章,我們主要集中在你系統中最重要的部分,數據庫schema,你的完整性規則駐留的地方---你創建的真實世界的模型。如果在真實世界中你的應用程序只能對一件物品競價一次,你的數據庫schema就應保證這點。如果一個競拍總有一個開始價格,你的數據庫模型應該包括一個適當的約束。如果數據滿足所有完整性約束,數據就是一致的,這個術語會在11.1章節再次遇到。
我們還假設一致的數據是正確的,數據庫所表述的所有數據,無論是顯式的還是隱式的,都是正確的,其它的所有都是錯誤的。如果你想知道這個理論背后的更多知識,請參考封閉世界假設說(CWA)。
Major new features in JPA 2
? Schema generation and execution of custom SQL scripts during bootstrap is?now standardized and can be configured on a persistence unit.
? You can map and customize schema artifacts such as indexes and foreign?key names with standard annotations.
? You can map foreign keys/many-to-one associations in composite primary?keys with @MapsId as “derived identity.”
JPA2的新特性
在啟動的時候生成schema和執行自定義的SQL腳本現在已經標準化了,在持久化單元中就可以進行配置了。
你可以映射和自定義schema物件,像索引和外鍵,使用標準的注解名稱。
你可以映射外鍵、多對一關系,通過使用@mapsid來定義復合主鍵這樣的衍生主鍵。
Sometimes you can start a project top-down. There is no existing database schema and?maybe not even any data—your application is completely new.?
Many developers like to?let Hibernate automatically generate the scripts for a database schema. You’ll probably
also let Hibernate deploy the schema on the test database on your development?machine or your continuous build systems for integration testing.?
Later, a DBA will?take the generated scripts and write an improved and final schema for production?deployment.?
The first part of this chapter shows you how to improve the schema from?within JPA and Hibernate, to make your DBA happy.
At the other end of the spectrum are systems with existing, possibly complex schemas,?with years’ worth of data. Your new application is just a small gear in a big?machine, and your DBA won’t allow any (sometimes even non-disruptive) changes to?the database.?
You need a flexible object/relational mapping so you don’t have to twist?and bend the Java classes too much when things don’t fit right away.?
This will be the?subject of the second half of this chapter, including a discussion of composite primary?and foreign keys.
有時候,你可以自頂向下的開始一個工程。你的項目工程是全新的,沒有現存的數據庫shema甚至沒有數據。很多開發者喜歡讓hibernate來產生數據庫的schema的腳本。你可能讓hibernate部署你的測試庫在你的開發機器上,或者你的集成測試中。稍晚些,DBA就會拿到生成的腳本,改善一下并且寫出改善之后的最終產品發布schema。本章的第一部分就給你展示如何提升hibernate和JPA生成的schema,來讓你的DBA高興。
在光譜的另一端就是集成可能是復雜的schema的具有多年數據價值的系統了。
你的新應用就是一個在大機器上的小齒輪。你的DBA不會允許你進行任何的數據庫改變(甚至包括非破壞性的)。你需要一個靈活的對象/關系映射,那樣的話,你就不需要為不合時宜的事情扭曲或者彎曲你的java實現類,這是本章第二部分的主題,包括討論一個復合主鍵和外鍵。
摘自 hibernate與JPA 第九章。轉載請標明來源,謝謝。