Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

object - relational mapping problem in Hibernate with Spring

$
0
0
Hello everyone.
I have the following exception when mapping object - relational transaction in Hibernate configured with Spring:
Code:

INFO: Loaded JDBC driver: org.postgresql.Driver
Exception in thread "main" java.lang.NullPointerException
        at mypackage.MyDataAccessObject.savePerformanceToDataBase(MyDataAccessObject.java:31)
        at org.springframework.prospring.ticket.db.Main.main(Main.java:32)
Java Result: 1

This is the configuration file in XML:
Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
   
    <bean id="myDataSource"
          class="org.apache.commons.dbcp.DataSource">
             
    <property name="driverClassName">
        <value>org.postgresql.Driver</value>
    </property> 
    <property name="url">
        <value>jdbc:postgresql://localhost:5432/Entertainment</value>
    </property>
    <property name="username">
        <value>shop</value>
    </property>
    <property name="password">
        <value>shop123</value>
    </property>
             
          </bean>
   
    <bean id="mySessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="annotatedClasses">
        <list>
                <value>org.springframework.prospring.ticket.domain.PerformanceHibernateTest</value>
        </list>
      </property>
      <property name="hibernateProperties">
          <props>
              <prop key="hibernate.dialect">
                  org.hibernate.dialect.PostgreSQLDialect
              </prop>
          </props>
      </property>
      <property name="dataSource">
          <ref bean="myDataSource" />
      </property>
    </bean>
   
    <bean id="mytransactionmanager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
          <ref bean="mySessionFactory"/>
      </property>     
    </bean>
    <bean id="myDataaccessObject" class="mypackage.MyDataAccessObject">
        <property name="sessionFactory">
          <ref bean="mysessionFactory">
        </property>
      </bean>
  </beans>

This is the class PerformanceHibernateTest:

Code:

package org.springframework.prospring.ticket.domain;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;


@Entity
public class PerformanceHibernateTest  {
   
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
   
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateAndTime = new Date();

    private int priceStructureId =8;
    private int showId =9;

    public PerformanceHibernateTest() {
    }
   
    public PerformanceHibernateTest(int id, Date dateAndTime, int priceStructureId, int showId) {
        this.id = id;
        this.dateAndTime = dateAndTime;
        this.priceStructureId = priceStructureId;
        this.showId = showId;
    }
}

This is the class MyDataAccessObject:

Code:


package mypackage;


import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.prospring.ticket.domain.PerformanceHibernateTest;
import org.springframework.transaction.annotation.Transactional;


public class MyDataAccessObject {
   
   
    private HibernateTemplate ht;
       
   
        public void setSessionFactory(SessionFactory sessionFactory) {
                this.ht = new HibernateTemplate(sessionFactory);
        }
       
        @SuppressWarnings("unchecked")
        @Transactional
       
   
        public void savePerformanceToDataBase(PerformanceHibernateTest pht) {
               
                ht.persist(pht);
        }
 
}

This is the class Main:

Code:

package org.springframework.prospring.ticket.db;

import java.util.Date;
import mypackage.MyDataAccessObject;
import org.springframework.prospring.ticket.domain.PerformanceHibernateTest;

public class Main {


    public static void main(String[] args) {
        Date date = new Date();
        PerformanceHibernateTest pht = new PerformanceHibernateTest(6, date, 7, 8); 
        MyDataAccessObject mdao = new MyDataAccessObject();
        mdao.savePerformanceToDataBase(pht);
     
    }
}

Please help to improve it.

Viewing all articles
Browse latest Browse all 297

Trending Articles