hk2000c技术专栏

技术源于哲学,哲学来源于生活 关心生活,关注健康,关心他人

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  111 随笔 :: 1 文章 :: 28 评论 :: 0 Trackbacks

#

class ThreadA
{
  public static void main(String[] args)
  {
    ThreadB b=new ThreadB();
    b.start();
    System.out.println("b is start....");
    synchronized(b)//括号里的b是什么意思,起什么作用?
    {
      try
      {
 System.out.println("Waiting for b to complete...");
 b.wait();//这一句是什么意思,究竟让谁wait?
        System.out.println("Completed.Now back to main thread");
      }catch (InterruptedException e){}
    }
    System.out.println("Total is :"+b.total);
   }
}


class ThreadB extends Thread
{
  int total;
  public void run()
  {
    synchronized(this)
    {
      System.out.println("ThreadB is running..");
      for (int i=0;i<100;i++ )
      {
        total +=i;
        System.out.println("total is "+total);
      }
      notify();
    }
  }
}

要分析这个程序,首先要理解notify()和wait(),为什么在前几天纪录线程的时候没有纪录这两个方法呢,因为这两个方法本来就不属于Thread类,而是属于最底层的object基础类的,也就是说不光是Thread,每个对象都有notify和wait的功能,为什么?因为他们是用来操纵锁的,而每个对象都有锁,锁是每个对象的基础,既然锁是基础的,那么操纵锁的方法当然也是最基础了.

 再往下看之前呢,首先最好复习一下Think in Java的14.3.1中第3部分内容:等待和通知,也就是wait()和notify了.

按照Think in Java中的解释:"wait()允许我们将线程置入“睡眠”状态,同时又“积极”地等待条件发生改变.而且只有在一个notify()或notifyAll()发生变化的时候,线程才会被唤醒,并检查条件是否有变."

  我们来解释一下这句话.
  "wait()允许我们将线程置入“睡眠”状态",也就是说,wait也是让当前线程阻塞的,这一点和sleep或者suspend是相同的.那和sleep,suspend有什么区别呢?

   区别在于"(wait)同时又“积极”地等待条件发生改变",这一点很关键,sleep和suspend无法做到.因为我们有时候需要通过同步(synchronized)的帮助来防止线程之间的冲突,而一旦使用同步,就要锁定对象,也就是获取对象锁,其它要使用该对象锁的线程都只能排队等着,等到同步方法或者同步块里的程序全部运行完才有机会.在同步方法和同步块中,无论sleep()还是suspend()都不可能自己被调用的时候解除锁定,他们都霸占着正在使用的对象锁不放.
   而wait却可以,它可以让同步方法或者同步块暂时放弃对象锁,而将它暂时让给其它需要对象锁的人(这里应该是程序块,或线程)用,这意味着可在执行wait()期间调用线程对象中的其他同步方法!在其它情况下(sleep啊,suspend啊),这是不可能的.
   但是注意我前面说的,只是暂时放弃对象锁,暂时给其它线程使用,我wait所在的线程还是要把这个对象锁收回来的呀.wait什么?就是wait别人用完了还给我啊!
   好,那怎么把对象锁收回来呢?
   第一种方法,限定借出去的时间.在wait()中设置参数,比如wait(1000),以毫秒为单位,就表明我只借出去1秒中,一秒钟之后,我自动收回.
   第二种方法,让借出去的人通知我,他用完了,要还给我了.这时,我马上就收回来.哎,假如我设了1小时之后收回,别人只用了半小时就完了,那怎么办呢?靠!当然用完了就收回了,还管我设的是多长时间啊.

   那么别人怎么通知我呢?相信大家都可以想到了,notify(),这就是最后一句话"而且只有在一个notify()或notifyAll()发生变化的时候,线程才会被唤醒"的意思了.
   因此,我们可将一个wait()和notify()置入任何同步方法或同步块内部,无论在那个类里是否准备进行涉及线程的处理。而且实际上,我们也只能在同步方法或者同步块里面调用wait()和notify().

   这个时候我们来解释上面的程序,简直是易如反掌了.

   synchronized(b){...};的意思是定义一个同步块,使用b作为资源锁。b.wait();的意思是临时释放锁,并阻塞当前线程,好让其他使用同一把锁的线程有机会执行,在这里要用同一把锁的就是b线程本身.这个线程在执行到一定地方后用notify()通知wait的线程,锁已经用完,待notify()所在的同步块运行完之后,wait所在的线程就可以继续执行.

posted @ 2007-12-19 15:20 hk2000c 阅读(7810) | 评论 (9)编辑 收藏

public class Foo {
    // 似有静态内部类, 只有当有引用时, 该类才会被装载
    private static class LazyFoo {
       public static Foo foo = new Foo();
    }

    public static Foo getInstance() {
       return LazyFoo.foo;
    }
}

posted @ 2007-12-18 23:58 hk2000c 阅读(323) | 评论 (0)编辑 收藏

一、 金 丝 燕 的 特 征 与 特 性

金丝燕种类大致有15种,会制造”燕窝”的燕子在生物学上被归类为:动物界,脊椎动物门,脊椎动物亚门,鸟纲,雨燕目,雨燕科,金丝燕属,种有:褐腰金丝燕,灰腰金丝燕,爪哇金丝燕,方尾金丝燕,短嘴金丝燕,白腰金丝燕,小白腰金丝燕,白喉针尾
金丝燕,白腹金丝燕,小白腹金丝燕,戈式金丝燕等。
根据八年来长期在印度尼西亚苏门达腊岛的观察与实际建筑燕屋供金丝燕栖息,在燕屋内外装设红外线监视器,并于金丝燕入口处加设红外线计数器,经长期观察的结果,金丝燕昼出晚归,成年金丝燕平均的身厚(含毛)约4cm,身长约10cm,双翅展开约22cm,重约20g,脚短且软,四个脚趾都朝前方无法抓握,所以不像一般的鸟类三个脚趾朝前一趾朝后,能栖息在电线上或在地上行走,金丝燕只能用四趾钩住挂在垂直的墙壁或岩壁上,金丝燕对回音有很好的定位感,能在漆黑的环境中自由飞行,并找到原来的栖息点,
成年金丝燕每年平均会有三次的产卵,每年4月8月12月是金丝燕的产卵高峰期,每次都产2个白色的卵,长约2.0cm,宽约1.2cm,重约3.5g,
每次产卵前约30天开始筑巢,每次筑巢都是雌雄燕一起筑需约30天,卵的孵化期约20天,小金丝燕孵化出后雌雄金丝燕会一起哺育,约40天后小金丝燕即能自行飞翔觅食,且另行寻找栖息点,不再使用此巢,从筑巢到小金丝燕飞离需约90天,

二、 金 丝 燕 的 繁 殖 周 期
金丝燕会在小金丝燕飞离后(空巢期)约30天后,就会再进行第二次的筑巢,金丝燕每次的筑巢都会筑新的巢,若前次筑的巢尚在,金丝燕会在原有的窝上面再筑一次巢,并不会因前次筑的巢还在而不再筑新巢,所以采收燕窝并不会造成金丝燕无家可归.


三、 金 丝 燕 与 家 燕 的 区 别
金丝燕觅食都是在飞行中进行的,而且只专吃会飞的昆虫或小生物,而喝水除了喝雨水外,会低飞将嘴巴贴在水池的水面上,边飞边喝水.常见停留在电线上或栖息于屋檐下的燕子是雀形目,燕科,的家燕种,是用泥土来筑巢的,所以是不会制造”燕窝”的。

四、 金 丝 燕 的 俗 称
金丝燕的俗称”白燕(又称官燕)”和”黄燕”,其实这两种名称,都是金丝燕的另一种称呼,金丝燕所筑的”燕窝”其颜色,在刚筑好时,一般都是珍珠白的颜色,但时间一久会因金丝燕筑巢的所处的环境而有所不同,虽是筑在同一个环境上,也会因所筑的时间长短不同而有不同的颜色,一般上”燕窝”存在的时间久了其颜色会由珍珠白色慢慢转变成灰白(即俗称”白燕,又称官燕”)或珍珠黄(即俗称”黄燕”)的颜色,这是因受氧化的结果,属自然现象,类似萷了皮的水黎放久了因接触空气中的氧,产生氧化作用而变色一样;大多数的金丝燕从筑巢,生蛋,孵化,成长到小金丝燕离巢,大约需90天的时间,采收”燕窝”的时间,都是在小金丝燕已离巢时再进行采收,所以”燕窝”的颜色是很自然的灰白或珍珠黄。

五、 屋 燕 的 习 性
金丝燕筑巢于人工建筑的屋内,我们称之为”屋燕”,因燕窝有其特殊的价值性,让人们想尽办法引金丝燕进屋来筑巢,一般通用的方法会用音响播放金丝燕的叫声,引其入屋,并保持屋中无光线,温度在28度C左右,湿度控制在90%以上,来营造一个金丝燕喜欢栖息与居住的环境。

六、 燕 屋 的 生 态 环 境
屋中有”燕窝”的屋主都会视金丝燕如珍宝,都会竭尽所能,尽心尽力的保护它,所以不可能让其它的动物进出或栖息,尤其是猫头鹰,猫头鹰是金丝燕最大的天敌,屋主会将也金丝燕的入口做成----长60-90CM.高20-30CM.并在洞口四周加装电灯.来防止猫头鹰的进入外.更会将燕屋的环境保持的很好。

七、 采 收 燕 窝 的 准 备
所有的燕屋屋主好不容易才将金丝燕引进屋内栖息筑窝,绝对不会以杀鸡取卵的方式来采收燕窝,所以我们都是在小金丝燕飞离巢后的空巢期,也就是在每年的2月6月10月才开始采收,且在采收每个燕窝之前都要先用镜子以反射的方式捡查小金丝燕是否已飞离,在确定小金丝燕已飞离。

八、 如 何 采 收 燕 窝
所以我们都是在小金丝燕飞离巢后的空巢期,也就是在每年的2月6月10月才开始采收,且在采收每个燕窝之前都要先用在确定小金丝燕已飞离,采收工人才架设楼梯爬上采收,采收工人会先以冷开水将整个燕窝喷湿,再以小铲子将燕窝取下.

九、 坚 持 100% 人 工 精 挑 燕 窝
刚采收的燕窝,因先前有喷水,需先分类晾干,以利保存,待日后分批慢慢清理,清理燕窝时须在恒温恒湿无尘的环境中工作,将燕窝以冷开水浸湿清洗几次,再用布盖上约30分钟使其软化,再用不锈钢的小夹子仔细的夹除异物,待异物处理完成后,尽量保持其原有的形状,置放于恒温,恒湿无尘的干燥室中干燥杀菌,经过检验之后再进包装。

十、 燕 窝 的 种 类 (一)
以来源区分为:洞燕,屋燕,草燕,毛燕.。
洞燕—较为灰黑或黄色,与毛及杂质较多,口感较为爽实.;屋燕—色较白,质松,毛少,口感较滑软;草燕—平均枯草重量占整个窝90%以上;毛燕—平均羽毛重量占整个窝50%以上,一般”毛燕”都喜欢筑巢于山洞中。

十一、 燕 窝 的 种 类 (二)
以颜色区分为:白燕(又称官燕),黄燕,红燕(俗称血燕);以形状区分为:盏,块,条,丝,碎。

十二、 二 次 结 巢 的 燕 窝
金丝燕每次的筑巢都会筑新的巢,若前次筑的巢尚在,金丝燕会在原有的窝上面再筑一次巢,并不会因前次筑的巢还在而不再筑新巢,所以采收燕窝并不会造成金丝燕无家可归。

posted @ 2007-12-08 09:27 hk2000c 阅读(2178) | 评论 (0)编辑 收藏

美国有超过1000家院校提供硕士及博士学位课程,而这类课程主要注重专科学系,以协助学生吸取不同的专门技能或进行深入研究。 


美国研究生学位不同之处

研究生学位分为专业学位及研究学位,而这两类学位都有硕士程度及博士程度之分。

在硕士程度,专业学位(如:工商管理硕士、教育硕士、艺术硕士),提供一些实用的专门技能及直接就业机会。以研究为主的硕士学位(如:文学硕士及理学硕士),则提供一般进行研究的经验、奖学金以及修读博士的机会。有些大学更有双学位课程供选择。举例说,一般收读商科及工业工程的学生便可同时获得工商管理和理学的硕士学位。硕士学位通常可于1至2年时间完成。其中专业学位的学生需进行实习或工作;研究学位的学生则需撰写论文或考试。硕士学位与博士学位课程内容的主要分别在于前者比较注重课堂功课,较少研讨会及考试。虽然很多文学及理学硕士课程都要求学生呈交论文,但学生亦可能选择豁免此要求。

  美国博士学位不好拿

博士学位的目的在于训练学者在一个特定学科内进行研究,而这些学位通常都包括课堂作业以及一个主要研究计划。博士学位通常需4至6年时间全职才能完成。前两年包括课堂、研讨会、及有关阅读,令学生能学到有关学科的全面知识。之后会有笔试或口试,测试学生的程度。成功完成考试及研究计划并获得认可后,便可得到候选资格。研究计划一般需时1至2年时间,视乎学科而定。该学科教师会对学生的研究计划作出指导及评核成绩,不过学生需要独立地进行其研究的工作。

  如何写好推荐信

选择推荐人的过程中,应从自己以往在课堂、课外活动及工作上所认识的人着手。一个理想的推荐人应具备以下条件:他(她)对你有很高的评价;拥有对你的多方面认知;对你所选的院校及学科有所认识;曾教导过为数不少的学生和可以将你与其他学生作正面的比较;入学办事处小组对其有所认识,而且会认真衡量其所作出的判断;拥有上佳的书写沟通技巧。当然,没有人可以完全符合以上条件,只需选择最接近自己理想中的人选便行了。

如果你是工作了数年后回归校园,你未必可以找回你大学时的教授。如果这样,便应联络你申请的院校了解有关的政策。

他们或可让你以雇主或上司的推荐信代替。当你决定了替你写推荐信的人选后,可以询问他们是否觉得自己对你有足够的认识去写封有意义的推荐信。向他们提供所需资料,协助他们替你写封详尽且有效的推荐信。

譬如:成绩单、履历表、申请入学时写的文章副本及你的研究论文副本,都是有帮助的资料。推荐信必须由推荐人亲自书写及签署。如果能将一个贴有空邮邮票及已写有地址的信封给你的推荐人便更好。

  个人自述

个人自述亦是申请入学过程中的重要一环。从你呈交的文章中,入学处小组可更清晰地认识你的自发能力和你对所选学科的投入程度;你对该课程的期望;你的写作能力;你主要的学习兴趣;你的研究或工作经验;你的学术背景;你目前和长远的目标;你申请入读的原因;或你所拥有的其它特长。文章的主题可能很明确或很辽阔,而不同的院校对文章长短的要求亦有所不同。最重要是要仔细阅读问题及作答。切忌将同一篇文章放进不同的申请信内。有关撰写文章的其它详情和建议可参考Peterson''sGuidestoGraduatePrograms及其它有关资料。

  选择研究院课程

比较美国各种研究院课程之时,除却地点、学校大小及收费之外,另外还有些更重要的因素应注意。

以下的几项重点便能帮助决定哪家院校最适合你:

  学习科目

●认清自己喜爱的学科及研究的兴趣。你或需请教教授或学习指导,找出哪家院校有提供你所选择的学科。

●向自己的教授们请教有关你所选择学科的课程。他们或可介绍你找到其他教授或所需资料。

●查看有列明院校各部门、师资及教师研究专长的参考书籍。找些与你有近似研究兴趣的教授。

●参考有关自己学科的学术刊物,了解谁活跃于该学系、编辑部门的人、谁撰写的文章最能吸引你,找出这些教授及研究专员所属的院校。

●找出哪所大学有提供你心目中学系的研究院。

切记如果你的研究喜好与该校的教师类似的话,你获录取和得到奖学金的机会便会相应提高。

课程及论文要求

●找出完成该学科所需时间多久。

●查看学校章程内的课程是否合乎你的要求和兴趣。

●找出完成学位的要求,是否包含考试、口试或论文。

posted @ 2007-12-05 23:13 hk2000c 阅读(534) | 评论 (0)编辑 收藏

  在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准。它非常强大、灵活,而且具备了优异的性能。在本文中,我们将了解如何使用Java 5 注释来简化Hibernate代码,并使持久层的编码过程变得更为轻松。

  传统上,Hibernate的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射文件,并且在启动时进行加载。创建这些映射有很多方法,可以从已有数据库模式或Java类模型中自动创建,也可以手工创建。无论如何,您最终将获得大量的 Hibernate 映射文件。此外,还可以使用工具,通过javadoc样式的注释生成映射文件,尽管这样会给您的构建过程增加一个步骤。

  在最近发布的几个Hibernate版本中,出现了一种基于 Java 5 注释的更为巧妙的新方法。借助新的 Hibernate Annotation 库,即可一次性地分配所有旧映射文件——一切都会按照您的想法来定义——注释直接嵌入到您的 Java 类中,并提供一种强大及灵活的方法来声明持久性映射。籍由自动代码完成和语法突出显示功能,最近发布的Java IDE也为其提供了有力的支持。

  Hibernate Annotation还支持新的 EJB 3 持久性规范。这些规范旨在提供一种标准化的 Java 持久性机制。由于 Hibernate 3 还提供了一些扩展,因此您可以十分轻松地遵从这些标准,并使用 EJB 3 编程模型来对 Hibernate 持久层进行编码。

  现在,让我们来动手使用Hibernate Annotation。

安装 Hibernate Annotation

  要使用 Hibernate Annotation,您至少需要具备 Hibernate 3.2和Java 5。可以从 Hibernate 站点 下载 Hibernate 3.2 和 Hibernate Annotation库。除了标准的 Hibernate JAR 和依赖项之外,您还需要 Hibernate Annotations .jar 文件(hibernate-annotations.jar)、Java 持久性 API (lib/ejb3-persistence.jar)。如果您正在使用 Maven,只需要向 POM 文件添加相应的依赖项即可,如下所示:

    ...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.1.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.2.0.ga</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
...

  下一步就是获取 Hibernate 会话工厂。尽管无需惊天的修改,但这一工作与使用 Hibernate Annotations有所不同。您需要使用 AnnotationConfiguration 类来建立会话工厂:

sessionFactory = new
AnnotationConfiguration().buildSessionFactory();

  尽管通常使用 <mapping> 元素来声明持久性类,您还是需要在 Hibernate 配置文件(通常是 hibernate.cfg.xml)中声明持久性类:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.onjava.modelplanes.domain.PlaneType"/>
<mapping class="com.onjava.modelplanes.domain.ModelPlane"/>
</session-factory>
</hibernate-configuration>

  近期的许多 Java 项目都使用了轻量级的应用框架,例如 Spring。如果您正在使用 Spring 框架,可以使用 AnnotationSessionFactoryBean 类轻松建立一个基于注释的 Hibernate 会话工厂,如下所示:

<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
...
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.onjava.modelplanes.domain.PlaneType</value>
<value>com.onjava.modelplanes.domain.ModelPlane</value>
...
</list>
</property>
</bean>

第一个持久性类

  既然已经知道了如何获得注释所支持的 Hibernate 会话,下面让我们来了解一下带注释的持久性类的情况:

  像在其他任何 Hibernate应用程序中一样,带注释的持久性类也是普通 POJO。差不多可以说是。您需要向 Java 持久性 API (javax.persistence.*)添加依赖项,如果您正在使用任何特定于 Hibernate的扩展,那很可能就是 Hibernate Annotation 程序包(org.hibernate.annotations.*),但除此之外,它们只是具备了持久性注释的普通 POJO 。下面是一个简单的例子:

@Entity
public class ModelPlane {
private Long id;
private String name;
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

  正像我们所提到的,这非常简单。@Entity 注释声明该类为持久类。@Id 注释可以表明哪种属性是该类中的独特标识符。事实上,您既可以保持字段(注释成员变量),也可以保持属性(注释getter方法)的持久性。后文中将使用基于属性的注释。基于注释的持久性的优点之一在于大量使用了默认值(最大的优点就是 “惯例优先原则(convention over configuration)”)。例如,您无需说明每个属性的持久性——任何属性都被假定为持久的,除非您使用 @Transient 注释来说明其他情况。这简化了代码,相对使用老的 XML 映射文件而言也大幅地减少了输入工作量。

生成主键

  Hibernate 能够出色地自动生成主键。Hibernate/EBJ 3 注释也可以为主键的自动生成提供丰富的支持,允许实现各种策略。下面的示例说明了一种常用的方法,其中 Hibernate 将会根据底层数据库来确定一种恰当的键生成策略:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}

定制表和字段映射

  默认情况下,Hibernate 会将持久类以匹配的名称映射到表和字段中。例如,前一个类可以与映射到以如下代码创建的表中:

CREATE TABLE MODELPLANE
(
ID long,
NAME varchar
)

  如果您是自己生成并维护数据库,那么这种方法很有效,通过省略代码可以大大简化代码维护。然而,这并不能满足所有人的需求。有些应用程序需要访问外部数据库,而另一些可能需要遵从公司的数据库命名惯例。如果有必要,您可以使用 @Table 和 @Column 注释来定制您自己的持久性映射,如下所示:

@Entity
@Table(name="T_MODEL_PLANE")
public class ModelPlane {
private Long id;
private String name;
@Id
@Column(name="PLANE_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="PLANE_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

  该内容将映射到下表中:

CREATE TABLE T_MODEL_PLANE
(
PLANE_ID long,
PLANE_NAME varchar
)

  也可以使用其他图和列的属性来定制映射。这使您可以指定诸如列长度、非空约束等详细内容。Hibernate支持大量针对这些注释的属性。下例中就包含了几种属性:

    ...
@Column(name="PLANE_ID", length=80, nullable=true)
public String getName() {
return name;
}
...

映射关系

  Java 持久性映射过程中最重要和最复杂的一环就是确定如何映射表间的关系。像其他产品一样, Hibernate 在该领域中提供了高度的灵活性,但却是以复杂度的增加为代价。我们将通过研究几个常见案例来了解如何使用注释来处理这一问题。

  其中一种最常用的关系就是多对一的关系。假定在以上示例中每个 ModelPlane 通过多对一的关系(也就是说,每个飞机模型只与一种飞机类型建立联系,尽管指定的飞机类型可以与七种飞机模型建立联系)来与 PlaneType 建立联系。可如下进行映射:

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
public PlaneType getPlaneType() {
return planeType;
}

  CascadeType 值表明 Hibernate 应如何处理级联操作。

  另一种常用的关系与上述关系相反:一对多再对一关系,也称为集合。在老式的 Hibernate 版本中进行映射或使用注释时,集合令人头疼,这里我们将简要加以探讨,以使您了解如何处理集合,例如,在以上示例中每个 PlaneType 对象都可能会包含一个 ModelPlanes 集合。可映射如下:

 @OneToMany(mappedBy="planeType",
cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
@OrderBy("name")
public List<ModelPlane> getModelPlanes() {
return modelPlanes;
}

命名查询

  Hibernate 最优秀的功能之一就在于它能够在您的映射文件中声明命名查询。随后即可通过代码中的名称调用此类查询,这使您可以专注于查询,而避免了 SQL 或者 HQL 代码分散于整个应用程序中的情况。

  也可以使用注释来实现命名查询,可以使用 @NamedQueries 和 @NamedQuery 注释,如下所示:

@NamedQueries(
{
@NamedQuery(
name="planeType.findById",
query="select p from PlaneType p left join fetch p.modelPlanes where id=:id"
),
@NamedQuery(
name="planeType.findAll",
query="select p from PlaneType p"
),
@NamedQuery(
name="planeType.delete",
query="delete from PlaneType where id=:id"
)
}
)

  一旦完成了定义,您就可以像调用其他任何其他命名查询一样来调用它们。

结束语

  Hibernate 3 注释提供了强大而精致的 API,简化了 Java 数据库中的持久性代码,本文中只进行了简单的讨论。您可以选择遵从标准并使用 Java 持久性 API,也可以利用特定于 Hibernate的扩展,这些功能以损失可移植性为代价提供了更为强大的功能和更高的灵活性。无论如何,通过消除对 XML 映射文件的需求,Hibernate 注释将简化应用程序的维护,同时也可以使您对EJB 3 有初步认识。来试试吧!

posted @ 2007-12-02 01:13 hk2000c 阅读(275) | 评论 (0)编辑 收藏

通常是封装在某一级的接口或者类没有事务声明所致

  <aop:config>
        <aop:pointcut id="systemDaoMethods" expression="execution(* com.unitedbiz.system.dao.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="systemDaoMethods"/>
    </aop:config>

申明一下就可以了

posted @ 2007-11-30 14:01 hk2000c 阅读(260) | 评论 (0)编辑 收藏

需要改装的是
图片,文件上传
服务器图片浏览
这两样工作应该需要3天左右


posted @ 2007-11-30 00:50 hk2000c 阅读(241) | 评论 (0)编辑 收藏

How to configure remote JSP debugging
under WebSphere Application Servers

Introduction

This article describes how you can create a run and debug configuration in IntelliJ IDEA 5.1 (or higher) to allow your Web modules with JSP files to be remotely debugged under WebSphere 5.1 and WebSphere 6.0 application servers. It also describes how to configure these application servers to enable integration with IntelliJ IDEA, and provides you with step-by-step instructions for application deployment.

Requirements

Before making any settings, make sure that the following requirements are met:

  1. WebSphere 5.1 or 6.0 application server is installed (full or express version). IntelliJ IDEA does not have any bundled servers, so you need to install them yourself.
  2. JDK (Sun Java2TM Standard Edition SDK) 1.4 or less is installed. Although IntelliJ IDEA supports JDK 5.0, WebSphere application servers require JDK 1.4 or less.
  3. WebSphere application server is started.

You can find the full documentation on how to install, start, and initially configure the server at http://www-306.ibm.com/software/websphere/.

Configuring WebSphere Application Server

To enable remote debugging on the server, perform the following steps:

  1. Start the server's Administrative Console.
  2. Expand Servers > Application Servers.
    screenshot


    Note:

    WebSphere Application Server V6.0: Administrative Console

  3. Click the server name and clic the Configuration tab
  4. Navigate to the Additional Properties group and click the Debugging Service link.
  5. Make sure that the debugging service is enabled at server startup.
    • For the WebSphere 5.1 application server, select the Startup check box.
      screenshot


      Note:

      WebSphere Application Server - Express 5.1: enabling debugging

    • For the WebSphere 6.0 application server, select the Enable service at server startup check box.

      To the JVM Debug arguments field add the following line before the existing set of arguments: -Dwas.debug.mode=true

      screenshot


      Note:

      WebSphere Application Server 6.0: enabling debugging

  6. Click the Apply button. Then save the settings (for example, using the notification message)
    screenshot


    Note:

    WebSphere Application Server 6.0: notification message

  7. Restart the server using WebSphere's or operating system's administration tools.

Generating the Application Archive File

IntelliJ IDEA can generate the WAR, EAR, or JAR file for your application automatically. For this purpose, you need to configure your module (Web, J2EE Application, or EJB).

  1. In IntelliJ IDEA, click File > Settings (Ctrl + Alt + S), and then select Modules.
  2. In the Modules list, click the module for which you need to generate the archive file.
  3. Click the J2EE Build Settings tab.
  4. Select the Create web module war/ear/jar file check box, and specify the location of the file. This path will be required later, on the application deployment stage.

Apply the changes, and make the project (click the Build > Make Project menu or press Ctrl + F9).

The application is now ready for deployment.

Deploying Application

You deploy the application using the WebSphere application server's tools.

  1. Start the server's Administrative Console.
  2. Click Applications > Install New Application, and then click your server.
  3. Select Local file system or Remote file system depending on your server installation, and then specify the path to your WAR, EAR, or JAR file (usually, it corresponds to the path you specified when generating the application archive file in IntelliJ IDEA).
  4. In the Context Root text box, specify the context (e.g. "myContext").
    screenshot


    Note:

    WebSphere Application Server 6.0: enabling debugging

  5. Click Next. Skip the next screen and click Next again. Note:

    The Security Warning may appear. It displays the content of the was.policy file. You can simply click Continue.

  6. On the first step of the Install New Application wizard, select the Enable class reloading check box, and then click Next. This allows you to redeploy the application without reinstalling it
    screenshot


    Note:

    WebSphere Application Server 6.0: selecting installation options.

  7. On the second step, select the check box next to your Web module to map it to the server. Then click Next.
  8. On the third step, select the check box next to your Web module to map the virtual host to it, and then click Next.
  9. On the last step, just click Finish. The deployment will start. Note:

    The server may not always notify you when the deployment is complete. If the operation takes too long, click Enterprise Applications. If your application is present in the enterprise applications list with the �Stopped� status (red cross icon), the deployment is complete, and you can move to the next step.

  10. When the deployment is finished, the server console will show you the installation results page. Save the configuration, for example using the Save to Master Configuration link on this page.
  11. Click Applications > Enterprise Applications. In the list of applications your application should be displayed with the �Stopped� status (red cross icon).
  12. Select the check box next to your application, and then click Start.
screenshot


Note:

WebSphere Application Server 6.0: starting the application

Creating Run/Debug Configuration

WebSphere 5.1/6.0 application servers are supported by means of the general IntelliJ IDEA integration with the JSR compatible servers and do not have a server-specific type of Run/Debug configuration. You need to create a configuration for a JSR45 compatible server and then modify it according to WebSphere application server's needs.

  1. In IntelliJ IDEA, click the Run > Edit Configurations menu, and then click the JSR45 Compatible Server tab.
  2. Click the plus sign or press Insert to add a new configuration, and then click Remote. Specify some configuration name.
  3. Click the Configure button to add an application server.
  4. In the Application Servers dialog box, click Add, and then specify some server name.
  5. Click the Attach Classes button, and find the j2ee.jar file for your version of WebSphere Application server (<installation folder>\lib\j2ee.jar).
    screenshot


    Note:

    IntelliJ IDEA 5.1: configuring application servers

  6. In the Application Servers dialog box, click Apply to save the changes, and then in the configuration, in the Application Server list, select the added server.
  7. In the Startup page text box, specify the home page of your web application in the following format:
    • WebSphere Application Server 5.1
      http://localhost:7080/<ContextRoot>/<home page>
    • WebSphere Application Server 6.0
      http://localhost:9080/<ContextRoot>/<home page>

    "7080" and "9080" are default ports for the WebSphere 5.1 and 6.0 servers, respectively. If you have changed the default port, specify the actual port number instead.

    <ContextRoot> is name of web context used during application deployment, e.g. "myContext".
    <home page> can be any page from your Web module.

  8. Optionally, you can configure the application server log to display its content in the IntelliJ IDEA's Run tool window, in the console.

    Click the Add button, and then in the Edit Log Files Aliases dialog specify some alias name (e.g. "activity"). In the Log File Location text box, specify the location of the activity.log file. By default, this file is located in the <home>\logs folder.

  9. In the JSP package text box, enter one of the following:
    • WebSphere Application Server 5.1
      org.apache.jsp
    • WebSphere Application Server 6.0
      com.ibm._jsp
  10. In the VM options variable text box, type DEBUG.
  11. If you use WebSphere Application Server 5.1, select to use the specific JSP's line mapping model. For the WebSphere Application Server 6.0 this check box is ignored, but it is recommended to clear it.
  12. In the Port text box, specify the port number of the corresponding application server (it should be the same as specified for the startup page).
    screenshot


    Note:

    IntelliJ IDEA 5.0: Run/Debug configuration for WebSphere Application Server 6.0

  13. Click the Startup/Connection tab, and then click Debug. Check that the Socket option is selected, and the Port number is 7777.

Debugging Application

To start debugging the application in IntelliJ IDEA, just click Run > Debug or press Shift + F9. You can use all the power of the IntelliJ IDEA debugger for the JSP files running on the WebSphere Application Server, including breakpoints, stepping commands, watches, etc.

Note:

To apply any changes in the web module, you will need to redeploy the application as described earlier in this article. The "Build on frame deactivation" feature is not supported for remote debugging.

Troubleshooting

The server crashes during the debug session

Such problems may appear if you use the IBM's virtual machine supplied with the WebSphere server. Possible solutions in this case are:

  • Run IntelliJ IDEA with the command line option:
    -Didea.debugger.keep.temp.objects=false
    In this case IntelliJ IDEA will avoid calling certain methods from debug API.
  • Use Sun's JDK 1.4 or less

IntelliJ IDEA is unable to open the debugger port

In most cases, it is an internal WebSphere server error. Try to restart the WebSphere application server.

Conclusion

We hope that this article helped you to configure remote debugging successfully. If you have encountered any problems that are not described in the Troubleshooting section, don't hesitate to contact our technical support: support@jetbrains.com.

posted @ 2007-11-20 21:56 hk2000c 阅读(906) | 评论 (0)编辑 收藏

Enter your name: Daniel
Your key is: 22457-MWPIW-INDLX-2PU2X-WBHSX-C4YK0

posted @ 2007-11-20 19:07 hk2000c 阅读(220) | 评论 (0)编辑 收藏

Hibernate 1.2.3 has built-in support for blobs. Hibernate natively maps blob columns to java.sql.Blob. However, it's sometimes useful to read the whole blob into memory and deal with it as a byte array.

One approach for doing this to create a new UserType as follows.

package mypackage;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Blob;
import cirrus.hibernate.Hibernate;
import cirrus.hibernate.HibernateException;
import cirrus.hibernate.UserType;
public class BinaryBlobType implements UserType
{
public int[] sqlTypes()
{
return new int[] { Types.BLOB };
}
public Class returnedClass()
{
return byte[].class;
}
public boolean equals(Object x, Object y)
{
return (x == y)
|| (x != null
&& y != null
&& java.util.Arrays.equals((byte[]) x, (byte[]) y));
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException
{
Blob blob = rs.getBlob(names[0]);
return blob.getBytes(1, (int) blob.length());
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException
{
st.setBlob(index, Hibernate.createBlob((byte[]) value));
}
public Object deepCopy(Object value)
{
if (value == null) return null;
byte[] bytes = (byte[]) value;
byte[] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);
return result;
}
public boolean isMutable()
{
return true;
}
}

The BinaryBlobType will convert a blob into a byte array and back again.

Here's how to use it. First, define an entity that contains a byte[] property:

public class ImageValue
{
private long id;
private image byte[];
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public byte[] getImage() { return image; }
public void setImage(byte[] image) { this.image = image; }
}

Then map a blob column onto the byte[] property:

<class name="ImageValue" table="IMAGE_VALUE">
<id name="id/>
<property name="image" column="IMAGE" type="mypackage.BinaryBlobType"/>
</class>

Notes:

1) Blobs aren't cachable. By converting the blob into a byte array, you can now cache the entity.

2) This approach reads the whole blob into memory at once.

3) The above type is known to work for reading blobs out of the db. Other usage patterns might also work.

Comments (GK)

I changed isMutable() to return true, since an array is a mutable object.

The use of setBlob() will work on some drivers, but not all. I think its more portable to use setBytes() or even setBinaryStream().

comments (db)

db's comment above was right, setBlob() didn't work on Oracle, I used setBytes().

comments (Chad Woolley)

Below is a modified nullsafeset() that i needed to use to get it to work with tomcat 4.1.27 & oracle 8/9i - the normal calls don't work through the tomcat/dbcp connection pool wrapper objects... (this caused me great pain)

pls note that the setBytes() doesn't seem to work with oracle driver & hibernate

d.birch@eclipsegroup.com.au

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException
{
if(st instanceof org.apache.commons.dbcp.DelegatingPreparedStatement &&
((org.apache.commons.dbcp.DelegatingPreparedStatement)st).getDelegate()
instanceof oracle.jdbc.OraclePreparedStatement)
{
oracle.sql.BLOB blob = oracle.sql.BLOB.createTemporary(
((org.apache.commons.dbcp.PoolableConnection)st.getConnection()).getDelegate(), false, oracle.sql.BLOB.DURATION_SESSION);
blob.open(BLOB.MODE_READWRITE);
OutputStream out = blob.getBinaryOutputStream();
try
{
out.write((byte[])value);
out.flush();
out.close();
}
catch(IOException e)
{
throw new SQLException("failed write to blob" + e.getMessage());
}
blob.close();
((oracle.jdbc.OraclePreparedStatement)((org.apache.commons.dbcp.DelegatingPreparedStatement)st).getDelegate()).setBLOB(index, blob);
}
else
{
st.setBlob(index, Hibernate.createBlob((byte[]) value));
}
}
//and.. note the null check, oracle drivers return a null blob...
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException
{
final Blob blob = rs.getBlob(names[0]);
return blob != null?blob.getBytes(1, (int)blob.length()):null;
}

/ comments Vanitha

I had to use the user type to save pdfs as oraBLOBs in oracle 91 database. nullsafeSet

needed a sligh modification , or else ther was a classcastexception. Used oracle Blob instead of Hibernate Blob type and it works.

 public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException
{
oracle.sql.BLOB t_blob = oracle.sql.BLOB.createTemporary(((org.jboss.resource.adapter.jdbc.WrappedConnection) st.getConnection()).getUnderlyingConnection(),
false, oracle.sql.BLOB.DURATION_SESSION);
OutputStream t_out = null;
t_blob.open(BLOB.MODE_READWRITE);
t_out = t_blob.getBinaryOutputStream();
try
{
t_out.write((byte[]) value);
t_out.flush();
t_out.close();
}
catch (IOException e)
{
throw new SQLException("failed write to blob" + e.getMessage());
}
t_blob.close();
st.setBlob(index, t_blob);
}

</code>

posted @ 2007-11-16 17:46 hk2000c 阅读(1377) | 评论 (0)编辑 收藏

仅列出标题
共11页: 上一页 1 2 3 4 5 6 7 8 9 下一页 Last