Announcement Announcement Module
Collapse
No announcement yet.
Odd problems with Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Odd problems with Hibernate

    (Mapping files and spring config at end of message, the code is running in a JUnit test and I've only got a single test so I don't think I need to worry about sessions/setup being carried across tests.)

    This works (SQL is select statement):

    Code:
    DealTypeService dealTypeService = 
    	(DealTypeService)factory.getBean("dealTypeService");
    DealType dealType = dealTypeService.getDealType(1);
    This doesn't (SQL select followed by update -- which fails):

    Code:
    DealService dealService = 
    	(DealService)factory.getBean("dealService");
    Deal deal = dealService.getDeal(1);
    Any idea why I'm getting an extra update? Is Spring doing something in the background that I'm missing, or is this purely a Hibernate problem? The interesting thing is if I try to call the dao directly from the test, I get a NullPointerException:

    Code:
    long id = 1;
    DealDaoImpl dealDao = new DealDaoImpl();
    Deal deal = dealDao.findById(id);
    Thanks for any help.

    Colin

    DealServiceImpl:

    Code:
    public DealServiceImpl(DealDao dao)
    {
    	dealDao = dao;
    }
    
    public Deal getDeal(int id)
    {
    	return dealDao.findById(id);
    }
    DealDaoImpl:

    Code:
    public Deal findById(long id)
    {
        Deal instance = (Deal)getHibernateTemplate()
            .get(Deal.class, new Long(id));
        return instance;
    }
    DealType.hbm.xml:

    Code:
    <hibernate-mapping>
        <class name="com.travelgator.EntityUpdate.model.DealType" table="t_deal_type" catalog="tg_development" dynamic-update="false" dynamic-insert="false" select-before-update="false">
            <id name="dealTypeId" type="java.lang.Long" unsaved-value="-1" column="deal_type_id">
                <generator class="native" />
            </id>
            <property name="description" type="string" column="description" length="250" not-null="true" />
            <set name="Deals" inverse="true" cascade="all-delete-orphan">
              <key column="deal_type_id" />
              <one-to-many class="com.travelgator.EntityUpdate.model.Deal" />
            </set>
        </class>
    </hibernate-mapping>
    Deal.hbm.xml:
    Code:
    <hibernate-mapping>
        <class name="com.travelgator.EntityUpdate.model.Deal" table="t_deal" catalog="tg_development" 
              select-before-update="false" optimistic-lock="version" dynamic-insert="false" dynamic-update="false">
            <id name="dealId" type="java.lang.Long" unsaved-value="-1" column="deal_id">
                <generator class="native" />
            </id>
            <version name="version" type="int" column="version" />
            <many-to-one name="dealType" class="com.travelgator.EntityUpdate.model.DealType" column="deal_type_id" />
            <property name="bookingAgentId" type="int" column="booking_agent_id" not-null="true" />
            <property name="promoteStart" type="timestamp" column="promote_start" length="19" />
            <property name="promoteEnd" type="timestamp" column="promote_end" length="19" />
            <property name="dealStart" type="timestamp" column="deal_start" length="19" />
            <property name="dealEnd" type="timestamp" column="deal_end" length="19" />
            <property name="description" type="string" column="description" length="65535" />
            <property name="propertyId" type="int" column="property_id" not-null="true" />
            <property name="propertyVersion" type="int" column="property_version" not-null="true" />
            <property name="tourId" type="int" column="tour_id" not-null="true" />
            <property name="tourVersion" type="int" column="tour_version" not-null="true" />
            <property name="carRentalId" type="int" column="car_rental_id" not-null="true" />
            <property name="facilityId" type="int" column="facility_id" not-null="true" />
            <property name="facilityVersion" type="int" column="facility_version" not-null="true" />
            <property name="airlineId" type="int" column="airline_id" not-null="true" />
            <property name="trainId" type="int" column="train_id" not-null="true" />
            <property name="cityId" type="int" column="city_id" not-null="true" />
            <property name="stateId" type="int" column="state_id" not-null="true" />
            <property name="countryId" type="int" column="country_id" not-null="true" />
            <property name="uri" type="string" column="uri" length="65535" />
            <property name="activeFlag" type="byte" column="active_flag" not-null="true" />
            <property name="createDate" type="timestamp" column="create_date" length="19" />
            <property name="lastUpdateDate" type="timestamp" column="last_update_date" length="19" />
            <property name="inactivateDate" type="timestamp" column="inactivate_date" length="19" />
        </class>
    </hibernate-mapping>
    Spring config:
    Code:
    <beans>
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/tg_development" />
        <property name="username" value="root" />
        <property name="password" value="root" />
      </bean>
      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
          <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
            <prop key="hibernate.show_sql">true</prop>
          </props>
        </property>
        <property name="mappingDirectoryLocations">
          <list>
            <value>classpath:/com/travelgator/EntityUpdate/model/mapping/</value>
          </list>
        </property>
      </bean>
      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
          <ref bean="sessionFactory" />
        </property>
      </bean>
      
      <!-- DAOs -->
      <bean id="dealDao" class="com.travelgator.EntityUpdate.dao.DealDaoImpl">
        <property name="hibernateTemplate">
          <ref bean="hibernateTemplate" />
        </property>
      </bean>
      <bean id="dealTypeDao" class="com.travelgator.EntityUpdate.dao.DealTypeDaoImpl">
        <property name="hibernateTemplate">
          <ref bean="hibernateTemplate" />
        </property>
      </bean>
      
      <!-- Services -->
      <bean id="dealService" class="com.travelgator.EntityUpdate.service.DealServiceImpl">
        <constructor-arg>
        <ref bean="dealDao" />
        </constructor-arg>
      </bean>
      <bean id="dealTypeService" class="com.travelgator.EntityUpdate.service.DealTypeServiceImpl">
        <constructor-arg>
        <ref bean="dealTypeDao" />
        </constructor-arg>
      </bean>
    </beans>

  • #2
    As it turns out, the problem was one of my properties was defined as int in the mapping file and the setter was int, but the getter was long (or the other way around). Once I fixed that up, I'm back to making mistakes that cause easier to figure out errors...

    Colin

    Comment

    Working...
    X