Commits

Loiane Groner  committed e7d0860

first commit

  • Participants

Comments (0)

Files changed (29)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="test"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="lib" path="lib/antlr-2.7.6.jar"/>
+	<classpathentry kind="lib" path="lib/commons-collections-3.1.jar"/>
+	<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/>
+	<classpathentry kind="lib" path="lib/hibernate-jpa-2.0-api-1.0.0.Final.jar"/>
+	<classpathentry kind="lib" path="lib/hibernate3.jar"/>
+	<classpathentry kind="lib" path="lib/javassist-3.9.0.GA.jar"/>
+	<classpathentry kind="lib" path="lib/jta-1.1.jar"/>
+	<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.12-bin.jar"/>
+	<classpathentry kind="lib" path="lib/slf4j-api-1.5.8.jar"/>
+	<classpathentry kind="lib" path="lib/slf4j-simple-1.5.8.jar"/>
+	<classpathentry kind="lib" path="lib/junit-4.8.2.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>hibernate-image-example</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

File .settings/org.eclipse.jdt.core.prefs

+#Fri Sep 23 10:36:38 BRT 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

File bin/com/loiane/dao/BookDAOImpl.class

Binary file added.

File bin/com/loiane/hibernate/HibernateUtil.class

Binary file added.

File bin/com/loiane/model/Book.class

Binary file added.

File bin/com/loiane/test/TestBookDAO.class

Binary file added.

File bin/hibernate.cfg.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE hibernate-configuration PUBLIC
+		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+    <session-factory>
+        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
+        <property name="hibernate.connection.url">jdbc:mysql://localhost/blog</property>
+        <property name="hibernate.connection.username">root</property>
+        <property name="hibernate.connection.password">root</property>
+        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
+    	<property name="connection.pool_size">1</property>
+    	<property name="show_sql">true</property>
+    </session-factory>
+</hibernate-configuration>

File images/extjsfirstlook.jpg

Added
New image

File images/output.jpg

Added
New image

File lib/antlr-2.7.6.jar

Binary file added.

File lib/commons-collections-3.1.jar

Binary file added.

File lib/dom4j-1.6.1.jar

Binary file added.

File lib/hibernate-jpa-2.0-api-1.0.0.Final.jar

Binary file added.

File lib/hibernate3.jar

Binary file added.

File lib/javassist-3.9.0.GA.jar

Binary file added.

File lib/jta-1.1.jar

Binary file added.

File lib/junit-4.8.2.jar

Binary file added.

File lib/mysql-connector-java-5.1.12-bin.jar

Binary file added.

File lib/slf4j-api-1.5.8.jar

Binary file added.

File lib/slf4j-simple-1.5.8.jar

Binary file added.
+# Hibernate Image Example
+
+Demonstrates how to save/load an image from MySQL using Hibernate.
+
+## Post - Blog - Step by Step
+
+* comming soon
+
+
+## More Tutorials
+
+* Loiane Groner
+* http://loianegroner.com (English)
+* http://loiane.com (Portuguese)

File sql/BOOK.mwb

Binary file added.

File sql/script.sql

+
+DROP SCHEMA IF EXISTS `blog` ;
+CREATE SCHEMA IF NOT EXISTS `blog` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
+USE `blog` ;
+
+
+-- -----------------------------------------------------
+-- Table `blog`.`BOOK`
+-- -----------------------------------------------------
+DROP TABLE IF EXISTS `blog`.`BOOK` ;
+
+CREATE  TABLE IF NOT EXISTS `blog`.`BOOK` (
+  `BOOK_ID` INT NOT NULL AUTO_INCREMENT ,
+  `BOOK_NAME` VARCHAR(45) NOT NULL ,
+  `BOOK_IMAGE` MEDIUMBLOB NOT NULL ,
+  PRIMARY KEY (`BOOK_ID`) )
+ENGINE = InnoDB;

File src/com/loiane/dao/BookDAOImpl.java

+package com.loiane.dao;
+
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+
+import com.loiane.hibernate.HibernateUtil;
+import com.loiane.model.Book;
+
+/**
+ * Book DAO
+ * Interacts with database.
+ * Implemented for academic purposes.
+ * 
+ * @author Loiane Groner
+ * http://loianegroner.com (English)
+ * http://loiane.com (Portuguese)
+ */
+public class BookDAOImpl {
+	
+	
+	/**
+	 * Inserts a row in the BOOK table.
+	 * Do not need to pass the id, it will be generated.
+	 * @param book 
+	 * @return an instance of the object Book
+	 */
+	public Book saveBook(Book book)
+	{
+		Session session = HibernateUtil.getSessionFactory().openSession();
+		Transaction transaction = null;
+		try {
+			transaction = session.beginTransaction();
+			session.save(book);
+			transaction.commit();
+		} catch (HibernateException e) {
+			transaction.rollback();
+			e.printStackTrace();
+		} finally {
+			session.close();
+		}
+		return book;
+	}
+	
+	
+	/**
+	 * Delete a book from database
+	 * @param bookId id of the book to be retrieved
+	 */
+	public Book getBook(Long bookId)
+	{
+		Session session = HibernateUtil.getSessionFactory().openSession();
+		try {
+			Book book = (Book) session.get(Book.class, bookId);
+			return book;
+		} catch (HibernateException e) {
+			e.printStackTrace();
+		} finally {
+			session.close();
+		}
+		return null;
+	}
+}

File src/com/loiane/hibernate/HibernateUtil.java

+package com.loiane.hibernate;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.AnnotationConfiguration;
+
+import com.loiane.model.Book;
+
+/**
+ * The HibernateUtil class helps in creating the SessionFactory
+ * from the Hibernate configuration file
+ * 
+ * @author Loiane Groner
+ * http://loianegroner.com (English)
+ * http://loiane.com (Portuguese)
+ */
+public class HibernateUtil {
+	
+	private static final SessionFactory sessionFactory;
+	
+	static {
+		try {
+			sessionFactory = new AnnotationConfiguration()
+								.configure()
+								.addPackage("com.loiane.model") //the fully qualified package name
+								.addAnnotatedClass(Book.class)
+								.buildSessionFactory();
+			
+		} catch (Throwable ex) {
+			System.err.println("Initial SessionFactory creation failed." + ex);
+			throw new ExceptionInInitializerError(ex);
+		}
+	}
+
+	public static SessionFactory getSessionFactory() {
+		return sessionFactory;
+	}
+}

File src/com/loiane/model/Book.java

+package com.loiane.model;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+import javax.persistence.Table;
+
+/**
+ * Book POJO class.
+ * Represents the BOOK table
+ * 
+ * @author Loiane Groner
+ * http://loianegroner.com (English)
+ * http://loiane.com (Portuguese)
+ */
+@Entity
+@Table(name="BOOK")
+public class Book {
+
+	@Id
+	@GeneratedValue
+	@Column(name="BOOK_ID")
+	private long id;
+	
+	@Column(name="BOOK_NAME", nullable=false)
+	private String name;
+	
+	@Lob
+	@Column(name="BOOK_IMAGE", nullable=false, columnDefinition="mediumblob")
+	private byte[] image;
+	
+	public long getId() {
+		return id;
+	}
+	
+	public void setId(long id) {
+		this.id = id;
+	}
+	
+	public String getName() {
+		return name;
+	}
+	
+	public void setName(String name) {
+		this.name = name;
+	}
+	
+	public byte[] getImage() {
+		return image;
+	}
+	
+	public void setImage(byte[] image) {
+		this.image = image;
+	}
+}

File src/hibernate.cfg.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE hibernate-configuration PUBLIC
+		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+    <session-factory>
+        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
+        <property name="hibernate.connection.url">jdbc:mysql://localhost/blog</property>
+        <property name="hibernate.connection.username">root</property>
+        <property name="hibernate.connection.password">root</property>
+        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
+    	<property name="connection.pool_size">1</property>
+    	<property name="show_sql">true</property>
+    </session-factory>
+</hibernate-configuration>

File test/com/loiane/test/TestBookDAO.java

+package com.loiane.test;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.loiane.dao.BookDAOImpl;
+import com.loiane.model.Book;
+
+/**
+ * Test Case
+ * 
+ * @author Loiane Groner
+ * http://loianegroner.com (English)
+ * http://loiane.com (Portuguese)
+ */
+public class TestBookDAO {
+	
+	private static BookDAOImpl bookDAO;
+
+	@BeforeClass
+	public static  void runBeforeClass() {
+		bookDAO = new BookDAOImpl();
+	}
+
+	@AfterClass
+	public static void runAfterClass() {
+		bookDAO = null;
+	}
+
+	/**
+	 * Test method for {@link com.loiane.dao.BookDAOImpl#saveBook()}.
+	 */
+	@Test
+	public void testSaveBook() {
+		
+		//File file = new File("images\\extjsfirstlook.jpg"); //windows
+		File file = new File("images/extjsfirstlook.jpg");
+        byte[] bFile = new byte[(int) file.length()];
+        
+        try {
+	        FileInputStream fileInputStream = new FileInputStream(file);
+	        fileInputStream.read(bFile);
+	        fileInputStream.close();
+        } catch (Exception e) {
+	        e.printStackTrace();
+        }
+        
+        Book book = new Book();
+        book.setName("Ext JS 4 First Look");
+        book.setImage(bFile);
+        
+        bookDAO.saveBook(book);
+        
+        assertNotNull(book.getId());
+	}
+
+	/**
+	 * Test method for {@link com.loiane.dao.BookDAOImpl#getBook()}.
+	 */
+	@Test
+	public void testGetBook() {
+		
+		Book book = bookDAO.getBook((long) 1);
+		
+		assertNotNull(book);
+		
+		try{
+        	//FileOutputStream fos = new FileOutputStream("images\\output.jpg");  //windows
+        	FileOutputStream fos = new FileOutputStream("images/output.jpg"); 
+            fos.write(book.getImage());
+            fos.close();
+        }catch(Exception e){
+        	e.printStackTrace();
+        }
+	}
+}