1. Update make a detached object persistent.
2. Config mapping relating in mapping files make query as object oriented.
3. For many-to-many relation relationship, middle table store link to two tables.
4. Key element is identify the column of middle table.
5. Db operation happend at the end of a unit work ---- session flush.
6. For value type, "element" will replace "many-to-many" in set element(acturally, for all java object, hibernate take it as a value type), value type can not shared by more than one instance.
7. All bi-directional associations need one side as inverse. bi-direction means both sides of mapping mentain relations of middle table, inverse side is a mirror of the other side, during save or update, the other side will handle.
1. batch insert/update session.flush session.clear
2. statlesssession
There are three main kinds of mapping for inheritance:
1. table per class hirachy, for each subclass, use discriminator to distinguish different data, subclass specific property can not be null, only one table.
2. table per subclass, there is a common table for super class, subclass table reference to super table through primary key, add relationship between tables.
3. table per concrete class, generate too much redandance data, pk was generated by super class.
some mix mapping is available.
1. Entity name: can be used query.
2. Way of define mapping: xml, xdoclet, annotation.
3. Gerated properties: timestamp, version, and property can be generated. Types as follows :
never: default value.
always: generated when insert and update.
insert: only geneated when create data, won't change when updated it later.
4. Create db schema can be written in xml file.
1. lite archetecture: application handle connection and transaction, use niminum hibernate api.
2. full scream architecture: application will ingore underlying jdbc, jta apis, hibernate hold it.
3. Persistent object stats: persistence, transient and detached.
4. Session factory: a cache of compiled configuration files for single db, thread-safed, factory of session and client of connectionProvider, an optional cache of data for process, cluster layer.
5. Session: represent a conversation between application and db, factory of transaction. wrapped conncetion.
6. Transatcion: specify automic unit of work.
7. Context session: hibernateUtil class, for certain application, the scope of current is different, have different context concept. (need more research!)
8. Configration: store xml to java, can got it through specify xml, java class and proeprties.
9. Hibernate mapping: mapping java type to SQL type.
10. Custome mapping type: implement UserType or CompsiteUserType, can define short name, chould get certain value of property by key value.(useless)
11. Entity and Value: entity can be shared by more than one persistent class as reference.
Singleton模式可能是應(yīng)用最廣泛的模式之一了, 但有些錯(cuò)誤的應(yīng)用。
?Singleton的實(shí)現(xiàn): 有兩種方式, 如下:
1. class Test {
public static final Test instance = new Test();
private Test() {}
}
?2. class Test {
private static final Test instance = new Test();
private Test() {}
public static Test getInstance() {
return instance;
}
}
這兩種方法都要求構(gòu)造器是私有的, 這樣就可以防止該類外的對象創(chuàng)建新的TEST對象。
但相對而言, 推薦使用第二種方法, 因?yàn)槠涓哂徐`活性,當(dāng)我們改變創(chuàng)建對象的方式的時(shí)候, 不需要改動客戶代碼。 第一種方法較第二種有一點(diǎn)完全可以忽略不計(jì)的效率的提高。
?但應(yīng)避免如下代碼實(shí)現(xiàn)Singleton:
class Test {
private static Test singleton = null;
private Test() {}
public Test getSingleton() {
if(singleton == null) {
singleton = new Test();
}
return singleton;
}
}
因?yàn)閲?yán)格上講, 這并不能完全實(shí)現(xiàn)Singleton模式,而且會導(dǎo)致程序出錯(cuò), 這同著名的線程問題--DCL實(shí)效的原理是完全一樣的:
JVM創(chuàng)建對象的過程可以分為幾個(gè)步驟:創(chuàng)建空間, 把所有的變量賦值位默認(rèn)值, 初始化。。。 當(dāng)有兩個(gè)線程A和B同事進(jìn)入該方法, A先執(zhí)行, A創(chuàng)建Test實(shí)例的空間, 這時(shí),因?yàn)镃PU的指令流機(jī)制,時(shí)間片段正好輪到B線程, 這時(shí)B判斷singleton是否為NULL, 因?yàn)锳已經(jīng)為Test的實(shí)例分配了空間, 所以JVM認(rèn)為實(shí)例已經(jīng)創(chuàng)建了, B繼續(xù)執(zhí)行, 更糟糕的是B調(diào)用了singleton, 這時(shí)因?yàn)樗]有初始化完全, 所以拋出NullPointerException, 太糟糕了!
Bridge:主要實(shí)現(xiàn)的原理就是把接口 和實(shí)現(xiàn)分離開來, 保證他們再兩個(gè)不同的類層次結(jié)構(gòu)。
用Bridge而不是直接繼承實(shí)現(xiàn)主要有兩個(gè)好處:
1。 二進(jìn)制兼容。 假設(shè)我們的應(yīng)用程序需要用到format功能, 我們可能有要引用兩個(gè)第三方JAR包, formatInterface.JAR And formatImp.jar, 我們程序可能只引用了formatInterface.jar中的接口, 而formatImpl.jar里是什么我們根本不需要關(guān)心, 因?yàn)樗莊ormatInterface的實(shí)現(xiàn), 所以當(dāng)他改變的時(shí)候, 我們的應(yīng)用程序完全不用重新修改代碼, 編譯。可能我在LINUX下用LINUXFormatImpl.jar, 再WINDOW下use WindowFormatImpl.jar, but Application will never care about it.
?2. 接口與實(shí)現(xiàn)的分離, 實(shí)現(xiàn)不一定實(shí)現(xiàn)接口的內(nèi)容, 就是說實(shí)現(xiàn)同接口之間不是一一對應(yīng)的, 實(shí)現(xiàn)可能完成最原子的操作, 而接口通過持有一個(gè)實(shí)現(xiàn)的應(yīng)用, 組裝這些操作來實(shí)現(xiàn)接口。 比如說接口是createRectangle(), 實(shí)現(xiàn)可能只完成了createLine的操作, 然后有接口來組裝。
?Composite模式則要從全局的角度考慮對象之間的關(guān)系是否滿足“樹枝” 與 “樹葉”的關(guān)系, 如果滿足, 則需要定義一個(gè)樹枝與樹葉的集合接口Tree, 既包含樹枝接口add(tree)和樹葉接口getColor()。
每個(gè)JAVA對象都有一把所, 當(dāng)有多個(gè)線程同時(shí)訪問共享資源的時(shí)候, 需要Synchronize 來控制安全性, synchronize 分 synchronize 方法 和synchronize快,使用synchronize塊時(shí), 一定要顯示的獲得該對象的鎖(如synchronize(object))而方法則不需要。
?JAVA 的內(nèi)存模型是對每一個(gè)進(jìn)程有一個(gè)主內(nèi)存, 每個(gè)線程有自己的內(nèi)存, 他們從主內(nèi)存中取數(shù)據(jù), 然后計(jì)算, 再存入主內(nèi)存中。
?并發(fā)問題如下:如果多個(gè)線程同事操作同一數(shù)據(jù), A線程從主內(nèi)存中取的I的值為1, 然后進(jìn)行加1操作, 這時(shí)B線程也取I的值, 進(jìn)行加2操作, 然后A存入2到主內(nèi)存中, B也存入, 這樣就覆蓋了A的值(同數(shù)據(jù)庫中的并發(fā)問題一樣)。
解決辦法是用synchronize, 如用synchronized(I)。被synchronize 修飾的方法(塊)把以下三步操作當(dāng)成一個(gè)原子操作:取數(shù)據(jù), 操作數(shù)據(jù), 存數(shù)據(jù)。 我們知道原子操作是不可以被打斷的, 所以其保證了數(shù)據(jù)一致性, 這樣同一時(shí)間只有一個(gè)線程再執(zhí)行, 對性能有一定的影響。這也是synchronize的第二個(gè)作用:保證統(tǒng)一時(shí)間只有一個(gè)線程再運(yùn)行。 當(dāng)實(shí)現(xiàn)SOCKET連接的時(shí)候經(jīng)常用到.
?JAVA中規(guī)定對非FLOAT, LONG的原始類型的取和存操作為原子操作。 其實(shí)就是對一個(gè)字(32位)的取,存位原始操作, 因?yàn)镕LOAT, LONG為兩個(gè)字節(jié)的長度, 所以其取, 存為非原子操作。 如果想把他們也變?yōu)樵硬僮鳎?可以用VOLATILE關(guān)鍵字來修飾。
1. serializable default serialize all field and object net.
2. Externalizable default deserialize all fields and use writeExternal and readExternal to serialize object. should provide public construct.
3. serializable interface use private writeObject and private readOjbect to implemnets the same fucntion as Extenalizable interface. (writeDefautObject and readDefaultObject).
4. Externalizable extends serializable
學(xué)習(xí)java也有幾年的時(shí)間了, 大學(xué)開始就一直沒有斷過, 工作后有專門學(xué)習(xí)了java。
由于對原理型的知識比較看重, 所以JAVA基礎(chǔ)學(xué)的比較多, 至今精通不敢說, 但至少也應(yīng)該算是熟悉了吧!
我認(rèn)為語言的學(xué)習(xí)最簡單的就是文法學(xué)習(xí), 估計(jì)一般學(xué)習(xí)一周 到兩周就可以使用這種語言編寫程序, 對于初級程序員這樣也就足夠了, 但要對語言有很深的了解:就是對這個(gè)語言的特性的了解, 把JAVA做例子, 他的所有類繼承自O(shè)BJECT(他的所有方法用途, 作用等等), CLONABLE接口, SERIALIZABEL接口改變了方法的行為,synchronized等等, 這些都是JAVA語言定義的自己的特征, 包括JAVA的性能, 要能高效率的運(yùn)用JAVA, 必須對對這些特性有比較深的了解, 還要對其主要的庫有一定的了解,比如說容器庫, 輸入輸出流。。如果了解這些的話最少也要花費(fèi)1--2年的學(xué)習(xí), 還不一定掌握精髓。 估計(jì)掌握這些知識的人怎么也應(yīng)該是高級程序員了。 JAVA學(xué)習(xí)的最高境界應(yīng)該是對其類庫的無比熟悉, 能清楚其內(nèi)在的實(shí)現(xiàn), 如容器類散列桶的實(shí)現(xiàn)方法等等, 以及他們實(shí)現(xiàn)的優(yōu)劣。 能達(dá)到這個(gè)程度的人應(yīng)該寥寥無幾, 這樣的人一般是有10年JAVA工作經(jīng)驗(yàn)的資深JAVA程序員了。
個(gè)人認(rèn)為學(xué)習(xí)語言也不過這三個(gè)階段:熟悉語法, 熟悉語言的特性, 精通類庫。一個(gè)語言成熟與否的標(biāo)志就是他是否有一個(gè)設(shè)計(jì)良好的全面的類庫。