Skip to content

Instantly share code, notes, and snippets.

@m0smith
Created September 28, 2011 12:52
Show Gist options
  • Save m0smith/1247851 to your computer and use it in GitHub Desktop.
Save m0smith/1247851 to your computer and use it in GitHub Desktop.
Proof of concept of using clojure and hibernate
(ns topoged.model.type
(:import
(javax.persistence Entity Id Column Table GeneratedValue)
))
(definterface IType (getId []))
(deftype
^{Entity {} Table {:name="TYPE"} org.hibernate.annotations.Entity {:mutable false}}
TypeX [^Long id]
IType
(^{Id {} Column {:name "TYPE_ID"} GeneratedValue {}} getId [this] (.id this)) )
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="Type" table="TYPE">
<id column="TYPE_ID" name="id" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property column="TYPE_DESC" name="desc" type="java.lang.String"/>
</class>
</hibernate-mapping>
(defn hibernate-session-factory []
(let [cfg (org.hibernate.cfg.AnnotationConfiguration.) ]
(doto cfg
(.setProperty "hibernate.dialect" "org.hibernate.dialect.DerbyDialect")
(.setProperty "hibernate.connection.driver_class" "org.apache.derby.jdbc.EmbeddedDriver")
(.setProperty "hibernate.connection.url" "jdbc:derby:/tmp/topogedDB2;create=false")
(.setProperty "hibernate.connection.username" "")
(.setProperty "hibernate.connection.password" "")
(.setProperty "hibernate.current_session_context_class" "org.hibernate.context.ThreadLocalSessionContext")
(.setProperty "hibernate.default_entity_mode" "dynamic-map")
(.setProperty "hibernate.dialect" "org.hibernate.dialect.DerbyDialect")
(.setProperty "hibernate.hbm2ddl.auto" "update")
(.setProperty "hibernate.transaction.factory_class" "org.hibernate.transaction.JDBCTransactionFactory")
(.setProperty "hibernate.show_sql" "true")
(.setProperty "hibernate.generate_statistics" "true")
(.setProperty "hibernate.use_sql_comments" "false")
(.addFile "src/test/resources/Type.hbm.xml"))
(.buildSessionFactory cfg)))
(defn tester []
(let [^org.hibernate.impl.SessionFactoryImpl sf (hibernate-session-factory)
sessionp (. sf openSession)
session (. sessionp getSession org.hibernate.EntityMode/MAP)
tx (. session beginTransaction)
entity "Type"
m (doto (java.util.HashMap.) (.put "desc" "one"))
m1 (doto (java.util.HashMap.) (.put "desc" "two") (.put "id" 42) )
m2 (doto (java.util.HashMap.) (.put "desc" "three"))]
(println (class session))
(let [ rtnval (. session save entity m1)]
(. session flush)
(. tx commit)
(. session close)
rtnval)))
;;; [org.apache.derby/derby "10.1.1.0"]
;; [org.hibernate/hibernate-annotations "3.4.0.GA"]
;; [org.slf4j/slf4j-api "1.6.2"]
;; [org.slf4j/slf4j-log4j12 "1.6.2"]
;; [org.slf4j/log4j-over-slf4j "1.6.2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment