| Yun's profile无尽的探索PhotosBlogLists | Help |
|
July 21 UserType for persisting a Typesafe Enumeration with a VARCHAR column [转]Use this Typesafe Enumeration class in your domain model as a simple property type (e.g. of the Comment class in a forum): public class Rating implements Serializable {
private String name;
public static final Rating EXCELLENT = new Rating("Excellent");
public static final Rating OK = new Rating("OK");
public static final Rating LOW = new Rating("Low");
private static final Map INSTANCES = new HashMap();
static {
INSTANCES.put(EXCELLENT.toString(), EXCELLENT);
INSTANCES.put(OK.toString(), OK);
INSTANCES.put(LOW.toString(), LOW);
}
private Rating(String name) {
this.name=name;
}
public String toString() {
return name;
}
private Object readResolve() {
return getInstance(name);
}
public static Rating getInstance(String name) {
return (Rating)INSTANCES.get(name);
}
}
Next, write the Hibernate custom mapping type: public class RatingUserType implements UserType {
private static final int[] SQL_TYPES = {Types.VARCHAR};
public int[] sqlTypes() { return SQL_TYPES; }
public Class returnedClass() { return Rating.class; }
public boolean equals(Object x, Object y) { return x == y; }
public Object deepCopy(Object value) { return value; }
public boolean isMutable() { return false; }
public Object nullSafeGet(ResultSet resultSet,
String[] names,
Object owner)
throws HibernateException, SQLException {
String name = resultSet.getString(names[0]);
return resultSet.wasNull() ? null : Rating.getInstance(name);
}
public void nullSafeSet(PreparedStatement statement,
Object value,
int index)
throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, Types.VARCHAR);
} else {
statement.setString(index, value.toString());
}
}
}
Finally, in your mapping files, bind it all together: <hibernate-mapping package="org.hibernate.auction.model">
<class name="Comment"
table="COMMENTS"
lazy="true">
<!-- Common id property. -->
<id name="id"
type="long"
column="COMMENT_ID"
unsaved-value="null">
<generator class="native"/>
</id>
<!-- Simple property. -->
<property
name="rating"
column="RATING"
type="org.hibernate.auction.persistence.RatingUserType"
not-null="true"
update="false"/>
...
</class>
</hibernate-mapping>
You may also use your Rating enumerated type in Hibernate queries: Query q = session.createQuery("from Comment c where c.rating = :rating");
q.setParameter("rating",
Rating.LOW,
Hibernate.custom(RatingUserType.class));
This is all there is to know about it. You may enhance this strategy with a generic UserType implementation, that knows how to persist different Typesafe Enumerations, this is described here. The examples and domain model you have just seen can be found in Hibernate in Action. Christian Reference: 1. http://www.hibernate.org/172.html 枚举类型的映射和表示 [转]在项目中常遇到用数字表示一种意思的字段,例如:type ,1 表示正常状态,0表示冻结状态;这是一类典型的问题,即枚举类型的映射和表示。在Hibernate2.1版本中提供了enum的接口类型来处理这类问题,参考这个讨论: http://forum.javaeye.com/viewtopic.php?t=8114&postdays=0&postorder=asc&highlight=enum&start=0 但是在Hibernate3中取消了enum类型的支持,Hibernate官方推荐使用UserType接口。不过使用UserType接口实际上会比较复杂,下面我将使用一个简单的案例来讲解使用UserType接口来解决这类问题的办法:
package com.javaeye.simple.domain; import java.io.Serializable; public class UserTitle implements Serializable { private int title; public static final UserTitle EMPLOYEE = new UserTitle(0); public static final UserTitle MANAGER = new UserTitle(1); public static final UserTitle DIRECTOR = new UserTitle(2); public static final UserTitle CEO = new UserTitle(3); private UserTitle(int title) { public int toInt() { public static UserTitle fromInt(int title) { public String toString() { } 注意,我们需要一个UserTitleType的类,实现UserType接口,定义一个User对象的UserTitle属性是如何向数据库持久化的: package com.javaeye.simple.domain; import java.io.Serializable; import org.hibernate.HibernateException; public class UserTitleType implements UserType { public int[] sqlTypes() { public Class returnedClass() { public boolean equals(Object x, Object y) throws HibernateException { public int hashCode(Object x) throws HibernateException { public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { public Object deepCopy(Object value) throws HibernateException { public boolean isMutable() { public Serializable disassemble(Object value) throws HibernateException { public Object assemble(Serializable cached, Object owner) throws HibernateException { public Object replace(Object original, Object target, Object owner) throws HibernateException { } 注意nullSafeGet和nullSafeSet方法的代码,是如何实现UserTitle的字符串值和数据库中保存数字进行转换的。 <?xml version="1.0"?> <hibernate-mapping package="com.javaeye.simple.domain"> <class name="User"> 指明title属性是用户自定义的某类型。 在Java程序中指定user的title属性:
July 18 深树1. deeptree.jsp中定义css为deeptree.css,并定义<div id="deeptree" class="deeptree" CfgXMLSrc="deeptreeconfig.xml"> 参考:1. Deeptree下载 July 17 Keycode对照表
应用:1.限制输入数字 <input style="ime-mode : disabled" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"> 参考:1. Keycode对照表 July 09 SSL Configuration HOW-TO
Reference: July 04 eclipse内置resin的方法此配置适用于Resin2.x和Resin3.x版本。运行时需要在每个project目录下放置一个resin.conf文件,由于Resin2.x和Resin3.x版本的配置文件不同,所以需要两个文件,resin.conf文件用于启动resin2.x,resin30.conf用于启动resin3.x。 以下说明以配置Resin2.1.16版本为例,具体步骤如下: May 29 修改Oracle9i中XDB的http和ftp服务端口安装Oracle 9i,其安装缺省包含了XDB。在启动数据库后,Oracle XDB的http服务将会自动占用了8080端口,使用下面方法修改XDB的http和ftp服务端口: 1.使用dbms_xdb包修改端口设置 使用sys登录sqlplus sqlplus sys/syspassword as sysdba 执行如下的脚本: ============================ -- Change the HTTP/WEBDAV port from 8080 to 8081 call dbms_xdb.cfg_update(updateXML( dbms_xdb.cfg_get() , '/xdbconfig/sysconfig/protocolconfig/httpconfig/http-port/text()' , 8081)) / -- Change the FTP port from 2100 to 2111 call dbms_xdb.cfg_update(updateXML( dbms_xdb.cfg_get() , '/xdbconfig/sysconfig/protocolconfig/ftpconfig/ftp-port/text()' , 2111)) / COMMIT; EXEC dbms_xdb.cfg_refresh; 参考 November 20 Hierarchical QueriesCONNECT BY Example The following hierarchical query uses the SELECT employee_id, last_name, manager_id FROM employees CONNECT BY PRIOR employee_id = manager_id;
START WITH Examples The next example adds a SELECT last_name, employee_id, manager_id, LEVEL
FROM employees
START WITH employee_id = 100
CONNECT BY PRIOR employee_id = manager_id
ORDER SIBLINGS BY last_name;August 23 MelodiesRETURN TO SOLITUDE Robert Bly It is a moonlit, windy night. We want to go back, to return to the sea, What shall we find when we return? from Silence in the Snowy Fields (1962)
One Fine Spring Day MV [Main Theme] August 22 The Isle - Percy Bysshe ShelleyThere was a little lawny islet
小岛 有一座小岛,细草如茵, Alice in WonderlandChapter I - Down the Rabbit-Hole
So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her. There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge. In another moment down went Alice after it, never once considering how in the world she was to get out again. The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well. Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled 'ORANGE MARMALADE', but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody, so managed to put it into one of the cupboards as she fell past it. 'Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!' (Which was very likely true.) Down, down, down. Would the fall never come to an end! 'I wonder how many miles I've fallen by this time?' she said aloud. 'I must be getting somewhere near the centre of the earth. Let me see: that would be four thousand miles down, I think--' (for, you see, Alice had learnt several things of this sort in her lessons in the schoolroom, and though this was not a very good opportunity for showing off her knowledge, as there was no one to listen to her, still it was good practice to say it over) '--yes, that's about the right distance -- but then I wonder what Latitude or Longitude I've got to?' (Alice had no idea what Latitude was, or Longitude either, but thought they were nice grand words to say.) Presently she began again. 'I wonder if I shall fall right through the earth! How funny it'll seem to come out among the people that walk with their heads downward! The Antipathies, I think--' (she was rather glad there was no one listening, this time, as it didn't sound at all the right word) '--but I shall have to ask them what the name of the country is, you know. Please, Ma'am, is this New Zealand or Australia?' (and she tried to curtsey as she spoke -- fancy curtseying as you're falling through the air! Do you think you could manage it?) 'And what an ignorant little girl she'll think me for asking! No, it'll never do to ask: perhaps I shall see it written up somewhere.' Down, down, down. There was nothing else to do, so Alice soon began talking again. 'Dinah'll miss me very much to-night, I should think!' (Dinah was the cat.) 'I hope they'll remember her saucer of milk at tea-time. Dinah my dear! I wish you were down here with me! There are no mice in the air, I'm afraid, but you might catch a bat, and that's very like a mouse, you know. But do cats eat bats, I wonder?' And here Alice began to get rather sleepy, and went on saying to herself, in a dreamy sort of way, 'Do cats eat bats? Do cats eat bats?' and sometimes, 'Do bats eat cats?' for, you see, as she couldn't answer either question, it didn't much matter which way she put it. She felt that she was dozing off, and had just begun to dream that she was walking hand in hand with Dinah, and saying to her very earnestly, 'Now, Dinah, tell me the truth: did you ever eat a bat?' when suddenly, thump! thump! down she came upon a heap of sticks and dry leaves, and the fall was over. Alice was not a bit hurt, and she jumped up on to her feet in a moment: she looked up, but it was all dark overhead; before her was another long passage, and the White Rabbit was still in sight, hurrying down it. There was not a moment to be lost: away went Alice like the wind, and was just in time to hear it say, as it turned a corner, 'Oh my ears and whiskers, how late it's getting!' She was close behind it when she turned the corner, but the Rabbit was no longer to be seen: she found herself in a long, low hall, which was lit up by a row of lamps hanging from the roof. There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again. Suddenly she came upon a little three-legged table, all made of solid glass; there was nothing on it except a tiny golden key, and Alice's first thought was that it might belong to one of the doors of the hall; but, alas! either the locks were too large, or the key was too small, but at any rate it would not open any of them. However, on the second time round, she came upon a low curtain she had not noticed before, and behind it was a little door about fifteen inches high: she tried the little golden key in the lock, and to her great delight it fitted! Alice opened the door and found that it led into a small passage, not much larger than a rat-hole: she knelt down and looked along the passage into the loveliest garden you ever saw. How she longed to get out of that dark hall, and wander about among those beds of bright flowers and those cool fountains, but she could not even get her head though the doorway; 'and even if my head would go through,' thought poor Alice, 'it would be of very little use without my shoulders. Oh, how I wish I could shut up like a telescope! I think I could, if I only know how to begin.' For, you see, so many out-of-the-way things had happened lately, that Alice had begun to think that very few things indeed were really impossible. There seemed to be no use in waiting by the little door, so she went back to the table, half hoping she might find another key on it, or at any rate a book of rules for shutting people up like telescopes: this time she found a little bottle on it, ('which certainly was not here before,' said Alice,) and round the neck of the bottle was a paper label, with the words 'DRINK ME' beautifully printed on it in large letters. It was all very well to say 'Drink me,' but the wise little Alice was not going to do that in a hurry. 'No, I'll look first,' she said, 'and see whether it's marked "poison" or not'; for she had read several nice little histories about children who had got burnt, and eaten up by wild beasts and other unpleasant things, all because they would not remember the simple rules their friends had taught them: such as, that a red-hot poker will burn you if you hold it too long; and that if you cut your finger very deeply with a knife, it usually bleeds; and she had never forgotten that, if you drink much from a bottle marked 'poison,' it is almost certain to disagree with you, sooner or later. However, this bottle was not marked 'poison,' so Alice ventured to taste it, and finding it very nice, (it had, in fact, a sort of mixed flavour of cherry-tart, custard, pine-apple, roast turkey, toffee, and hot buttered toast,) she very soon finished it off. 'What a curious feeling!' said Alice; 'I must be shutting up like a telescope.' And so it was indeed: she was now only ten inches high, and her face brightened up at the thought that she was now the right size for going through the little door into that lovely garden. First, however, she waited for a few minutes to see if she was going to shrink any further: she felt a little nervous about this; 'for it might end, you know,' said Alice to herself, 'in my going out altogether, like a candle. I wonder what I should be like then?' And she tried to fancy what the flame of a candle is like after the candle is blown out, for she could not remember ever having seen such a thing. After a while, finding that nothing more happened, she decided on going into the garden at once; but, alas for poor Alice! when she got to the door, she found she had forgotten the little golden key, and when she went back to the table for it, she found she could not possibly reach it: she could see it quite plainly through the glass, and she tried her best to climb up one of the legs of the table, but it was too slippery; and when she had tired herself out with trying, the poor little thing sat down and cried. 'Come, there's no use in crying like that!' said Alice to herself, rather sharply; 'I advise you to leave off this minute!' She generally gave herself very good advice, (though she very seldom followed it), and sometimes she scolded herself so severely as to bring tears into her eyes; and once she remembered trying to box her own ears for having cheated herself in a game of croquet she was playing against herself, for this curious child was very fond of pretending to be two people. 'But it's no use now,' thought poor Alice, 'to pretend to be two people! Why, there's hardly enough of me left to make one respectable person!' Soon her eye fell on a little glass box that was lying under the table: she opened it, and found in it a very small cake, on which the words 'EAT ME' were beautifully marked in currants. 'Well, I'll eat it,' said Alice, 'and if it makes me grow larger, I can reach the key; and if it makes me grow smaller, I can creep under the door; so either way I'll get into the garden, and I don't care which happens!' She ate a little bit, and said anxiously to herself, 'Which way? Which way?', holding her hand on the top of her head to feel which way it was growing, and she was quite surprised to find that she remained the same size: to be sure, this generally happens when one eats cake, but Alice had got so much into the way of expecting nothing but out-of-the-way things to happen, that it seemed quite dull and stupid for life to go on in the common way. So she set to work, and very soon finished off the cake. August 15 Prototype JavaScript Framework Review (Cont'd)String.prototype 的扩展: strip: function() { Introduction to Ajax August 13 王菲选辑最近,无意识地断断续续哼哼听听地温故了些王菲的歌,集中选辑如下:
August 12 学术三境界古今之成大事业、大学问者,必经过三种之境界。“昨夜西风凋碧树,独上高楼,望尽天涯路①”,此第一境也。“衣带渐宽终不悔,为伊消得人憔悴②”,此第二境也。“众里寻他千百度,回头蓦见,那人正在灯火阑珊处③”,此第三境也。此等语皆非大词人不能道。然遽以此意解释诸词,恐晏、欧诸公所不许也。
August 09 秋兴赋 - 晋·潘安仁晋十有四年,余春秋三十有二,始见二毛。以太尉掾兼虎贲中郎将,寓直于散骑之省。高阁连云,阳景罕曜,珥蝉冕而袭纨绮之士,此焉游处。仆野人也,偃息不过茅屋茂林之下,谈话不过农夫田父之客。摄官承乏,猥厕朝列,夙兴晏寝,匪遑疷宁,譬犹池鱼笼鸟,有江湖山薮之思。于是染翰操纸,慨然而赋。于时秋也,故以"秋兴"命篇。辞曰: 四时忽其代序兮,万物纷以回薄。览花莳之时育兮,察盛衰之所托。感冬索而春敷兮,嗟夏茂而秋落。虽末士之荣悴兮,伊人情之美恶。善乎宋玉之言曰:"悲哉,秋之为气也!萧瑟兮草木摇落而变衰,憀栗兮若在远行,登山临水送将"。夫送归怀慕徒之恋兮,远行有羁旅之愤。临川感流以叹逝兮,登山怀远而悼近。彼四戚之疚心兮,遭一涂而难忍。嗟秋日之可哀兮,谅无愁而不尽。 野有归燕,隰有翔隼。游氛朝兴,槁叶夕殒。于是乃屏轻箑,释纤絺,藉莞蒻,御袷衣。庭树槭以洒落兮,劲风戾而吹帷。蝉嘒嘒而寒吟兮,雁飘飘而南飞。天晃朗以弥高兮,日悠阳而浸微。何微阳之短晷,觉凉夜之方永。月朣朦以含光兮,露凄清以凝冷。熠耀粲于阶闼兮,蟋蟀鸣乎轩屏。听离鸿之晨吟兮,望流火之余景。宵耿介而不寐兮,独辗转于华省。 悟时岁之遒尽兮,慨俛首而自省。斑鬓髟以承弁兮,素发飒以垂领。仰群俊之逸轨兮,攀云汉以游骋。登春台之熙熙兮,珥金貂之炯炯。苟趣舍之殊涂兮,庸讵识其躁静。闻至人之休风兮,齐天地于一指。彼知安而忘危兮,故出生而入死。行投趾于容迹兮,殆不践而获底。阙侧足以及泉兮,虽猴猿而不履。龟祀骨于宗祧兮,思反身于绿水。且敛衽以归来兮,忽投绂以高厉。耕东皋之沃壤兮,输黍稷之余税。泉涌湍于石间兮,菊扬芳于崖澨。澡秋水之涓涓兮,玩游鯈之潎潎。逍遥乎山川之阿,放旷乎人间之世。悠哉游哉,聊以卒岁。 July 25 Donald Ervin Knuth
Donald E. Knuth was born in Milwaukee, Wisconsin on January 10, 1938 and is world-famous for he is Professor Emeritus of The Art of Computer Programming. From his website and many other biographys you can find Knuth maded so many great academic or other achievements. In 1971, Knuth was the recipient of the first ACM Grace Murray Hopper Award. He has received various other awards including the Turing Award, the National Medal of Science, the John von Neumann Medal and the Kyoto Prize. After producing the third volume of his series in 1976, he expressed such frustration with the nascent state of the then newly developed electronic publishing tools (esp. those which provided input to phototypesetters) that he took time out to work on typesetting and created the TeX and METAFONT tools. In addition to his writings on computer science, Knuth is also the author of 3:16 Bible Texts Illuminated. In his biography, you can also learn Knuth is known for a famous programmer as well as for his geek professional humor. He pays a finder's fee of $2.56 for any typos/mistakes discovered in his books, because "256 pennies is one hexadecimal dollar". Version numbers of his TeX software approach the transcendental number π, that is versions increment in the style 3, 3.1, 3.14 and so on. Version numbers of Metafont approach the number e similarly. He once warned users of his software, "Beware of bugs in the above code; I have only proved it correct, not tried it." Knuth's impact is far-reaching. I also found Knuth has a Chinese name 高德納, given to him in 1977 by Frances Yao just before his first visit to China. While acknowledging his contributions to the field, Knuth comments only that "some people seem to be interested in what I have to say." Recently weeks, I bought the first volume of "The Art of Computer Progamming". I hope I can also seek the funny comes from this respected and immortal master.
References: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|