Default value in HIBERNATE Database independently

How to initialize Field in Hibernate @Entity without using @Column( columnDefenition=”” ) ?
WHY?
Because columnDefinition is DB dependently option.

Imagine you have some @Entity :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    @Column( name = "insstmp", nullable = false )
    @Type( type = "org.joda.time.contrib.hibernate.PersistentDateTime" )
    private DateTime insstmp;// date without time

    @Column( name = "updstmp", nullable = false )
    @Type( type = "org.joda.time.contrib.hibernate.PersistentDateTime" )
    private DateTime updstmp;// date without time

    /**
     * We set default value in case if the value is not set yet.
     */

    @PrePersist
    public void prePersist() {

        if ( this.updstmp == null ) {
            // Default value
            this.updstmp = getUpdstmp();
        }
        if ( this.insstmp == null ) {
            // Default value
            this.insstmp = getInsstmp();
        }
    }


    /**
     * Getter.
     *
     * @return the insstmp
     */

    public final DateTime getInsstmp() {

        return insstmp == null ? new DateTime( DateTimeZone.UTC ) : insstmp;
    }

    /**
     * Getter.
     *
     * @return the updstmp
     */

    public final DateTime getUpdstmp() {

        return updstmp == null ? new DateTime( DateTimeZone.UTC ) : updstmp;
    }

Hibernate-SQLite how to add in Java project

http://code.google.com/p/hibernate-sqlite/

1.add repository in the pom.xml file(between the repositories tag)

1
2
3
4
<repository>
  <id>hibernatesqlite-maven</id>
  <url>https://hibernate-sqlite.googlecode.com/svn/trunk/mavenrepo</url>
</repository>

2.add dependency in the pom.xml file(between the dependencies tag)

1
2
3
4
5
6
<!-- hibernate sqlite dialect -->
<dependency>
  <groupId>com.applerao</groupId>
  <artifactId>hibernatesqlite</artifactId>
  <version>1.0</version>
</dependency>

3.modify the hibernate dialect configuration like:

1
<property name="dialect">com.applerao.hibernatesqlite.dialect.SQLiteDialect</property>

4.maven your project and the hibernate-sqlite is ready to use.

Additionally, the version of hibernate used in hibernatesqlite-1.0 is 3.2.6.ga, and if you want to use another version of hibernate, you can exclude the hibernate like this:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- hibernate sqlite dialect -->
<dependency>
        <groupId>com.applerao</groupId>
        <artifactId>hibernatesqlite</artifactId>
        <version>1.0</version>
        <exclusions>
                <exclusion>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate</artifactId>
                </exclusion>
        </exclusions>
</dependency>

and add hibernate dependency like this:

1
2
3
4
5
6
<!-- hibernate jars -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate</artifactId>
  <version>3.2.7.ga</version>
</dependency>

CRUD operation in Hibernate (re-link)

http://mrbool.com/crud-operation-in-hibernate/26236

This article will discuss about basic CRUD operations in hibernate.
add to favorites
Today, we are going to discuss about basic CRUD operations in Hibernate.

Pre requirement
We are going to discuss about 4 main functionality:

Create a record
Read a record
Update a Record
Delete a Record
We make a class Course and we will perform the CRUD operation on this class.

Read more: http://mrbool.com/crud-operation-in-hibernate/26236#ixzz2AuiKc9c7 ….

Hibernate SPRING ~ howto use ANONYMOUS CLASS

1
2
3
4
5
6
7
8
9
10
//anonymous inner classes can only access local variables if they are declared as final  
public Pet getPetById(final String id) {
     return (Pet) getHibernateTemplate().execute(new HibernateCallback() {
             public Object doInHibernate(Session session) {
                     HibernateTemplate ht = getHibernateTemplate();
                     // … can access variable “id”
                     return myPet;
             }
     });
}