Browse Source
First commit with working H2 database and Hibernate configuration\! Persistance here we come\!\!\!
master
First commit with working H2 database and Hibernate configuration\! Persistance here we come\!\!\!
master
Drew Short
11 years ago
15 changed files with 293 additions and 45 deletions
-
1pom.xml
-
2src/main/resources/application.conf
-
16src/main/resources/hibernate.cfg.xml
-
16src/main/resources/hibernate/Image.hbm.xml
-
18src/main/resources/hibernate/ImageHash.hbm.xml
-
36src/main/scala/com/sothr/imagetools/dao/HibernateUtil.scala
-
40src/main/scala/com/sothr/imagetools/dao/ImageDAO.scala
-
24src/main/scala/com/sothr/imagetools/dto/ImageHashDTO.scala
-
27src/main/scala/com/sothr/imagetools/image/Image.scala
-
21src/main/scala/com/sothr/imagetools/image/ImageService.scala
-
2src/main/scala/com/sothr/imagetools/util/PropertiesEnum.scala
-
2src/test/resources/application.conf
-
35src/test/resources/hibernate.cfg.xml
-
16src/test/resources/hibernate/Image.hbm.xml
-
18src/test/resources/hibernate/ImageHash.hbm.xml
@ -0,0 +1,16 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<!DOCTYPE hibernate-mapping PUBLIC |
|||
"-//Hibernate/Hibernate Mapping DTD//EN" |
|||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> |
|||
<hibernate-mapping> |
|||
<class name="com.sothr.imagetools.image.Image" table="Image"> |
|||
<meta attribute="class-description"> |
|||
This class contains the image hashes and meta data |
|||
</meta> |
|||
<id name="imagePath" type="string" column="path"/> |
|||
<property name="thumbnailPath" column="thumbnail_path" type="string"/> |
|||
<property name="width" column="width" type="int"/> |
|||
<property name="height" column="height" type="int"/> |
|||
<one-to-one name="hashes" class="com.sothr.imagetools.dto.ImageHashDTO"/> |
|||
</class> |
|||
</hibernate-mapping> |
@ -0,0 +1,18 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<!DOCTYPE hibernate-mapping PUBLIC |
|||
"-//Hibernate/Hibernate Mapping DTD//EN" |
|||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> |
|||
<hibernate-mapping> |
|||
<class name="com.sothr.imagetools.dto.ImageHashDTO" table="ImageHash"> |
|||
<meta attribute="class-description"> |
|||
This class contains the image hashes |
|||
</meta> |
|||
<id name="id" type="int" column="id"> |
|||
<generator class="native"/> |
|||
</id> |
|||
<property name="ahash" column="ahash" type="long"/> |
|||
<property name="dhash" column="dhash" type="long"/> |
|||
<property name="phash" column="phash" type="long"/> |
|||
<property name="md5" column="md5" type="string"/> |
|||
</class> |
|||
</hibernate-mapping> |
@ -0,0 +1,36 @@ |
|||
package com.sothr.imagetools.dao |
|||
|
|||
import grizzled.slf4j.Logging |
|||
import org.hibernate.SessionFactory |
|||
import org.hibernate.cfg.Configuration |
|||
import com.sothr.imagetools.util.{PropertiesEnum, PropertiesService} |
|||
|
|||
/** |
|||
* Created by drew on 2/8/14. |
|||
*/ |
|||
object HibernateUtil extends Logging { |
|||
|
|||
private val sessionFactory:SessionFactory = buildSessionFactory() |
|||
|
|||
private def buildSessionFactory():SessionFactory = { |
|||
try { |
|||
// Create the SessionFactory from hibernate.cfg.xml |
|||
val configuration = new Configuration().configure("hibernate.cfg.xml") |
|||
//set the database location |
|||
info(s"Connecting to database at: \'${PropertiesService.get(PropertiesEnum.DatabaseConnectionURL.toString)}\'") |
|||
configuration.setProperty("hibernate.connection.url", PropertiesService.get(PropertiesEnum.DatabaseConnectionURL.toString)) |
|||
return configuration.buildSessionFactory |
|||
} catch { |
|||
case ex:Throwable => |
|||
// Make sure you log the exception, as it might be swallowed |
|||
error("Initial SessionFactory creation failed.", ex) |
|||
throw new ExceptionInInitializerError(ex) |
|||
} |
|||
} |
|||
|
|||
def getSessionFactory():SessionFactory = { |
|||
sessionFactory |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,40 @@ |
|||
package com.sothr.imagetools.dao |
|||
|
|||
import org.hibernate.{Session, SessionFactory} |
|||
import com.sothr.imagetools.image.Image |
|||
|
|||
/** |
|||
* Created by drew on 2/8/14. |
|||
*/ |
|||
class ImageDAO { |
|||
|
|||
private val sessionFactory:SessionFactory = HibernateUtil.getSessionFactory() |
|||
private val example:Image = new Image() |
|||
|
|||
def find(path:String):Image = { |
|||
val session:Session = sessionFactory.getCurrentSession |
|||
session.beginTransaction |
|||
val result = session.get(example.getClass, path).asInstanceOf[Image] |
|||
session.getTransaction.commit() |
|||
result |
|||
} |
|||
|
|||
def save(image:Image) = { |
|||
val session:Session = sessionFactory.getCurrentSession |
|||
session.beginTransaction |
|||
|
|||
session.saveOrUpdate(image) |
|||
|
|||
session.getTransaction.commit() |
|||
} |
|||
|
|||
def save(images:List[Image]) = { |
|||
val session:Session = sessionFactory.getCurrentSession |
|||
session.beginTransaction |
|||
|
|||
for (image <- images) session.saveOrUpdate(image) |
|||
|
|||
session.getTransaction.commit() |
|||
} |
|||
|
|||
} |
@ -0,0 +1,35 @@ |
|||
<?xml version='1.0' encoding='utf-8'?> |
|||
<!DOCTYPE hibernate-configuration PUBLIC |
|||
"-//Hibernate/Hibernate Configuration DTD//EN" |
|||
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> |
|||
<hibernate-configuration> |
|||
<!-- a SessionFactory instance listed as /jndi/name --> |
|||
<session-factory> |
|||
|
|||
<!-- properties --> |
|||
<property name="connection.driver_class">org.h2.Driver</property> |
|||
<property name="dialect">org.hibernate.dialect.H2Dialect</property> |
|||
<property name="show_sql">true</property> |
|||
<!--<property name="transaction.factory_class"> |
|||
org.hibernate.transaction.JTATransactionFactory |
|||
</property>--> |
|||
<property name="jta.UserTransaction">java:comp/UserTransaction</property> |
|||
|
|||
<property name="hibernate.hbm2ddl.auto">create</property> |
|||
|
|||
<!-- Enable Hibernate's automatic session context management --> |
|||
<property name="current_session_context_class">thread</property> |
|||
|
|||
<!-- Disable the second-level cache --> |
|||
<!--<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>--> |
|||
|
|||
<!-- mapping files --> |
|||
<mapping resource="hibernate/Image.hbm.xml"/> |
|||
<mapping resource="hibernate/ImageHash.hbm.xml"/> |
|||
|
|||
<!-- cache settings --> |
|||
<!--<class-cache class="org.hibernate.auction.Item" usage="read-write"/> |
|||
<class-cache class="org.hibernate.auction.Bid" usage="read-only"/> |
|||
<collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>--> |
|||
</session-factory> |
|||
</hibernate-configuration> |
@ -0,0 +1,16 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<!DOCTYPE hibernate-mapping PUBLIC |
|||
"-//Hibernate/Hibernate Mapping DTD//EN" |
|||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> |
|||
<hibernate-mapping> |
|||
<class name="com.sothr.imagetools.image.Image" table="Image"> |
|||
<meta attribute="class-description"> |
|||
This class contains the image hashes and meta data |
|||
</meta> |
|||
<id name="imagePath" type="string" column="path"/> |
|||
<property name="thumbnailPath" column="thumbnail_path" type="string"/> |
|||
<property name="width" column="width" type="int"/> |
|||
<property name="height" column="height" type="int"/> |
|||
<one-to-one name="hashes" class="com.sothr.imagetools.dto.ImageHashDTO"/> |
|||
</class> |
|||
</hibernate-mapping> |
@ -0,0 +1,18 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<!DOCTYPE hibernate-mapping PUBLIC |
|||
"-//Hibernate/Hibernate Mapping DTD//EN" |
|||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> |
|||
<hibernate-mapping> |
|||
<class name="com.sothr.imagetools.dto.ImageHashDTO" table="ImageHash"> |
|||
<meta attribute="class-description"> |
|||
This class contains the image hashes |
|||
</meta> |
|||
<id name="id" type="int" column="id"> |
|||
<generator class="native"/> |
|||
</id> |
|||
<property name="ahash" column="ahash" type="long"/> |
|||
<property name="dhash" column="dhash" type="long"/> |
|||
<property name="phash" column="phash" type="long"/> |
|||
<property name="md5" column="md5" type="string"/> |
|||
</class> |
|||
</hibernate-mapping> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue