cayenneメモ


cayenneメモ

primary key の取得法

http://www.objectstyle.org/cayenne/userguide/dataobjects/unmapped-keys.html

package org.objectstyle.art;

public class Painting extends org.objectstyle.art.auto._Painting {
   /** Read-only access to PK */
   public Integer getPaintingId() {
      return (getObjectId() != null && !getObjectId().isTemporary()) 
               ? (Integer)getObjectId().getIdSnapshot().get(PAINTING_ID_PK_COLUMN) 
               : null;
   }
   
   /** Read-only access to FK */
   public Integer getArtistId() {
      Artist artist = getArtist();
      return (artist != null) 
               ? (Integer)artist.getObjectId().getIdSnapshot().get(Artist.ARTIST_ID_PK_COLUMN) 
               : null;
   }
}

ソートの仕方

普通のカラムでソート

     SelectQuery query = new SelectQuery(Table.class);
     query.addOrdering("column1", true);
     List list = dcx.performQuery(query);

PKでソート(普通のやり方ではできないくさい?)

     Comparator sortInstitution = new Comparator() {
       public int compare(Object arg0, Object arg1) {
         Integer lid0 = (Integer) ((Institution) arg0).getObjectId()
             .getIdSnapshot().get(Institution.INSTITUTION_LID_PK_COLUMN);
         Integer lid1 = (Integer) ((Institution) arg1).getObjectId()
             .getIdSnapshot().get(Institution.INSTITUTION_LID_PK_COLUMN);
         if (lid0.intValue() > lid1.intValue()) {
           return 1;
         } else {
           return -1;
         }
       }
     };
     Collections.sort(list, sortInstitution);

主キーでの取得方法

なかった場合Exceptionでる

ObjectId shohinId = new ObjectId(TShohin.class, orm.auto._TShohin.SHOHIN_ID_PK_COLUMN, 3);
TShohin inu = (TShohin) context.refetchObject(shohinId);

Exceptionは避けたい

ObjectId shohinId = new ObjectId(TShohin.class, orm.auto._TShohin.SHOHIN_ID_PK_COLUMN, 3);
SelectQuery query = QueryUtils.selectObjectForId(objId);
List list = dcx.performQuery(query);
if (list != null && list.size() > 0) {
    TShohin inu = (TShohin) list.get(0);
}

cayenneからConnectionを取得

あってるかは分からんけど、とりあえずできた

private Connection getConnectionFromCayenne() throws Exception {
       Configuration conf = Configuration.getSharedConfiguration();
       DataDomain domain = conf.getDomain();
       Iterator nodes = domain.getDataNodes().iterator();

       if (!nodes.hasNext()) {
           log.error("I400001 getConnectionSameAsCayenne cannnot get node");
           return null;
       }

       DataNode dataNode = (DataNode) nodes.next();

       //        PoolManager poolManager = (PoolManager) dataNode.getDataSource();
       //
       //        String jdbcDriver = poolManager.getJdbcDriver();
       //        String url = poolManager.getDataSourceUrl();
       //        String userName = poolManager.getUserName();
       //        String passWord = poolManager.getPassword();
       //
       //        log.debug("poolManager#jdbcDriver = " + jdbcDriver);
       //        log.debug("poolManager#url = " + url);
       //        log.debug("poolManager#userName = " + userName);
       //        log.debug("poolManager#passWord = " + passWord);
       //
       //        Class.forName(jdbcDriver);
       //
       //        return DriverManager.getConnection(url, userName, passWord);

       return dataNode.getDataSource().getConnection();
   }

SQLTemplate

cayenne1.1.1 の SQLTemplate には以下の制限があるようである

  • すべてのカラムを取得しないとEntityの値がクリアされる
  • プライマリーキーの値がユニークでないと意図どおりにEntityに格納されない
     テーブル構成
     GET_TBL
       PK_COLUMN
       GET_COLUMN
       NOT_GET_COLUMN1
       NOT_GET_COLUMN2
       JUDGE_FLG
     String sql =
         "select "
           + " row_number() over(order by PATIENT_ID) as PK_COLUMN,"
           + " t0.GET_COLUMN as GET_COLUMN,"
           + " '' as NOT_GET_COLUMN1,"
           + " '' as NOT_GET_COLUMN2 "
           + "from"
           + " ( "
           + "select distinct"
           + " GET_COLUMN,"
           + "from GET_TBL "
           + "where JUDGE_FLG == 1"
           + ") t0 "
           + "order by GET_COLUMN";
     SQLTemplate rawSelect = new SQLTemplate(GetTbl.class, sql, true);
     rawSelect.setLoggingLevel(Level.INFO);
     rawSelect.setPageSize(10);
     List list = dcx.performQuery(rawSelect);

IndexOutOf?.... : 0 みたいなエラーが出る

CayenneオブジェクトとCayenne.jarのバージョンが違ってる可能性あり。


FrontPage

Menu

Linux

サーバ構築

Windows

Java

Program

 

最新の20件

2008-04-02 2008-03-11 2008-01-28 2008-01-21 2007-11-27 2007-09-19 2007-06-07 2007-04-11 2007-04-06 2007-03-22 2006-12-20 2006-12-05 2006-12-04 2006-10-27 2006-09-22 2006-07-03 2006-05-16