2013年6月29日土曜日

RailsのDatabase確認

RailsプロジェクトのRootで
rake dbconsole

もしくは、同じディレクトリで、
sqlite3を使用しているなら、
sqlite3 'DatabaseのPath'

ちなみに、DatabaseのPathは、
config/database.yml

に書いてあります。

2013年6月3日月曜日

複数メソッドに跨ったトランザクション処理

SpringのTransactionを利用せずに、自分でTransactionを制御したい。
SpringのTransactionは、1メソッド内で完結する必要があるが、私がやりたいことは、複数メソッドにまたがってTransactionを維持したいのである。

それをするには、Transactionを持ち回わらなければいけない。
Hibernateでは、EntityManagerからTransactionを取得することが禁止されているようであるが、それを取得するメソッドが公開されている。恐らく設定なのだろう。

java.sql.Connectionクラスを使って、おとなしくDBからデータ取得して、Mapにでも詰めるのがベターなのだろうか。

しかし、私が取得したいデータは複数にまたがっているので、データ取得時に、JOINしないとダメかな。

取りあえず、単純にDBからQuery発行して直接データ取得した後、Objectにデータ詰めたいですって人には、以下のURLが参考になると思います。

http://www.codeproject.com/Tips/372152/Mapping-JDBC-ResultSet-to-Object-using-Annotations

2013年5月31日金曜日

[ Hibernate ] ORDER BYの結果を上から10件取得する

PureJavaで、ORDER BYでソートした後、その結果を、先頭から10件ずつデータを取得したかったので、FROM句以下でサブクエリを作った。

SELECT * FROM (SELECT * FROM table ORDER BY table.date ASC) WHERE ROWNUM <= 10

これを、EntityManagerから、createQuery( query )をCallしてSQLを実行するも失敗。

↓の仕様をみると、


HQL 副問い合わせは、 select または where 節だけで使われることに注意してください。

FROM句で使えないので、はまりました。

で、解決策は、PureJavaのjavax.persistence.QueryのsetMaxResults()を使って引数に10を入れれば、ソート後の結果から10件取得できました。

2013年5月14日火曜日

hamlの準備

rails generate した後に、hamlを使うようにする準備
1.Gemfileに以下の1行を記載
    gem 'haml-rails'
2.bundle install

hamlの準備をしていない時のErrorには、hamlを取り扱えるよ!、という記載がないが、hamlの準備をしたら、取り扱えるようになっていた!


ActionView::MissingTemplate (Missing template hello/view, application/view with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/workspace/RubyOnRails/railbook/app/views"
):



ActionView::MissingTemplate (Missing template hello/view, application/view with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
  * "/workspace/RubyOnRails/railbook/app/views"
):


あと、MissingTemplateがでる理由だが、ルーティングを間違えていた。

/app/views/コントローラー名/アクション名.html.haml

ex)
/app/views/hello/view.html.haml
の場合、URLは以下のようになる。
http://localhost:3000/hello/view

ちなみに、アクション名は、コントローラでは以下の色付きのところ。

-------


# codinf: utf-8

class HelloController < ApplicationController
  def view
    @msg = 'Hello World from view'
  end
end

2013年1月2日水曜日

herokuでsqlite3からpostgresqlへ

sqlite3 -> postgresql にDBを変更する時のログ。

$ heroku rake db:migrate</ br> ! For Cedar apps, use: `heroku run rake db:migrate` $ heroku run rake db:migrate</ br> Running rake db:migrate attached to terminal... up, run.5672 DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from at /app/Rakefile:9) DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from at /app/Rakefile:9) Connecting to database specified by DATABASE_URL Migrating to CreateMessages (20121222150925) == CreateMessages: migrating ================================================= -- create_table(:messages) NOTICE: CREATE TABLE will create implicit sequence "messages_id_seq" for serial column "messages.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "messages_pkey" for table "messages" -> 0.0589s == CreateMessages: migrated (0.0590s) ========================================

2013年1月1日火曜日

Gemfileを修正したらbundle install

しないと、以下の様なErrorが出て、herokuにpushできない。


git push heroku master
Counting objects: 91, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (81/81), done.
Writing objects: 100% (91/91), 30.37 KiB, done.
Total 91 (delta 5), reused 0 (delta 0)
-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.3.0.pre.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
       You are trying to install in deployment mode after changing
       your Gemfile. Run `bundle install` elsewhere and add the
       updated Gemfile.lock to version control.
       You have added to the Gemfile:
       * sqlite3
       * thin
       * pg
 !
 !     Failed to install gems via Bundler.
 !
 !     Heroku push rejected, failed to compile Ruby/rails app

To git@heroku.com:xxxxxxxxxxxxxxxxxxx.git
 ! [remote rejected] master -> master (pre-receive hook declined)

2012年12月25日火曜日

【Linux】CentOS6でuseraddしても、ログインできない

問題: タイトルどおり。
原因: ログインユーザがGroup指定されており、そこに新規Userを追加していなかった。
解決策: vi /etc/group で、対象のGroupに新規ユーザを追加する。

↓のサイトを見て解決。
http://koexuka.blogspot.jp/2009/08/ssh.html


他にもメモ。
以下、lockoutはユーザ名です。


password-authに以下の行を追加すると、パスワードを間違えた回数が記録される。
auth        required      pam_tally2.so deny=3

記録回数の確認は、以下のコマンド。
pam_tally2 -u lockout

ロックアウト解除
pam_tally2 -r -u lockout
or
pam_tally2 -u lockout --reset


ロックアウトをテストするために、ユーザ追加して、確認する手順。

useradd lockout ← ユーザの新規追加

tail -n 10 /var/log/secure ← ログインできない理由を調査

Dec 25 14:05:03 xxx sshd[32330]: User lockout from xxx.xxx.xxx.xxx not allowed because none of user's groups are listed in AllowGroups


vi /etc/group ← 所属Groupにlockoutユーザを追加


これで、ログイン可能!