Showing posts with label hibernate. Show all posts
Showing posts with label hibernate. Show all posts

Monday, March 23, 2009

Hibernate optimistic locking

@Column(name = "VERSION", nullable = false, columnDefinition = "bigint default 0")
@Version()
private Long version;
try {
spaceShipManager.saveSpaceShip(ship);
} catch (HibernateOptimisticLockingFailureException e) {
return mapping.findForward("error");
}

Hibernate annotation default column value

@Column(name = "VERSION", nullable = false, columnDefinition = "bigint default 0")
private Long version;

Thursday, March 12, 2009

Hibernate bytecode instrumentation (enhance persistent classes) with help of Maven

Is used to enable Hibernate lazy property fetching. "**/model/*" are your persistent classes.

pom.xml:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>Instrument domain classes</id>
<configuration>
<tasks>
<taskdef name="instrument"
classname="org.hibernate.tool.instrument.cglib.InstrumentTask">
<classpath>
<path refid="maven.dependency.classpath"/>
<path refid="maven.plugin.classpath"/>
</classpath>
</taskdef>
<instrument verbose="true">
<fileset dir="${project.build.outputDirectory}">
<include name="**/model/*.class"/>
</fileset>
</instrument>
</tasks>
</configuration>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
</dependencies>
</plugin>

Hibernate ddl (database schema) generation with help of Maven

pom.xml:
<plugin>
<!-- hibernate ddl generation -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.0-alpha-2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<outputDirectory>.</outputDirectory>
</component>
</components>
<componentProperties>
<configurationfile>src/main/db/hibernate.cfg.xml</configurationfile>
<drop>false</drop>
<create>true</create>
<export>false</export>
<jdk5>true</jdk5>
<format>true</format>
</componentProperties>
</configuration>
<executions>
<execution>
<id>export-mysql</id>
<phase>process-classes</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
<configuration>
<componentProperties>
<propertyfile>src/main/db/hibernate-mysql.properties</propertyfile>
<outputfilename>target/mysql-ddl.sql</outputfilename>
</componentProperties>
</configuration>
</execution>
</executions>
</plugin>

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>
<!--Annotated classes-->
<mapping class="com.etymgiko.lyricscollection.model.Artist" />
<mapping class="com.etymgiko.lyricscollection.model.Song" />
</session-factory>
</hibernate-configuration>

hibernate-mysql.properties:
# This properties file defines the dialect for the MYSQL database
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

Thursday, February 26, 2009

Hibernate Spring where clause

public List getSongs(Long artistId) {
return getHibernateTemplate().find("from Song as song where song.artist.id = ?", new Object [] {artistId});
}

Followers