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

How to add DATABASE access in Dynamic Web Project ?

We will get such structure :

Add DATABSE in Dynamic Web Projet

Add DATABSE in Dynamic Web Projet

1. Create in your WEB_ROOT these folders: WEB-INF/database
2. Create file “datapase.properties” :

1
2
3
4
5
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc\:postgresql\://109.197.1.1\:5433/yourDB
jdbc.username=yourLogin
jdbc.password=yourPWD
jdbc.showSql=true

3. Create DataSource.xml file – it contains bean “dataSource” and gets value from “datapase.properties” file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<beans
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>/WEB-INF/database/database.properties</value>
    </property>
</bean>
 
<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>
 
</beans>

4. Create Hibernate.xml file – it cares about Spring Transactions, Hibernate Session Factory :

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
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="UTF-8"?>
<beans
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       ">


    <!-- Hibernate transaction manager -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- +1 -->
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <!-- Enable Hibernate's automatic session context management
                   Spring manages the transaction, and it doesn't need of that the session is holding in a
                   hibernate thread.
               <prop key="hibernate.current_session_context_class">thread</prop>
               -->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>

            </props>
        </property>

        <property name="packagesToScan" value="ru.tomatch.admin.model" />

        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>

        <property name="exposeTransactionAwareSessionFactory">
            <value>false</value>
        </property>


    </bean>
</beans>

5. And last file applicationContext.xml :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<beans
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   
    <!-- Database Configuration -->
    <import resource="./database/DataSource.xml"/>
    <import resource="./database/Hibernate.xml"/>
 
    <!-- Auto scan the components -->
    <context:component-scan base-package="some.your.package1" />    
    <context:component-scan base-package="some.your.package2" />    

</beans>

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

ORACLE : HOWTO create user and something else

call imp system/%1@XE file=%2 log=import.log fromuser=USER touser=USER

call sqlplus USER/password@XE @scripts\update.sql

CREATE USER {user} IDENTIFIED BY "{password}" DEFAULT TABLESPACE "USER_DATA" TEMPORARY TABLESPACE "TEMP" QUOTA UNLIMITED ON "USER_DATA";

GRANT "CONNECT" TO {user} ;
GRANT "DBA" TO {user} ;
GRANT "RESOURCE" TO {user} ;

GRANT SELECT ON sys.v_$session TO {user};
GRANT CREATE TRIGGER TO {user};
GRANT CREATE TYPE TO {user};

of course change {user} & {password}