JAVA開発メモ
Hibernate の変更点
 

[リロード]   [新規|編集|差分|添付]   [トップ|一覧|単語検索|最終更新|バックアップ|ヘルプ]

サイトメニュー

  • 追加された行はこの色です。
  • 削除された行はこの色です。
  • Hibernate へ行く。

  
  #amazonkey2(hibernate)
  
  
  DB2,FrontBase,HSQLDB,informix,interbase,MS SQL server,MySQL, Oracle,Pointbase,PostgreSQL,Sybase etc.
  
  
  -[[Working with Hibernate in Eclipse:http://www.onjava.com/pub/a/onjava/2004/06/23/hibernate.html]]
  
  
   package test;
   import java.util.List;
   import java.util.Properties;
   import net.sf.hibernate.Hibernate;
   import net.sf.hibernate.HibernateException;
   import net.sf.hibernate.Session;
   import net.sf.hibernate.Transaction;
   import net.sf.hibernate.cfg.Configuration;
   import net.sf.hibernate.expression.Expression;
  
   public class SampleMain {
  
       public static void main(String[] args) {
           Configuration cfg = null;
           Session session = null;
           Transaction transaction = null;
           Properties props = new Properties();
  
           try {
               cfg = new Configuration().addClass(Person.class).addProperties(props);
               session = cfg.buildSessionFactory().openSession();
               //session.setFlushMode(FlushMode.COMMIT);
               transaction = session.beginTransaction();
  
               Person person = new Person();
  
               Long id = (Long) session.save(person);
  
               Person load = (Person) session.load(Person.class, id);
               System.out.println(load);
  
               session.update(person);
  
  
               List list = session.find("from Person where id=?", id, Hibernate.LONG);
               System.out.println(list);
               List list2 =
                   session.createCriteria(Person.class).add(Expression.eq("id", id)).list();
               System.out.println(list2);
  
               session.delete(person);
  
               list = session.find("from Person");
               System.out.println(list);
  
               transaction.commit();
  
           } catch (Exception e) {
               try {
                   if (transaction != null)
                       transaction.rollback();
               } catch (Exception e1) {
                   e1.printStackTrace();
               }
               e.printStackTrace();
           } finally {
               try {
                   if (session != null && session.isOpen())
                       session.close();
               } catch (HibernateException e1) {
                   e1.printStackTrace();
               }
           }
  
       }
   }
   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   <hibernate-mapping>
     <class name="test.Person" table="PERSON">
       <id name="id" column="ID" type="long">
         <generator class="native"/>
       </id>
       <property name="name" column="NAME" type="string" length="20" not-null="true"/>
     </class>
   </hibernate-mapping>
  
   Person load = (Person) session.load(Person.class, id, LockMode.UPGRADE);
  
   List list2 = session.createCriteria(Person.class)
                       .add(Expression.eq("id", id))
                       .setLockMode(LockMode.UPGRADE)
                       .list();
  
  ~
  #amazonkey2(JAVA DB)

リロード   新規 編集 差分   トップ 一覧 検索 最終更新 バックアップ   ヘルプ   最終更新のRSS
Last-modified: Wed, 03 Sep 2008 01:12:03 UTC (5960d)
Link: FrontPage(3474d)

Modified by MT22(Moriwaki Takashi)

"PukiWiki" 1.3.7 Copyright © 2001,2002,2003 PukiWiki Developers Team. License is GNU/GPL.
Based on "PukiWiki" 1.3 by sng
Powered by PHP 7.4.33

HTML convert time to 0.011 sec.