1. 程式人生 > >hibernate

hibernate

可能 port src query rac session join() osi function

  1. package com.tudou.hibernates.t1;
  2. import java.util.List;
  3. import org.hibernate.Query;
  4. import org.hibernate.Session;
  5. import org.hibernate.SessionFactory;
  6. import org.hibernate.Transaction;
  7. import org.hibernate.cfg.Configuration;
  8. public class TestGetHql {
  9. private static Configuration cfg = new Configuration().configure();
  10. private static SessionFactory fac = cfg.buildSessionFactory();
  11. private static Session son = fac.openSession();
  12. // hql普通查詢 Card為類名,不是表名,可以寫全路徑
  13. public static void from() {
  14. String hql = "from Card";
  15. Query query = son.createQuery(hql);
  16. List<Card> cards = query.list();
  17. for (Card c : cards) {
  18. System.out.println(c.getCardName());
  19. System.out.println(c.getCreateDate());
  20. }
  21. }
  22. // 條件查詢 where
  23. public static void where() {
  24. String hql = "from Card where cardName=‘三國無雙‘";
  25. Query query = son.createQuery(hql);
  26. List<Card> cardss = query.list();
  27. for (Card c : cardss) {
  28. System.out.println(c.getCardName());
  29. System.out.println(c.getCreateDate());
  30. }
  31. }
  32. // 模糊查詢 like
  33. public static void like() {
  34. String hql = "from Card where cardName like ‘%世%‘";
  35. Query query = son.createQuery(hql);
  36. List<Card> cards = query.list();
  37. for (Card c : cards) {
  38. System.out.println(c.getCardName());
  39. System.out.println(c.getCreateDate());
  40. }
  41. }
  42. // 邏輯條件查詢 >
  43. public static void gt() {
  44. String hql = "from Card c where c.createDate >‘2011-08-08‘";
  45. Query query = son.createQuery(hql);
  46. List<Card> cards = query.list();
  47. for (Card c : cards) {
  48. System.out.println(c.getCardName());
  49. System.out.println(c.getCreateDate());
  50. }
  51. }
  52. // 邏輯條件查詢 between and 此處用了別名,省略了as關鍵字
  53. public static void between() {
  54. String hql = "from Card c where c.createDate between ‘2011-08-08‘ and ‘2022-11-11‘";
  55. Query query = son.createQuery(hql);
  56. List<Card> cards = query.list();
  57. for (Card c : cards) {
  58. System.out.println(c.getCardName());
  59. System.out.println(c.getCreateDate());
  60. }
  61. }
  62. // 邏輯多條件查詢and
  63. public static void and() {
  64. String hql = "from Card c where c.createDate between ‘2011-01-08‘ and ‘2022-11-11‘ and c.cardName like ‘%世%‘";
  65. Query query = son.createQuery(hql);
  66. List<Card> cards = query.list();
  67. for (Card c : cards) {
  68. System.out.println(c.getCardName());
  69. System.out.println(c.getCreateDate());
  70. }
  71. }
  72. // update 更新
  73. public static void update() {
  74. String hql = "update Card as c set c.createDate=‘2011-03-03‘ where c.cardType.cardTypeId=3";
  75. Query query = son.createQuery(hql);
  76. int num = query.executeUpdate();
  77. System.out.println(num + "行被更新。。。");
  78. }
  79. // delete刪除
  80. public static void delete() {
  81. String hql = "delete from Card as c where c.createDate=‘2011-03-04‘";
  82. Query query = son.createQuery(hql);
  83. int num = query.executeUpdate();
  84. System.out.println(num + "行被刪除。。。");
  85. }
  86. // 單個屬性查詢
  87. public static void simpleProperty() {
  88. String hql = "select c.cardName from Card as c where c.cardType.cardTypeId=1";
  89. Query query = son.createQuery(hql);
  90. List<String> name = query.list();
  91. for (String s : name) {
  92. System.out.println(s);
  93. }
  94. }
  95. // 多個屬性查詢 其中cardTypeName直接通過card對象的cardType對象獲得,省去了使用普通的sql語句必須多表連接查詢的麻煩
  96. public static void mulProperty() {
  97. String hql = "select c.cardName,c.cardType.cardTypeName,c.createDate from Card as c where c.cardType.cardTypeId=1";
  98. Query query = son.createQuery(hql);
  99. List<Object[]> obj = query.list();
  100. for (Object[] o : obj) {
  101. System.out.println(o[0] + "\t" + o[1] + "\t" + o[2]);
  102. }
  103. }
  104. // 多個屬性查詢 面向對象方式
  105. public static void orientedObject() {
  106. String hql = "select new Card(c.cardName,c.createDate) from Card as c";
  107. Query query = son.createQuery(hql);
  108. List<Card> cards = query.list();
  109. for (Card c : cards) {
  110. System.out.println(c.getCardName() + "\t" + c.getCreateDate());
  111. }
  112. }
  113. // 函數查詢
  114. public static void function() {
  115. String hql = "select count(*),max(c.createDate) from Card as c";
  116. Query query = son.createQuery(hql);
  117. List<Object[]> oo = query.list();
  118. for (Object[] o : oo) {
  119. System.out.println("總記錄數:" + o[0] + "\t最新日期為:" + o[1]);
  120. }
  121. }
  122. // 排序
  123. public static void orderBy() {
  124. String hql = "from Card as c order by c.createDate desc";
  125. Query query = son.createQuery(hql);
  126. List<Card> cards = query.list();
  127. for (Card c : cards) {
  128. System.out.println(c.getCardName() + "\t" + c.getCreateDate());
  129. }
  130. }
  131. // 分組
  132. public static void groupBy() {
  133. String hql = "from Card as c group by c.cardType.cardTypeId";
  134. Query query = son.createQuery(hql);
  135. List<Card> cards = query.list();
  136. for (Card c : cards) {
  137. System.out.println(c.getCardName() + "\t" + c.getCreateDate());
  138. }
  139. }
  140. // 單個對象查詢 呵呵,奇怪吧,對象可以查詢出來
  141. public static void simpleObject() {
  142. String hql = "select c.cardType from Card as c";
  143. Query query = son.createQuery(hql);
  144. query.setMaxResults(1);// 必須在查詢之前指定,使其返回單個對象
  145. CardType cardType1 = (CardType) query.uniqueResult();
  146. System.out.println(cardType1.getCardTypeName() + "\t"
  147. + cardType1.getCreateDate());
  148. }
  149. // 按照命令行參數 格式為: :參數名
  150. public static void parameter() {
  151. String hql = "select c.cardType from Card as c where c.cardType.cardTypeId=:id";
  152. Query query = son.createQuery(hql);
  153. query.setParameter("id", 1);
  154. query.setMaxResults(1);// 必須在查詢之前指定,使其返回單個對象
  155. CardType cardType = (CardType) query.uniqueResult();
  156. System.out.println(cardType.getCardTypeName() + "\t"
  157. + cardType.getCreateDate());
  158. }
  159. // 按照參數位置 從0開始
  160. public static void parameterPosition() {
  161. String hql = "select c.cardType from Card as c where c.cardType.cardTypeId=?";
  162. Query query = son.createQuery(hql);
  163. query.setParameter(0, 1);
  164. query.setMaxResults(1);// 必須在查詢之前指定,使其返回單個對象
  165. CardType cardType = (CardType) query.uniqueResult();
  166. System.out.println(cardType.getCardTypeName() + "\t"
  167. + cardType.getCreateDate());
  168. }
  169. // 多個參數
  170. public static void mulParameter() {
  171. String hql = "from Card as c where c.cardType.cardTypeId in (3,2)";
  172. Query query = son.createQuery(hql);
  173. // query.setParameterList("id", new Object[]{1,2});
  174. List<Card> cards = query.list();
  175. for (Card o : cards) {
  176. System.out.println(o.getCardName());
  177. }
  178. }
  179. // inner join 查詢結果為多個對象的集合
  180. public static void innerJoin() {
  181. String hql = "from Card as c inner join c.cardType";
  182. Query query = son.createQuery(hql);
  183. List<Object[]> cards = query.list();
  184. for (Object[] o : cards) {
  185. System.out.println(((Card) o[0]).getCardName() + "\t"
  186. + ((CardType) o[1]).getCreateDate());
  187. }
  188. }
  189. // leftJoin 查詢結果為多個對象的集合
  190. public static void leftJoin() {
  191. String hql = "from CardType as c left join c.cards";
  192. Query query = son.createQuery(hql);
  193. List<Object[]> cards = query.list();
  194. for (Object[] o : cards) {
  195. // 由於保存卡片時在多的一方card進行操作,使用了級聯。但手動插入的cardType可能沒有相應的卡片
  196. if (o[1] != null) {// 當卡片不為空時
  197. System.out.println(((CardType) o[0]).getCardTypeName() + "\t"
  198. + ((Card) o[1]).getCardName());
  199. } else {
  200. System.out.println(((CardType) o[0]).getCardTypeName()
  201. + "\t沒有相應的卡片");
  202. }
  203. }
  204. }
  205. // rightJoin 查詢結果為多個對象的集合
  206. public static void rightJoin() {
  207. String hql = "from CardType as c right join c.cards";
  208. Query query = son.createQuery(hql);
  209. List<Object[]> cards = query.list();
  210. // 插入時保證了每張卡片的類型,所以此處不用判斷卡片類型是否為空
  211. for (Object[] o : cards) {
  212. System.out.println(((CardType) o[0]).getCardTypeName() + "\t"
  213. + ((Card) o[1]).getCardName());
  214. }
  215. }
  216. // 使用子查詢
  217. public static void childSelect() {
  218. String hql = "from CardType as c where (select count(*) from c.cards)>0";
  219. Query query = son.createQuery(hql);
  220. List<CardType> cards = query.list();
  221. for (CardType c : cards) {
  222. System.out.println(c.getCardTypeName() + "\t" + c.getCreateDate());
  223. }
  224. }
  225. // 程序入口
  226. public static void main(String[] args) {
  227. // 測試方法
  228. Transaction tr = son.beginTransaction();
  229. // update();
  230. mulParameter();
  231. tr.commit();
  232. son.close();
  233. fac.close();
  234. }
  235. }
技術分享 [java] view plain copy print?

hibernate投影查詢:

String hql="select u.id,u.name from user as u" //1方法

String hql="select new com.mypack.User2(u.id,u.name) from User as u " //2方法

Iterator userHQL= session.createQuery(hql).iterate();

while (userHql.hasNext()) {
User user=(User)userHql.next();

Object[] row=(Object[]) userHql.next();
int id=(Integer)row[0];
String nameString =(String)row[1];
System.out.print(id+"\t"+nameString);

//User2 user2=(User2) userHql.next();
//System.out.println(user2.getId()+"\t\t"+user2.getName());

}

--------------------------------------------------

package com.mypack;

public class User2 {
private int id;
private String name;
public User2(int id,String nameString){
this.id=id;
this.name=nameString;
}

......省略了屬性的get,set方法

}

hibernate