Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    SmartGWT EE and HibernateDaoSupport

    Yes, I've been RTFM'ing the documentation on the SmartGWT EE, Data Integration for CRUD. I have looked at the various options and it seems that for my company needs, the DMI option might be best.

    We are using Spring 2.5.x and Hibernate, and we have already created a number of SpringBean DAO's, an example follows below.

    Looking at the examples in the EE showcase, it shows the POJO's and DAO's a little differently. If we have to change our existing classes, or add new classes for the DMI/CRUD/SmartGWT EE integration, that shouldn't be a problem.

    Code:
    package com.tom.dao.test;
    import java.util.List;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import com.tom.or.test.Community;
    
    public class CommunityDaoImpl extends HibernateDaoSupport implements CommunityDao {
    
    	public List<Community> getCommunity(String communityName) {
    	return  (List<Community>)getHibernateTemplate().find(" from com.tom.or.test.Community where communityName = ?", communityName);
    	}
    
    	public List<Community> getAllCommunities() {		
    	return (List<Community>)getHibernateTemplate().find(" from com.tom.or.test.Community");		
    	}
    
    	public Community getCommunityId(int communityId) {
    		return (Community)this.getHibernateTemplate().get(Community.class, communityId);
    	}
    }
    The Community object is defined as follows:
    Code:
    package com.tom.or.test;
    
    import java.io.Serializable;
    import java.util.*;
    import javax.persistence.*;
    import com.tom.or.test.Data;
    
    @Entity(name = "community")
    public class Community implements Serializable
    {
    	private int communityID;
    	private String communityName;
    	private Set<Data> data;
    	
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "community_id")	
    public int getCommunityID(){ return communityID;}
    public void setCommunityID(int communityID){this.communityID =communityID;}
    
    @Column(name = "community_name")
    public String getCommunityName(){return communityName;}
    public void setCommunityName(String communityName){
    		this.communityName = communityName;
    }
    	
    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @JoinColumn(name = "community_id")
    public Set<Children> getChildren(){ return children; }
    public void setChildren(Set<Children> children) {
    		this.children = children;
    }
    }
    Thanks!
    Tom

    #2
    What's your question?

    What you've showed so far has no business logic and could be replaced with the HibernateDataSource after using the DataSource Wizard in Visual Builder to derive a DataSource from your existing Hibernate mappings, and this would be a gigantic boost in functionality (arbitrary filtering, data paging, built-in client and server validation, etc).

    For any similar samples where you have actual business logic, you can convert those to DMI calls and still use the HibernateDataSource.

    Comment


      #3
      I did ask a question:

      but maybe I wasn't entirely clear.

      I was showing what we currently have ... what the EE Showcase uses,
      and I was trying to bridge the gap. We've already invested time to build Hibernate POJO's using annotations and implemented Hibernate DAO's using the HibernateDaoSupport from Spring.

      I didn't think I needed to show or talk about business logic at this time.
      The exact need, eventually, is is in the SmartGWT EE Showcase for Master-Detail Batch Load and Save. That's ultimately what I want to do.

      > with the HibernateDataSource
      I may have missed this in the showcase ... I'd love to see an example
      This might mean I have to change the datasources I've already created.

      > after using the DataSource Wizard in Visual Builder
      How do we get the Visual Builder? Is that something we can download and use, any licensing? is it free?

      > to derive a DataSource from your existing Hibernate mappings,
      When you say Hibernate mappings, do you mean hbm.xml files?
      With the way we are using Spring, we don't need to use those hbm.xml files.
      if my mappings, do you mean the POJO's? We have those, and I showed an example of how we did a Hibernate POJO using Hibernate annotations.

      I'm certainly not trying to be a jerk ... I am just desperately trying to get a Master-detail Load/Save code working and trying to ascertain how much new code I need to write vs. how much code I can use that is already written, and now much code I need to change.

      Unfortunately, I might have to invest a lot of time with trial and error ...

      Thanks!
      Tom

      Comment


        #4
        In that sample, in the .ds.xml files, serverType="hibernate" specifies that the HibernateDataSource is used and beanClass="[classname]" specifies the class of the bean. Having added your jdbc settings and mappings to hibernate.cfg.xml this is all you need - no server-side Java code other than your bean itself.

        You will only introduce server-side Java code once you need to add business logic, and then only if the business logic goes beyond the things you can simply declare in your DataSource, such as validators, role-based access control, transaction chaining, etc.

        To include Visual Builder, inherit this module:

        <inherits name="com.smartgwtee.tools.Tools"/>

        then programatically call

        com.smartgwtee.tools.client.SCEE.openVisualBuilder();

        The DataSource wizards are launched from the "New" button in the DataSources pane. You want "Hibernate Bean".

        Comment


          #5
          One more clarification - we interrogate a Hibernate Configuration object to find out about mapped beans and currently we're assuming that a hibernate.cfg.xml exists in the classpath. Do you have this file, or have you instead put similar settings into Spring? If it's in Spring can you show a sample?

          Comment


            #6
            EE Data Integation Choices

            Thanks for the information! I looked at the various choices you've provided, and I think the best one mght be the DMI, based on the SmartGWT EE documentation I read yesterday.

            There was the Java Data Integration -> Crud -> Hibernate (Beanless):
            Which as described is for rapid prototyping. We already past that point, so that didn't look like an option. However, if this creates it's own DAO's, then it may be worthwhile, or we'd have to modify our DAO's to do the database methods accordingly.

            There was the Java Data Integration -> Crud -> Hibernate and Spring:
            This didn't look like an option because with Spring, the way we setup the hibernate POJO's with annotations, we don't have any Object.hbm.xml files.

            There was the Java Data Integration -> Crud -> DMI:
            This looked like the best match so far, so I was looking into this.

            As you suggested, we do not have the hibernate.cfg.xml file as we put those settings in Spring in the applicationContext.xml file.

            I can show leave the relevant parts of the file here:
            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            <!--
            	Application context definition for PetClinic on Hibernate.
            -->
            <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            		xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
            		xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
            		xsi:schemaLocation="
            			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
            
            	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
            
            
            	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
            		<property name="driverClassName">
            			<value>com.mysql.jdbc.Driver</value>
            		</property>
            		<property name="url">
            			<value>jdbc:mysql://localhost:3306/myDatabase</value>
                             </property>
            		<property name="username">
            			<value>test_user</value>
            		</property>
            		<property name="password">
            			<value>test_pass</value>
            		</property>
            
            	</bean>
            
            	<!-- Hibernate SessionFactory -->
            
                     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            	       <property name="configurationClass">
               				<value>org.hibernate.cfg.AnnotationConfiguration</value>
            		   </property>
                        <property name="annotatedClasses">
            	         <list>   <value>com.tom.or.test.Community</value>
            	        </list>
            	      </property>
            	      <property name="hibernateProperties">
            	       <props>
            	         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            	         <prop key="hibernate.show_sql">true</prop>
            	         <prop key="hibernate.hbm2ddl.auto">update</prop>
            	         <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
            	         <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            	       </props>
            	      </property>
            	      <property name="dataSource">
            	       <ref bean="dataSource"/>
            	      </property>
                    </bean>
            
            	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            	           <property name="sessionFactory"><ref bean="sessionFactory"/></property>
            	 </bean>
            
                <bean id="communityDao" class="com.tom.dao.test.CommunityDaoImpl">
            	         <property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property>
                </bean>
            
                <bean id="contextApplicationContextProvider" class="com.tom.ws.ApplicationContextProvider">
                </bean>
            
              <!-- enable the configuration of transactional behavior based on annotations -->
              <tx:annotation-driven transaction-manager="transactionManager"/>
            
               <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                  <property name="sessionFactory"><ref local="sessionFactory"/></property>
                </bean>
            
                <bean id="hBusinessService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                        <property name="transactionManager"><ref local="transactionManager"/></property>
                        <property name="target"><ref local="actUserDao"/></property>
                         <property name="transactionAttributes">
                            <props>
                                <prop key="*">PROPAGATION_REQUIRED</prop>
                            </props>
                        </property>
                </bean>
            
            	<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
            
            	<!--
            		Activates various annotations to be detected in bean classes:
            		Spring's @Required and @Autowired, as well as JSR 250's @Resource.
            	-->
            	<context:annotation-config/>
            
            </beans>
            Finally, my plans are to get the local DMI version working in my test environment, and then I'll change/add code to have it work with my tables.

            Again, I have a master-detail table relations that I need to get working with the UI to/from database ASAP.
            I'm very new to SmartGWT, and so I've been working with the non EE UI Widgets for the past month. I can get data out of the database and onto the widgets, but when writing the data back ... I need the data to get to the database and not just an XML or JSON file.

            Thanks!
            Tom

            p.s. If you need any more information from me, please let me know.

            Comment

            Working...
            X