Skip to content
Browse files

Make it possible to handle simple updates

  • Loading branch information...
1 parent 67ae64f commit 697f1e4c8fd5eaa64204ed070c8da1462f83285e @olabini committed Aug 29, 2008
Showing with 55 additions and 12 deletions.
  1. +1 −0 lib/ribs/db.rb
  2. +2 −2 lib/ribs/definition.rb
  3. +9 −9 lib/ribs/session.rb
  4. +30 −0 src/java/org/jruby/ribs/EntityNameInterceptor.java
  5. +13 −1 test/artist_spec.rb
View
1 lib/ribs/db.rb
@@ -72,6 +72,7 @@ def create
properties.set_property(key, value)
end
@configuration = Configuration.new.add_properties(properties)
+ @configuration.set_interceptor org.jruby.ribs.EntityNameInterceptor.new
@mappings = @configuration.create_mappings
reset_session_factory!
end
View
4 lib/ribs/definition.rb
@@ -160,7 +160,7 @@ def define_ribs(on, options = {})
val.add_column(c)
val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
prop.value = val
-
+
if (!rib.primary_keys.empty? && rib.primary_keys[c.name.downcase]) || c.name.downcase == 'id'
pc.identifier_property = prop
pc.identifier = val
@@ -201,7 +201,7 @@ def #{downcased}=(value)
def get_type_for_sql(name, code)
case code
when JTypes::VARCHAR
- "java.lang.String"
+ "string"
when JTypes::INTEGER
"int"
when JTypes::TIME
View
18 lib/ribs/session.rb
@@ -30,12 +30,12 @@ def release
@db.release(self)
end
- # LOW LEVEL - shouldn't be used
+ # LOW LEVEL - shouldn't be used except by Ribs
def hibernate_session
@hibernate_session
end
- # LOW LEVEL - shouldn't be used
+ # LOW LEVEL - shouldn't be used except by Ribs
def find(entity_name, id)
chk_conn
if id == :all
@@ -45,33 +45,33 @@ def find(entity_name, id)
end
end
- # LOW LEVEL - shouldn't be used
+ # LOW LEVEL - shouldn't be used except by Ribs
def save(obj)
chk_conn
tx = @hibernate_session.begin_transaction
if obj.__ribs_meat.persistent
- @hibernate_session.update(obj.class.ribs_metadata.persistent_class.entity_name, obj)
+ @hibernate_session.update(obj)
else
- @hibernate_session.save(obj.class.ribs_metadata.persistent_class.entity_name, obj)
+ @hibernate_session.save(obj)
obj.__ribs_meat.persistent = true
end
tx.commit
obj
end
- # LOW LEVEL - shouldn't be used
+ # LOW LEVEL - shouldn't be used except by Ribs
def meta_data
chk_conn
@hibernate_session.connection.meta_data
end
- # LOW LEVEL - shouldn't be used
+ # LOW LEVEL - shouldn't be used except by Ribs
def ddl(string)
chk_conn
execute(string)
end
- # LOW LEVEL - shouldn't be used
+ # LOW LEVEL - shouldn't be used except by Ribs
def insert(template, *data)
chk_conn
conn = @hibernate_session.connection
@@ -91,7 +91,7 @@ def insert(template, *data)
stmt.close rescue nil
end
- # LOW LEVEL - shouldn't be used
+ # LOW LEVEL - shouldn't be used except by Ribs
def select(string)
chk_conn
conn = @hibernate_session.connection
View
30 src/java/org/jruby/ribs/EntityNameInterceptor.java
@@ -0,0 +1,30 @@
+/**
+ * Copyright (c) 2008, Ola Bini <ola.bini@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the IT-Centrum, Karolinska Institutet, Sweden nor the names of its contributors may be used to endorse or
+ * promote products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * 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.
+ */
+package org.jruby.ribs;
+
+import org.hibernate.EmptyInterceptor;
+import org.jruby.runtime.builtin.IRubyObject;
+
+public class EntityNameInterceptor extends EmptyInterceptor {
+ public String getEntityName(Object object) {
+ return ((IRubyObject)object).getType().getName().replace("::",".");
+ }
+}
View
14 test/artist_spec.rb
@@ -62,5 +62,17 @@ class Artist
end
end
- it "should be possible to update name property on existing bean"
+ it "should be possible to update name property on existing bean" do
+ begin
+ artist = Artist.find(2)
+ artist.name = "U2"
+ Artist.find(2).name.should == "New Model Army"
+ artist.save
+ Artist.find(2).name.should == "U2"
+ ensure
+ reset_database!
+ end
+ end
+
+ it "should be possible to delete existing bean"
end

0 comments on commit 697f1e4

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