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 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;
             }
     });
}

HIBERNATE as JPA

persistence.xml

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
<persistence -unit name="helloworld">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <!-- Not needed, Hibernate supports auto-detection in JSE
       <class>hello.Message
    -->
  <properties>
      <property name="hibernate.archive.autodetection"
          value="class, hbm"></property>
      <property name="hibernate.show_sql" value="true"></property>
      <property name="hibernate.format_sql" value="true"></property>
      <property name="hibernate.connection.driver_class"
                value="org.hsqldb.jdbcDriver"></property>
      <property name="hibernate.connection.url"
                value="jdbc:hsqldb:hsql://localhost"></property>
      <property name="hibernate.connection.username"
                value="sa"></property>
      <property name="hibernate.c3p0.min_size"
                value="5"></property>
      <property name="hibernate.c3p0.max_size"
                value="20"></property>
      <property name="hibernate.c3p0.timeout"
                value="300"></property>
      <property name="hibernate.c3p0.max_statements"
                value="50"></property>
      <property name="hibernate.c3p0.idle_test_period"
                value="3000"></property>
      <property name="hibernate.dialect"
                value="org.hibernate.dialect.HSQLDialect"></property>
      <property name="hibernate.hbm2ddl.auto" value="create"></property>
  </properties>
</persistence>

Continue reading

Hibernate, JBoss, PostgreSQL, No suitable driver found

we have the problem :

1
2
3
4
5
...
00:48:51,964 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--0.0.0.0-9180-2) SQL Error: 0, SQLState: 08001
00:48:51,964 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--0.0.0.0-9180-2) No suitable driver found for jdbc:postgresql://109.197.9.4:5433/2match02
00:48:51,964 WARNING [javax.enterprise.resource.webcontainer.jsf.application] (http--0.0.0.0-9180-2) JSF1064: Unable to find or serve resource, style.css, from library, styles.
...

Continue reading

Java HIBERNATE examples (2)

Начало тут

hibernate.cfg.xml example forPostgreSQL

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
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <class>entities.AdminStatus</class>
        <class>entities.Admin</class>
        <class>entities.Adminmenu</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://10.10.10.10:5433/YOURPGDB"/>
            <property name="javax.persistence.jdbc.user" value="PGUSER"/>
            <property name="javax.persistence.jdbc.password" value="PGPASSWD"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
           
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://10.10.10.10:5433/YOURPGDB"/>
            <property name="hibernate.default_schema" value="public"/>
            <property name="hibernate.connection.username" value="PGUSER"/>
            <property name="hibernate.connection.password" value="PGPASSWD"/>
           
            <property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>
           
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
           
        </properties>
    </persistence-unit>
</persistence>
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
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <class>entities.AdminStatus</class>
        <class>entities.Admin</class>
        <class>entities.Adminmenu</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://10.10.10.10:5433/YOURPGDB"/>
            <property name="javax.persistence.jdbc.user" value="PGUSER"/>
            <property name="javax.persistence.jdbc.password" value="PGPASSWD"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
           
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://10.10.10.10:5433/YOURPGDB"/>
            <property name="hibernate.default_schema" value="public"/>
            <property name="hibernate.connection.username" value="PGUSER"/>
            <property name="hibernate.connection.password" value="PGPASSWD"/>
           
            <property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>
           
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
           
        </properties>
    </persistence-unit>
</persistence>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ! _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="toMatchHibernate">
  <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
  <property name="hibernate.connection.username">PGUSER</property>
  <property name="hibernate.connection.password">PGPASSWD</property>
  <property name="hibernate.connection.url">jdbc:postgresql://10.10.10.10:5433/YOURPGDB</property>
  <property name="hibernate.default_schema">public</property>
  <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <property name="hibernate.query.factory_class">info.abnsoft.java.logger.hibernate.HibernateUtil2</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.use_sql_comments">true</property>
  <property name="hibernate.format_sql">false</property>
 </session-factory>
</hibernate-configuration>

About persistence.xml

Setting up an annotations project

Hibernate 3.6 Configuration : Chapter 3. Configuration