-
Notifications
You must be signed in to change notification settings - Fork 5
/
hibernate.rb
132 lines (108 loc) · 3.02 KB
/
hibernate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require 'java'
require 'lib/antlr-2.7.6.jar'
require 'lib/commons-collections-3.1.jar'
require 'lib/dom4j-1.6.1.jar'
require 'lib/javassist-3.9.0.GA.jar'
require 'lib/jta-1.1.jar'
require 'lib/slf4j-simple-1.5.8.jar'
require 'lib/slf4j-api-1.5.8.jar'
require 'lib/hibernate3.jar'
require 'stringio'
require 'dialects'
module Hibernate
import org.hibernate.cfg.Configuration
import javax.xml.parsers.DocumentBuilderFactory
import org.xml.sax.InputSource
JClass = java.lang.Class
JVoid = java.lang.Void::TYPE
DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.new_instance
DOCUMENT_BUILDER_FACTORY.validating = false
DOCUMENT_BUILDER_FACTORY.expand_entity_references = false
DOCUMENT_BUILDER = DOCUMENT_BUILDER_FACTORY.new_document_builder
def self.dialect=(dialect)
config.set_property "hibernate.dialect", dialect
end
def self.current_session_context_class=(ctx_cls)
config.set_property "hibernate.current_session_context_class", ctx_cls
end
def self.connection_driver_class=(driver_class)
config.set_property "hibernate.connection.driver_class", driver_class
end
def self.connection_url=(url)
config.set_property "hibernate.connection.url", url
end
def self.connection_username=(username)
config.set_property "hibernate.connection.username", username
end
def self.connection_password=(password)
config.set_property "hibernate.connection.password", password
end
class PropertyShim
def initialize(config)
@config = config
end
def []=(key, value)
key = ensure_hibernate_key(key)
@config.set_property key, value
end
def [](key)
key = ensure_hibernate_key(key)
config.get_property key
end
private
def ensure_hibernate_key(key)
unless key =~ /^hibernate\./
key = 'hibernate.' + key
end
key
end
end
def self.properties
PropertyShim.new(@config)
end
def self.tx
session.begin_transaction
if block_given?
yield session
session.transaction.commit
end
end
def self.factory
@factory ||= config.build_session_factory
end
def self.session
factory.current_session
end
def self.config
@config ||= Configuration.new
end
def self.add_model(mapping)
config.add_xml(File.read(mapping))
end
module Model
TYPES = {
:string => java.lang.String,
:long => java.lang.Long,
:date => java.util.Date
}
def hibernate_sigs
@hibernate_sigs ||= {}
end
def hibernate_attr(attrs)
attrs.each do |name, type|
attr_accessor name
get_name = "get#{name.to_s.capitalize}"
set_name = "set#{name.to_s.capitalize}"
alias_method get_name.intern, name
add_method_signature get_name, [TYPES[type].java_class]
alias_method set_name.intern, :"#{name.to_s}="
add_method_signature set_name, [JVoid, TYPES[type].java_class]
end
end
def hibernate!
become_java!
# Hibernate.mappings.
# Hibernate.add_mapping reified_class,
end
end
end