Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make it possible to handle simple updates

  • Loading branch information...
commit 697f1e4c8fd5eaa64204ed070c8da1462f83285e 1 parent 67ae64f
Ola Bini authored
1  lib/ribs/db.rb
@@ -72,6 +72,7 @@ def create
72 72 properties.set_property(key, value)
73 73 end
74 74 @configuration = Configuration.new.add_properties(properties)
  75 + @configuration.set_interceptor org.jruby.ribs.EntityNameInterceptor.new
75 76 @mappings = @configuration.create_mappings
76 77 reset_session_factory!
77 78 end
4 lib/ribs/definition.rb
@@ -160,7 +160,7 @@ def define_ribs(on, options = {})
160 160 val.add_column(c)
161 161 val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
162 162 prop.value = val
163   -
  163 +
164 164 if (!rib.primary_keys.empty? && rib.primary_keys[c.name.downcase]) || c.name.downcase == 'id'
165 165 pc.identifier_property = prop
166 166 pc.identifier = val
@@ -201,7 +201,7 @@ def #{downcased}=(value)
201 201 def get_type_for_sql(name, code)
202 202 case code
203 203 when JTypes::VARCHAR
204   - "java.lang.String"
  204 + "string"
205 205 when JTypes::INTEGER
206 206 "int"
207 207 when JTypes::TIME
18 lib/ribs/session.rb
@@ -30,12 +30,12 @@ def release
30 30 @db.release(self)
31 31 end
32 32
33   - # LOW LEVEL - shouldn't be used
  33 + # LOW LEVEL - shouldn't be used except by Ribs
34 34 def hibernate_session
35 35 @hibernate_session
36 36 end
37 37
38   - # LOW LEVEL - shouldn't be used
  38 + # LOW LEVEL - shouldn't be used except by Ribs
39 39 def find(entity_name, id)
40 40 chk_conn
41 41 if id == :all
@@ -45,33 +45,33 @@ def find(entity_name, id)
45 45 end
46 46 end
47 47
48   - # LOW LEVEL - shouldn't be used
  48 + # LOW LEVEL - shouldn't be used except by Ribs
49 49 def save(obj)
50 50 chk_conn
51 51 tx = @hibernate_session.begin_transaction
52 52 if obj.__ribs_meat.persistent
53   - @hibernate_session.update(obj.class.ribs_metadata.persistent_class.entity_name, obj)
  53 + @hibernate_session.update(obj)
54 54 else
55   - @hibernate_session.save(obj.class.ribs_metadata.persistent_class.entity_name, obj)
  55 + @hibernate_session.save(obj)
56 56 obj.__ribs_meat.persistent = true
57 57 end
58 58 tx.commit
59 59 obj
60 60 end
61 61
62   - # LOW LEVEL - shouldn't be used
  62 + # LOW LEVEL - shouldn't be used except by Ribs
63 63 def meta_data
64 64 chk_conn
65 65 @hibernate_session.connection.meta_data
66 66 end
67 67
68   - # LOW LEVEL - shouldn't be used
  68 + # LOW LEVEL - shouldn't be used except by Ribs
69 69 def ddl(string)
70 70 chk_conn
71 71 execute(string)
72 72 end
73 73
74   - # LOW LEVEL - shouldn't be used
  74 + # LOW LEVEL - shouldn't be used except by Ribs
75 75 def insert(template, *data)
76 76 chk_conn
77 77 conn = @hibernate_session.connection
@@ -91,7 +91,7 @@ def insert(template, *data)
91 91 stmt.close rescue nil
92 92 end
93 93
94   - # LOW LEVEL - shouldn't be used
  94 + # LOW LEVEL - shouldn't be used except by Ribs
95 95 def select(string)
96 96 chk_conn
97 97 conn = @hibernate_session.connection
30 src/java/org/jruby/ribs/EntityNameInterceptor.java
... ... @@ -0,0 +1,30 @@
  1 +/**
  2 + * Copyright (c) 2008, Ola Bini <ola.bini@gmail.com>
  3 + * All rights reserved.
  4 + *
  5 + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6 + *
  7 + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8 + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
  9 + * in the documentation and/or other materials provided with the distribution.
  10 + *
  11 + * Neither the name of the IT-Centrum, Karolinska Institutet, Sweden nor the names of its contributors may be used to endorse or
  12 + * promote products derived from this software without specific prior written permission.
  13 + *
  14 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15 + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16 + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  17 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  18 + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  19 + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20 + */
  21 +package org.jruby.ribs;
  22 +
  23 +import org.hibernate.EmptyInterceptor;
  24 +import org.jruby.runtime.builtin.IRubyObject;
  25 +
  26 +public class EntityNameInterceptor extends EmptyInterceptor {
  27 + public String getEntityName(Object object) {
  28 + return ((IRubyObject)object).getType().getName().replace("::",".");
  29 + }
  30 +}
14 test/artist_spec.rb
@@ -62,5 +62,17 @@ class Artist
62 62 end
63 63 end
64 64
65   - it "should be possible to update name property on existing bean"
  65 + it "should be possible to update name property on existing bean" do
  66 + begin
  67 + artist = Artist.find(2)
  68 + artist.name = "U2"
  69 + Artist.find(2).name.should == "New Model Army"
  70 + artist.save
  71 + Artist.find(2).name.should == "U2"
  72 + ensure
  73 + reset_database!
  74 + end
  75 + end
  76 +
  77 + it "should be possible to delete existing bean"
66 78 end

0 comments on commit 697f1e4

Please sign in to comment.
Something went wrong with that request. Please try again.