HSQL In-memory database hibernate maven config example

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

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()

Query Example

Hibernate Query Example In this simple query I’ll bring active clubs and memberships. In the query we use object names, not the names seen in the persistence unit SQL Query SELECT c.name as clubname, c.legacyId as clublegacyId, c.clubCity as clubcity, c.isclubactive as clubactive, m.id as membershipId , m.type as membershiptype, m.legacy_id as membershiplegacyid, m.ismembershipactive as

One to Many relationship in a separated table

The Entites Club: one to many Facility: many to one in this example the relation will be held in a separated table, ClubTable.java @Entity @Table(name = "club_table") public class ClubTable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @Column(name = "name", nullable = false) private String name; @Column(name = "latitude") private String

Enabling auto reconnect in mysql hibernate spring

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

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

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>

Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:

This happens because you define set in factory session as just like below. To ward off the issue delete the name definition. <?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 name="KYSSessionFactory"> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.password">xxxx</property> <property name="hibernate.connection.url">jdbc:Oracle:thin:@exap-scan.xxx:1525/xxxx</property> <property name="hibernate.connection.username">xxxx</property> <property name="hibernate.default_schema">xxxx</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <mapping class="com.xxx"/> </session-factory> </hibernate-configuration>

org.hibernate.HibernateException: save is not valid without active transaction

This issue had caused me lots of troubles in my projects. Finally I have found out the main reason is that in spring application context within session factory definition you need to remove hibernate.current_session_context_class entry the below is full code Projects tested Spring 3 Hibernate 3 and 4 <!– Hibernate configuration settings –> <bean id=”sessionFactory” class=”org.springframework.orm.hibernate4.LocalSessionFactoryBean”>