2014年3月26日 星期三

Hibernate HQL examples


The Hibernate Query Language (HQL) is similar to SQL. When compared to SQL, HQL is completely Object Oriented and hence it uses class names in the place of table names and property names in the place of column names. HQL understands inheritance, polymorphism and association. HQL is not case sensitive.

Let us go the package com.demo.HQLexamples and look into HQLexampleExecutor.java

HQL From Clause:

List studentList = session.createQuery(" from Student ").list();
for(Object stud : studentList){
System.out.println("Student Name is "+ ((Student)stud).getStudentName());
Set courses = ((Student)stud).getCourses();
System.out.println("Courses to which this student is associated");
for(Object course: courses)
System.out.println(((Course)course).getCourseName());
}



* The session.createQuery creates an instance of type org.hibernate.Query and the list() method of Query interface is actually the place where the execution of query starts.
* The from clause returns all the instances of a class.
* Objects can be retrieved by iterating the list.

HQL Joins:

Hibernate supports inner join, left outer join, right outer join, full join. In the following example, the query returns an array of objects ( the parent and its associated objects). Pay attention while iterating the list as follows.

List joinList = session.createQuery("from Student as s inner join s.courses").list();
Iterator ite = joinList.iterator();
while(ite.hasNext()){
Object [] objects = (Object []) ite.next();
Student student = (Student)objects[0];
Course course = (Course)objects[1];
System.out.println("Student Name " + student.getStudentName());
System.out.println("Course Name "+ course.getCourseName());
}


HQL Select Clause:

The select clause actually decides which objects and properties to return in the query result set. In the following example it returns only the Student objects and not the Course objects even though they are associated with the Student objects.

List selectList = session.createQuery("select distinct s from Student as s inner join s.courses").list(); //now only Student object is returned and not the Course object
for(Object sl : selectList)
System.out.println("Student Name "+ ((Student)sl).getStudentName());


HQL Aggregate functions:

In HQL, the aggregate functions like sum(), max(), min(), count() can be applied the properties and the results can be returned in the queries.

List aggreList = session.createQuery("select count(distinct s.studentName) from Student as s inner join s.courses").list();
System.out.println("Total Number of Students "+ aggreList.toString());

HQL Where Clause:
The where clause is used to filter the list of objects returned. Have a look at the syntax for the place holder in the where clause (:sname).

List whereList = session.createQuery("select distinct s from Student as s inner join s.courses where s.studentName= :sname").setParameter("sname","Prathap Kumar").list();//The where clause allows you to refine the list of instances returned.
for(Object wl: whereList)
System.out.println("Student Id is "+ ((Student)wl).getId());


HQL Order by Clause:
The objects returned in the query can be ordered on any property or component.

List orderList = session.createQuery("select s from Student s order by s.studentName asc").list();
for(Object ol : orderList)
System.out.println("Student name in ascending order "+ ((Student)ol).getStudentName());


HQL Group By Clause:

The aggregate values returned from a query can be grouped on any property or component.

List groupList = session.createQuery("select c.courseName, count(distinct c.courseName) from Student as s, Course c inner join s.courses group by c.courseName").list(); //A query that returns aggregate values can be grouped by any property of a returned class or components
ite = groupList.iterator();
while(ite.hasNext()){
Object [] objects = (Object []) ite.next();
System.out.println("Course Name is "+ objects[0]);
System.out.println("Count representation of the above course "+ objects[1]);
}

HQL Subqueries:
Hibernate supports subqueries within queries for the databases that allows subselects.
A subquery must be surrounded by parentheses.

List subList = session.createQuery("select s from Student s where s.studentName not in (select distinct s from Student as s inner join s.courses where s.studentName= 'Sekar')").list();
System.out.println("List size "+ subList.size());
for(Object sl: subList)
System.out.println("Student Name from the sub query is "+ ((Student)sl).getStudentName());


Native SQL Queries:

Hibernate also supports native SQL queries. We can directly use the native SQL queries in the place of HQL queries with one minor change. The methodsession.
createSQLQuery() should be used instead of session.createQuery() as follows.

List nativeSqlList = session.createSQLQuery("select * from STUDENTS").list(); // STEDENTS refer the actual table name
ite = nativeSqlList.iterator();
while(ite.hasNext()){
Object [] objects = (Object []) ite.next();
System.out.println("Student Id "+ objects[0]);
System.out.println("Student name "+ objects[1]);
}

The result set of a native SQL query can be converted into entity or object of a specific class by invoking a method addEntity() as follows.

List nativeRsHandlinglList = session.createSQLQuery("select * from COURSES").addEntity(Course.class).list(); // COURSES refer the actual table name
for(Object nativeList : nativeRsHandlinglList)
System.out.println("Course name is "+ ((Course)nativeList).getCourseName());

Named HQL:

The HQL queries can be named and embedded into the hibernate mapping file and can be retrieved using their respective names. The scope of the names of the HQL queries are globally visible.

Have a look at the namedQueries.hbm.xml file in the package com.demo.HQLexamples.

<query name="namedStudentHql"><![CDATA[ from Student s where s.studentName= :sname]]></query>

List namedHqlList = session.getNamedQuery("namedStudentHql").setString("sname", "Joseph Raj Kumar").list();
for(Object namedhql: whereList)
System.out.println("Student Id is "+ ((Student)namedhql).getId());

Named SQL:

Just as the HQL named queries, the SQL queries can also be named and retrieved by name in the same fashion.

<sql-query name="namedNativeStudentSQL"><![CDATA[select * from STUDENTS]]></sql-query>

List namedSqlList = session.getNamedQuery("namedNativeStudentSQL").list();
ite = nativeSqlList.iterator();
while(ite.hasNext()){
Object [] objects = (Object []) ite.next();
System.out.println("Student Id "+ objects[0]);
System.out.println("Student name "+ objects[1]);

2014年3月24日 星期一

《大行報告》匯豐升電盈(00008.HK)目標價至4.35元 評級「增持」

匯豐表示,電信市場整合的好處,已很大程度反映於香港電訊(06823.HK)及數碼通(00315.HK)之股價,惟電盈(00008.HK)較低之再投資風險,則未有完全反映於電盈股價,故重申電盈「增持」評級,目標價由4元上調至4.35元。
該行指,電盈在2012年分拆香港電訊以去槓桿化,現決定收購CSL及重新進入無線市場,意味其策略起了顯著變化。今年1月,該行調整兩間公司的盈測和評級,認為香港電訊風險正在增加,而電盈正在減少。另外,值得注意的是,電盈幾乎沒有債務。(na/m)

2014年3月22日 星期六

VMWare中安装Mac OS Lion和XCode的注意事项

1. MacOS安装
a) 请使用VMWare 7.1.x,我用的是7.1.4,注册码是网上找的。
b) 为了顺利安装VMWare补丁,一定要将VMWare装在C盘默认位置。一般为 C:\program files\VMWare\VMWare Workstation
c) 在安装MacOS前,必须对VMWare进行打补丁,目的是让Windows的平台下的VMWare能够运行MacOS。否则:在出现MacOS启动屏幕数秒后虚拟机运行窗口会自动关闭,并在右下角提示“The guest operating system is not Mac OS X Server.”

2. VMWare Tools的安装
a) 为了性能考虑,必须安装VMWare Tools。该软件在darwin.iso中。请使用VMWare提供的挂在工具,将其作为CDROM挂在到Mac中,进入并安装。
b) Mac中的用户名为MagicStudio,安装密码为sssssssss   (9个s)  

3. 显示补丁
a) 为了解决花屏问题,必须安装显示补丁。安装方法,下载补丁,然后将其放入到VMWare的共享文件夹中,然后在Mac中打开并安装。注意:共享文件夹路径中不能出现中文!!!

4. 让你的XCode支持iOS 5.0以上的硬件调试,请下载XCode 4.3版
a) 最开始的时候我下载了XCode 4.1,刚才才发现,十分傻逼。傻逼有2:其一,XCOde 4.1超级大,4GB多, 其二,XCode 4.1 preview版本,只支持iOS 4.3及以下版本,如果你想测试iOS 5.0以上的硬件,没戏。XCode 4.2大小才1GB多点,支持iOS 5.0以上版本。
b) 如果你安装的Mac是Mac Lion 10.7版本,请安装XCode 4.2,不要下载XCode 4.3,因为后者要求系统版本必须为10.7.x以上,可能会给你带来麻烦。

对于最新买到的ipad,iOS版本基本都是5.0以上。千万注意。XCode 4.3官方地址:https://developer.apple.com/downloads/index.action#

5. 虚拟机里面测试硬件,报告找不到provision
a) 请先切换到VMWare中的iOS,然后再插入ipad到USB,这样XCode才能探测到ipad的插入,才能将app直接丢到pad上执行。另外,可能需要在iTunes里面对pad授权一下?不确定这一步,反正我做了。


OK~


附录(列出各个文件的关键字,便于搜索下载):
1. VMWare补丁: macosx_guest_vmware_7.tar.gz
2. Mac显示器花屏补丁:VMDrivers_Mac.rar
3. Mac Lion版本: Mac.OSX.10.7(纯净OS)-VM.rar
4. VMWare虚拟机:VMware-workstation-full-7.1.4
5. XCode 4.2

我用了2核,2G内存跑虚拟机,不卡。编译一个XCode的iOS默认demo,很快。


一个很不错的iOS编程开发教程:
相比于自己去辛辛苦苦网上搜集乱七八糟的资料,这里效率要高太多!



iOS开发资源:


iOS 5 开发的好东西【本地下载】 http://bbs.weiphone.com/read-htm-tid-4008975.html


下载要顶及加分呀
很多帖子都有 iOS 4 开发的好东西
但只有这里是【本地下载】及有【源代码】


基础类 Beginning iPhone 4 Development Exploring the iOS SDK


pdf + 源代码:  Beginning iPhone 4 Development Exploring the iOS SDK+code.rar (12021 K) 下载次数:7052 

pdf:  Beginning iPhone 4 Development Exploring the iOS SDK.pdf (11666 K) 下载次数:3105 





基础类 iOS SDK Programming : A Beginner's Guide


rar 压缩:  iOS SDK Programming A Beginners Guide.pdf.rar (12061 K) 下载次数:1155 

pdf 版本:  iOS SDK Programming A Beginners Guide.pdf (14411 K) 下载次数:1625 

源代码 :  iOS SDK Sample Code.zip (13446 K) 下载次数:1604 






基础类 Programming iOS 4: Fundamentals of iPhone, iPad, and iPod touch Development


pdf + 源代码:  Programming iOS 4+code.rar (12203 K) 下载次数:1185 

源代码 :  Programming-iOS-4-Book-Examples-09d82b5.zip (3343 K) 下载次数:1291 

 




基础类 From Idea to App: Creating iOS UI, animations, and gestures


pdf + 源代码:  From Idea to App+code.rar (8053 K) 下载次数:3139 

pdf 版本:  From Idea to App.pdf (5131 K) 下载次数:622 

源代码 :  From Idea to App_code.rar (5275 K) 下载次数:650 






ObjC 基础类 Programming in Objective-C, 3rd Edition


pdf 版本:   Programming in Objective-C 3rd Edition.pdf (4505 K) 下载次数:3966 

源代码: http : // classroom m.com/objective-c/index.php?board=56.0






基础类 iOS Programming: The Big Nerd Ranch Guide, 2nd Edition


pdf 版本:   iOS Programming 2nd Edition.pdf (14432 K) 下载次数:1121 

源代码 :   iOSProgramming.zip (6881 K) 下载次数:1406 






实用类 iOS 4 Programming CookBook


pdf + 源代码:  iOS4ProgrammingCookBook+code.rar (11197 K) 下载次数:2033 

pdf 版本:  iOS4ProgrammingCookBook.pdf (7918 K) 下载次数:1031 

源代码 :  iOS4ProgrammingCookBook_code.rar (5763 K) 下载次数:965 






实用类 Tips and Tricks for Awesome iPhone and iPad Apps


pdf + 源代码 :  iOS Recipes Tips and Tricks for Awesome iPhone and iPad Apps.pdf+code.rar (11068 K) 下载次数:1426 

pdf 版本:  iOS Recipes Tips and Tricks for Awesome iPhone and iPad Apps.pdf (1290 K) 下载次数:440 

源代码 :  cdirec-code.zip (10513 K) 下载次数:802 






专题类 Advanced iOS 4 Programming


pdf 版本:  Advanced iOS 4 Programming.pdf (3431 K) 下载次数:1434 

源代码 :  Advanced iOS 4 Programming_code.rar (15769 K) 下载次数:1033 






专题类 Concurrent Programming in Mac OS X and iOS


1. Introducing Block Objects
2. Programming Grand Central Dispatch 
pdf + 源代码:  Concurrent Programming in Mac OS X and iOS+code.rar (3326 K) 下载次数:1362 

pdf 版本:   Concurrent Programming in Mac OS X and iOS.pdf (3975 K) 下载次数:429 

源代码 :  Concurrent Programming_code.zip (536 K) 下载次数:400 






专题类 Core Data iOS Essentials


rar 压缩:  Core Data iOS Essentials.pdf.rar (8616 K) 下载次数:1820 

pdf 版本:  Core Data iOS Essentials.pdf (11224 K) 下载次数:1190 

源代码 :  Core Data iOS Essentials_Code.zip (11325 K) 下载次数:1941 





专题类 Pro Core Data for iOS


pdf + 源代码:  Pro Core Data for iOS+code.rar (12890 K) 下载次数:2413 

pdf 版本:  Pro Core Data for iOS.pdf (7085 K) 下载次数:658 

源代码 :  Pro Core Data for iOS_code.zip (6751 K) 下载次数:563 






专题类 Graphics and Animation on iOS


rar 压缩:  Graphics and Animation on iOS.pdf.rar (3738 K) 下载次数:1539 

pdf 版本:  Graphics and Animation on iOS.pdf (5267 K) 下载次数:632 

源代码 :  Chapter_01.zip (2055 K) 下载次数:2781 






专题类 Pro Objective-C Design Patterns for iOS


pdf + 源代码:  Pro Objective-C Design Patterns for iOS+code.rar (4573 K) 下载次数:1555 

pdf 版本:  Pro Objective-C Design Patterns for iOS.pdf (3563 K) 下载次数:563 

源代码 :  Pro Objective-C Design Patterns for iOS_code.zip (1557 K) 下载次数:882 





游戏类 Learning Cocos2D



pdf 版本:   Learning Cocos2D.pdf (14667 K) 下载次数:1702 

源代码 : http://cocos2dbook.com/projects/LearningCocos2D_Resources_SourceCode_v1_1.dmg



Learn iPhone and iPad Cocos2D Game Development, Cocos2d for iPhone 0.99 Beginners Guide 书及源代码



【官方下载链接】



Xcode 4.1 and iOS SDK 4.3.x for Lion (要求 10.7)【官方下载链接, 下载需要开发者帐号】

Xcode 4.0.2 and iOS SDK 4.3.2 for Snow Leopard (要求 10.6.6)【官方下载链接, 下载需要开发者帐号】

Xcode 3.2.6 and iOS SDK 4.3.1 for Snow Leopard (要求 10.6.6)【官方下载链接, 下载需要开发者帐号】
Xcode 3.2.6 and iOS SDK 4.3.1 for Snow Leopard (要求 10.6.6)【官方下载链接, 下载需要开发者帐号】

Xcode 3.2.5 and iOS SDK 4.2 for Snow Leopard (要求 10.6.4)【官方下载链接, 下载需要开发者帐号】
Xcode 3.2.4 and iOS SDK 4.1 for Snow Leopard (要求 10.6.4)【官方下载链接, 下载需要开发者帐号】
Xcode 3.2.3 and iPhone SDK 4 Final for Snow Leopard (要求 10.6.2)【官方下载链接, 下载需要开发者帐号】



The Objective-C Programming Language (pdf)【官方 pdf 链接】
Object-Oriented Programming with Objective-C (pdf)【官方 pdf 链接】
Cocoa Fundamentals Guide (pdf)【官方 pdf 链接】
iOS Application Programming Guide (pdf)【官方 pdf 链接】
View Programming Guide for iOS (pdf)【官方 pdf 链接】
View Controller Programming Guide for iOS (pdf)【官方 pdf 链接】
Table View Programming Guide for iOS (pdf)【官方 pdf 链接】
Scroll View Programming Guide for iOS (pdf)【官方 pdf 链接】
Collections Programming Topics (pdf)【官方 pdf 链接】
Text, Web, and Editing Programming Guide for iOS (pdf)【官方 pdf 链接】
Camera Programming Topics for iOS (pdf)【官方 pdf 链接】
Address Book Programming Guide for iOS (pdf)【官方 pdf 链接】
Event Kit Programming Guide (pdf)【官方 pdf 链接】
Local and Push Notification Programming Guide (pdf)【官方 pdf 链接】
System Messaging Programming Topics for iOS (pdf)【官方 pdf 链接】
Core Data Programming Guide (pdf)【官方 pdf 链接】
Core Animation Programming Guide (pdf)【官方 pdf 链接】
Quartz 2D Programming Guide (pdf)【官方 pdf 链接】
OpenGL ES Programming Guide for iOS (pdf)【官方 pdf 链接】

iOS Debugging Magic http://developer.apple.com/library/ios/#technotes/tn2239/_index.html
pdf   iOS Debugging Magic.pdf (388 K) 下载次数:455 


iOS 3 的好东西
iPhone SDK 3.1.3 with XCode 3.1.4 for Leopard (要求 10.5.7) 【官方下载链接】
iPhone SDK 3.1.3 with XCode 3.2.1 for Snow Leopard (要求 10.6.0) 【官方下载链接】
iPhone SDK 3.2 Final with Xcode 3.2.2 for Snow Leopard (要求 10.6.0)【官方下载链接】

[中文扫描版]Objective-C.2.0 程序设计(原书第2版) + iPhone 3开发基础教程
Learn iPhone and iPad Cocos2D Game Development, Cocos2d for iPhone 0.99 Beginners Guide 书及源代码
7本开发 iPhone OpenGL 游戏好东西
20多本+4视频 开发iPhone好东西【本地下载】