Duran's technical life
踏踏实实学技术,认认真真做研究。

环境:Hibernate 2.1.7 + MySQL4.1(MySQL的编码已设置为utf8)
问题:通过Hibernate向MySQL写入中文后,通过Hibernate取回数据,在console中打印java对象显示正常。但在MySQL Query Browser中看到的是乱码,传给前端的Flex也是乱码。
原因:Hibernate的基础还是JDBC,所以一样需要设置characterEncoding!
解决方法
在hibernate.cfg.xml中应该这样写

<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
如果用hibernate.properties
#hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

ps:处理中文的话,characterEncoding用GBK一样可以。
但字节编码问题最好的解决方法还是统一使用UTF-8!!!
posted @ 2005-06-10 21:35 Duran's technical life 阅读(15883) | 评论 (13)编辑 收藏
 
Degrees and what they mean:
 BS  I can code, do basic technical work
 MS  More design responsibility and more independence
PhD  Research, teaching, or architecting/design
posted @ 2005-06-10 12:35 Duran's technical life 阅读(511) | 评论 (0)编辑 收藏
 
GUI编程太繁琐,而且感觉相对而言“技术含量”不高。讨厌也得学啊,毕竟写的程序要通过GUI暴露给用户。
有的人笑话java的跨平台是幌子,一次编写,到处调试。我倒想问,怎样该被称做“幌子”。不同的操作系统之间的差异本来就很大,做到完全统一从原理上就是永远不可能的。就像写的再好的模拟器也只能达到99%的模拟程度!Swing其实已经不错了,随着jdk更新的每个版本都能有所改进。但是仍然远没达到让人满意的地步。一是表现力仍然很差,看Swing做的软件Together、NetBeans……界面真的好丑!也看了一些第三方公司自己扩展Swing做出的demo,效果还不错。要价几k美刀,也不知开发效率如何。二就是开发效率仍然过低。“鄙视”vb、pb这样的RAD工具,写出来的代码混乱没架构,但是它们做GUI的效率很高。
最期待的表现层技术还是M$的Avalon,M$虽然讨厌,但它的开发环境对程序员是最友善的。
说说Flex。Flex学习曲线不大,各种UI Component超好用。不爽的ActionScript语法好别扭,不习惯。现有的官方reference文档太少了,更新又慢,例子也少得很。做东西时没有pattern模仿,自己摸蛮痛苦而且写出来的代码也觉得有些cuo。
posted @ 2005-06-07 23:18 Duran's technical life 阅读(550) | 评论 (0)编辑 收藏
 

Martin Fowler是ThoughtWorks的首席科学家。从80年代开始,他就一直从事软件开发的工作。在80年代中期,他对面向对象开发这个新领域发生了兴趣。他擅长在商业信息系统中加入面向对象的思想。时至21世纪,他又在UML推广普及、领域建模、企业应用开发和敏捷方法等方面建树卓著,被称为软件开发的教父。

可惜他只去上海、北京,眼巴巴地看着啊。

posted @ 2005-06-01 23:31 Duran's technical life 阅读(268) | 评论 (0)编辑 收藏
 
以前没人考研的时候都是开考研动员大会,现在人招多了辅导员又开始劝大家条件不好的就尽量去找工作,呵呵。看上一届就业单位名单里居然连“安利(中国)日用品有限公司”都有,真是晕菜死。
posted @ 2005-06-01 23:24 Duran's technical life 阅读(268) | 评论 (0)编辑 收藏
 
考试周,明天晚上GF来,忙~~~自习去了。
posted @ 2005-05-26 07:45 Duran's technical life 阅读(368) | 评论 (1)编辑 收藏
 
     摘要: 参考package EDU.oswego.cs.dl.util.concurrent 1public interface Sync {2    public void acquire();3    public void release();4...  阅读全文
posted @ 2005-05-19 09:54 Duran's technical life 阅读(1029) | 评论 (0)编辑 收藏
 
     庐山回来后就没写过blog了,不过每天晚上跑步倒是一直在坚持。写blog的目的是对自己学习成长的记录、反省和促进,贵在坚持。上个星期用java写了个评估项目CPM的小程序,实践了hibernate、xDoclet,练习了反复Refactor+TDD的开发方式,感觉很爽。当做到PERT计算功能时发现需要查正态分布表,google查了查没找到第三方的java数学运算包。直到在图书管看书时才发现用编程进行复杂的函数运算的方法是属于《计算方法》课的,奇怪我们专业为什么不开这门课。发现自己知识结构上的一个空白。

    前几天做操作系统作业,顺便看了看《Concurrent Programming in Java: Design Principles and Patterns》,确信《程序员》第三期上关于Mutex的一段代码完全是错误的,又给《程序员》编辑发了mail(上次发了mail后了无音讯)。没想很快到过了收到编辑和作者的回复,确认了错误。倒不是说出了错就说明作者水平不高,java相关技术太多了,软件开发相关技术就更多了。我想说的的是搞软件开发虽然是个智力活动,但终究谈不上需要极高的天赋才行(不像数学这样的基础理论学科)。作者也是人,当然也会犯错误。国外的那些软件大牛也是人,他们也是一步一步成长起来的,其实并不是在神坛上高不可攀。那为什么国内高手少呢?环境所致。记得好像是在白云上看过有个人说“Linus好牛,20岁就搞出了linux,我们20岁能干吗”,下面有个人回帖说“那是他在Stanford,要是把你丢在那,你说不定也能做出个XXX来”。

    HeadStorm是我很钦佩的一位程序员,工作了几年后凭着不停的学习、实践对j2ee应用开发技术有了较高的造诣。在圈内取得一定成绩后又及时地认清了自己的目标,做一个全面的技术高手,要“往下走”,而不是安于“j2ee架构师”这个名头。说到“往上走”和“往下走”之争,想到两个不好笑的小笑话。第一个是有天听到几个同学谈论开发软件,一个同学说开发mis没意思,另一个同学就说:“那你去写汇编啊,写底层驱动啊,那个有技术含量。”第二个是有次听武大计算机系某老师说:“java有什么了不起,我用c一样可以实现多态、继承。”第一个笑话不加评论。第二个笑话让我想起以前一直流传的一句话:“学程序,什么语言都一样。”个人觉得这句话是带有很大的误导成分的。语法层面上,基本的顺序、选择、循环语句是差不多;但要你实际做开发应用呢,你还能说用啥都一样吗。不同的语言,设计者在最初设计语言特性时就决定了该语言最有效的应用领域。Ruby之类的动态语言现在大放光彩也是有其理由的。在“向上走”的时候也要注意提醒自己“勿筑高台于浮沙”。技术无尽的,时间是有限的,要学习什么,学到什么程度,得有清醒的头脑。

    说到学习技术,决定开始研究Flex了,看了两天,感觉还不错。这才是程序员需要的表现层方案,拖放好控件,再编写用户交互的script就完了,至于很cool的页面表现效果的是如何做到的,那是Macromedia的事情。什么struts、webWork之流都是自娱自乐,并不能给用户带来多大的使用体验。JSF想法还不错,可有多少人支持呢。你说你表现层框架技术再先进,可表现出来的交互界面像yts一样,对用户来说还不如回到c/s的怀抱。js的登顶之作也就是Bindows了,还不就那样。一句话,非常看好Flex!Laszlo也还行,但终究没有Flex感觉好,而且是开源的,技术支持也没Flex强。Flex还有一个很好的可视化开发工具——FlexBuilder,大大加快了开发效率。

posted @ 2005-05-19 09:47 Duran's technical life 阅读(419) | 评论 (1)编辑 收藏
 
Suppose we start with a simple <one-to-many> association from Parent to Child.
 1<hibernate-mapping package="org.hibernate.auction.model">
 2 <class name="Parent" table="PARENT">
 3  <id name="id" column="PARENT_ID" type="long">
 4   <generator class="native"></generator>
 5  </id>
 6  <set name="children">
 7   <key column="MY_PARENT_ID" />
 8   <one-to-many class="org.hibernate.auction.model.Child" />
 9  </set>
10 </class>
11 <class name="Child" table="CHILD">
12  <id name="id" column="CHILD_ID" type="long">
13   <generator class="native"></generator>
14  </id>
15  <property name="name" column="NAME" type="string" />
16 </class>
17</hibernate-mapping>

If we were to execute the following code

 1Parent p = new Parent(new HashSet()); 
 2Child c1 = new Child(new String("c1"
));
 3

 4try 
{
 5    session =
 sessionFactory.openSession();            
 6    tx =
 session.beginTransaction();           
 7
    session.save(p);
 8    Long pid =
 p.getId();
 9
    tx.commit();
10
    session.close();
11
    
12    session =
 sessionFactory.openSession();
13    tx =
 session.beginTransaction();
14    Parent pp = (Parent) session.load(Parent.class
, pid); 
15
    pp.getChildren().add(c1);        
16
    session.save(c1);
17
    session.flush();
18
    tx.commit();
19

20}
 catch (HibernateException e) {
21}

Hibernate would issue two SQL statements:
- an INSERT to create the record for c
- an UPDATE to create the link from p to c

Hibernate: insert into PARENT values ( )
Hibernate: select parent0_.PARENT_ID as PARENT_ID0_ from PARENT parent0_ where parent0_.PARENT_ID=?
Hibernate: select children0_.MY_PARENT_ID as MY_PAREN3___, children0_.CHILD_ID as CHILD_ID__, children0_.CHILD_ID as CHILD_ID0_, children0_.NAME as NAME0_ from CHILD children0_ where children0_.MY_PARENT_ID=?

- Hibernate: insert into CHILD (NAME) values (?)
- Hibernate: update CHILD set MY_PARENT_ID=? where CHILD_ID=?

This is not only inefficient, but also violates any NOT NULL constraint on the
MY_PARENT_ID column.

**********1
The underlying cause is that the link (the foreign key parent_id) from p to c is not considered part of the state of the Child object and is therefore not created in the INSERT. So the solution is to make the link part of the Child mapping.
**********1

 <many-to-one name="my_parent"
  class
="org.hibernate.auction.model.Parent" column="MY_PARENT_ID"
  not-null
="true">
 
</many-to-one>

**********2
Now that the Child entity is managing the state of the link, we tell the collection not to update the link. We use the inverse attribute.
**********2

 <set name="children" inverse="true">
  
<key column="MY_PARENT_ID" />
  
<one-to-many class="org.hibernate.auction.model.Child" />
 
</set>

The following code would be used to add a new Child

1Parent pp = (Parent) session.load(Parent.class, pid); 
2
pp.getChildren().add(c1);
3c1.setMy_parent(pp); //the link is part of the Child object's state. @see***1 

4session.save(c1);

And now, only one SQL INSERT would be issued!
To tighten things up a bit, we could create an addChild() method of Parent.

1public void addChild(Child c) {
2  c.setParent(this
);
3
  children.add(c);
4}

Now, the code to add a Child looks like

1Parent p = (Parent) session.load(Parent.class, pid);
2Child c = new
 Child();
3p.addChild(c);  // 1.let both side of the association knows each other.

4session.save(c); // 2.save the non-verse side

=========================================================================================
CONCLUSION:
1.The non-inverse(inverse="false") side is responsible to save the in-memory representation (object) to the database .
--- you should save the child: session.save(c);
2.Changes made only to the inverse end of the association are not persisted.
--- you needn't update the parent: //session.update(p);

 1session = sessionFactory.openSession();
 2tx =
 session.beginTransaction();
 3Parent pp = (Parent) session.load(Parent.classnew Long(1
)); 
 4pp.getChildren().add(c1); //the parent(pp) now knowns about the relationship

 5c1.setMy_parent(pp); //the child(c1) now knows about the relationship
 6

 7//
 when cascade="save-update", update(p) will cause all p's
 8//
 referencing children become persistence(cascade 
 9//
 saveOrUpdate() operation to parent's all children).
10//
 So, it's same as you explicitly call session.save(c1).
11

12//
 when cascade="none"(default), update(p) will just cause
13//
 p become persistence.
14//
 And inverse="true", update(p) just persists p, but not 
15// persists the link between p and c1. @see ***2

16session.update(pp);  
17//session.save(c1);  

18session.flush();
19
tx.commit();
20


Hibernate: select parent0_.PARENT_ID as PARENT_ID0_ from PARENT parent0_ where parent0_.PARENT_ID=?
Hibernate: select children0_.MY_PARENT_ID as MY_PAREN3___, children0_.CHILD_ID as CHILD_ID__, children0_.CHILD_ID as CHILD_ID0_, children0_.NAME as NAME0_, children0_.MY_PARENT_ID as MY_PAREN3_0_ from CHILD children0_ where children0_.MY_PARENT_ID=?

 1session = sessionFactory.openSession();
 2tx =
 session.beginTransaction();
 3Parent pp = (Parent) session.load(Parent.classnew Long(1
)); 
 4
pp.getChildren().add(c1);
 5
c1.setMy_parent(pp);
 6//
session.update(pp);
 7

 8//
 when save(c1), because c1 holds a reference to its Parent
 9// (my_parent), so p become persistence too.

10session.save(c1);  //The relationship will be saved. @see ***1
11session.flush();
12
tx.commit();
13


Hibernate: select parent0_.PARENT_ID as PARENT_ID0_ from PARENT parent0_ where parent0_.PARENT_ID=?
Hibernate: select children0_.MY_PARENT_ID as MY_PAREN3___, children0_.CHILD_ID as CHILD_ID__, children0_.CHILD_ID as CHILD_ID0_, children0_.NAME as NAME0_, children0_.MY_PARENT_ID as MY_PAREN3_0_ from CHILD children0_ where children0_.MY_PARENT_ID=?
Hibernate: insert into CHILD (NAME, MY_PARENT_ID) values (?, ?)


 

posted @ 2005-05-19 09:28 Duran's technical life 阅读(563) | 评论 (0)编辑 收藏
仅列出标题
共2页: 上一页 1 2