HSQL In-memory database hibernate maven config example

Reading Time: 1 minute maven dependency <properties> <!– PROJECT DEPENDENCIES BEGIN –> <hsqldb.version>2.3.4</hsqldb.version> <!– PROJECT DEPENDENCIES END –> </properties> <dependencies> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>${hsqldb.version}</version> </dependency> </dependencies> hibernate configuration <?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.archive.autodetection”>class,hbm</property> <property name=”hibernate.dialect”>org.hibernate.dialect.HSQLDialect</property> <property name=”hibernate.show_sql”>true</property> <property name=”hibernate.connection.driver_class”>org.hsqldb.jdbcDriver</property> <property name=”hibernate.connection.username”>sa</property> <property name=”hibernate.connection.password”>1</property> <property name=”hibernate.connection.url”>jdbc:hsqldb:mem:testdb</property> <property name=”hibernate.hbm2ddl.auto”>create</property> <mapping class=”de.gameduell.entity.User”

Converting in between Blob and StringBuilder

Reading Time: 1 minute From StringBuilder to Blob @Autowired private SessionFactory sessionFactory; //saving the blob field java.sql.Blob blob = org.hibernate.Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(errorDescs.toString().getBytes()); From Blob to StringBuilder StringBuilder errorDescs = new StringBuilder(); //error description if (order.getBatchErrorDescription() == null){ errorDescs.append(response.getErrors().getDescription().toString() + “;”); } else{ try { Blob blob = order.getBatchErrorDescription(); byte[] byteData = blob.getBytes(1, (int) blob.length()); String data = new String(byteData); errorDescs.append(data) .append(response.getErrors().getDescription().toString()

Enabling auto reconnect in mysql hibernate spring

Reading Time: 1 minute in your spring config file append the url entry with the below sample   <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”> <property name=”driverClassName” value=”${database.driverClassName}” /> <property name=”url” value=”${database.url}?characterEncoding=UTF-8&amp;autoReconnect=true”/> <property name=”username” value=”${database.user}” /> <property name=”password” value=”${database.password}” /> </bean>

Hibernate C3P0 pooling integration

Reading Time: 1 minute hibernate.cfg.xml <?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.org/dtd/hibernate-configuration-3.0.dtd”> <hibernate-configuration> <session-factory> <!– Hibernate Settings –> <property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property> <property name=”hibernate.connection.url”>jdbc:mysql://10.10.100.106:3306/nuspa?autoReconnect=true</property> <property name=”hibernate.connection.username”>root</property> <property name=”hibernate.connection.password”>xxxx</property> <property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property> <property name=”hibernate.current_session_context_class”>thread</property> <property name=”hibernate.hbm2ddl.auto”>validate</property> <property name=”show_sql”>true</property> <!– C3P0 Pooling settings –> <property name=”hibernate.c3p0.min_size”>7</property> <property name=”hibernate.c3p0.max_size”>53</property> <property name=”hibernate.c3p0.timeout”>100</property> <property name=”hibernate.c3p0.max_statements”>50</property> <property name=”hibernate.c3p0.idle_test_period”>1000</property> <property name=”hibernate.c3p0.validate”>true</property> <!– Mapping Entity classes

Switching Between Hibernate’s and Spring’s Transaction

Reading Time: 1 minute Uncomment the below lines to hand the activation down to Spring pom.xml <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.tugrulaslan</groupId> <artifactId>WebApp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>BlogWebApp Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project-java.version>1.7</project-java.version> <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version> <junit.version>4.11</junit.version> <mysql-connector.version>5.1.34</mysql-connector.version> <hibernate.version>4.3.8.Final</hibernate.version> <javax-persistance-api.version>1.0.2</javax-persistance-api.version> <spring.version>4.0.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version>