satar (32) [Avatar] Offline
#1
Majorly bummed... I have been dieing to get to chapter 13 to finally connect Wicket to a database. Actually to my local mySQL database. I figured at some point I would see implementation of the MyDataBase that was hinted at in chapter 8 -- where is it?

I did all the setup for Spring and Hibernate and added the appropriate annotations to two of my domain classes that live in my database but the book ended before showing me how to actually "use" these connections. I even looked for it in the example code thought I might see it there -- did I miss something?
satar (32) [Avatar] Offline
#2
Re: MyDataBase?
So wait, if I am following correctly going back through things, it would seem that MyDataBase would be the same as DiscountsService, which is a bean that points to DiscountsServiceImpl. The service would basically be an instance of a Hibernate connection to my database -- am I following this correctly?

Sorry, I know it isn't good practice to reply to one's self but unfortunately, I may have pulled the trigger too soon on my first post...
dashorst (107) [Avatar] Offline
#3
Re: MyDataBase?
Connecting your database to your services is really out of scope for Wicket: it is something you can read in the spring manual.

Basically what you say is correct: you define a data source, or a connection pool, which you connect to your hibernate session factory. The following XML spring config connects everything with Spring's @Service and @Transactional annotations, and Hibernate's @Entity annotations:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="autodetect" >
xmlns="http://www.springframework.org/schema/beans" xmlnssmiliesi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<bean id="wicketApplication" class="com.mycompany.WicketApplication" />

<bean id="placeholderConfigurer" >
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="false" />
<property name="locations">
<list>
<value>classpath*:/application.properties</value>
</list>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>

<tx:annotation-driven transaction-manager="txManager" />

<!-- setup transaction manager -->
<bean id="txManager" >
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="interceptor" class="com.mycompany.hibernate.HibernateInterceptor">
</bean>

<!-- hibernate session factory -->
<bean id="sessionFactory" >
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.connection.pool_size">5</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.hibernate.cache.use_query_cache">true</prop>
</props>
</property>
<property name="entityInterceptor">
<ref bean="interceptor" />
</property>
<property name="packagesToScan">
<list>
<value>com.mycompany.entities</value>
</list>
</property>
</bean>
<context:component-scan base-package="com.mycompany" />
</beans>

This needs an application.properties to define the ${value}s:
jdbc.driver=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:mem:phonebook
jdbc.username=sa
jdbc.password=
hibernate.dialect=org.hibernate.dialect.HSQLDialect

For mysql you only modify the properties file.
satar (32) [Avatar] Offline
#4
Re: MyDataBase?
Martijn,

I understand that interacting with a database through wicket could probably be a book by itself, especially using Hibernate with Spring in a Wicket context, but I really thought chapter 13 by the name of it was going to at least get me started. It sort of did and then I felt like it left me hanging. If you were to write a book that picked up from there and elaborated more on how to use them I would SO buy it!

At this point, I only have chapter 14 to go before reading your entire book and I loved it! I am an old Java swing / client application developer and have been doing other things for a few years. I came back to this and wanted to learn a solid easy to understand web framework. I struggled with several articles and a few books before I found yours. Your breakdown of examples approach, written in such a way that one can write them as they go along really makes it work for me. Now were is that follow-on hibernate book?

Your book actually explains most of the applicationContext.xml file you mention in the reply below but not the "placeholderConfigurer" bean -- what is that for?

Also, while trolling the internet, I bumped into this artical called "5 Days of Wicket - Designing the backend" that seems to elaborate on this (http://www.mysticcoders.com/blog/2009/03/11/5-days-of-wicket-day-designing-the-backend/) -- wondering what you would think of it.

Thanks, and thank you for writing this book -- I think it is the tech book I have actually read, or will very soon, cover-to-cover.
dashorst (107) [Avatar] Offline
#5
Re: MyDataBase?
Well, the idea was to ensure you could get up and walking with the integration points, not provide a full stack for building enterprise applications. Too many combinations exist to cover that. And the online documentation for most products is adequate: hibernate reference and spring reference are decent enough to find information in. If you want more, Java Persistence with Hibernate is solid, but intimidating (> 2kg, comes in its own shipping container), and Spring in Action (which could do without the whole Spring MVC framework part, IMO) will help you further.

In any case you really should couple the source code of your open source dependencies in your IDE. This gives inline JavaDoc support, and allows you to quickly read up without even having to resort to Google. Having the code available allows for a better understanding of the products and their inner workings (especially when debugging).

For example set a break point in WicketFilter#doFilter() and step through a couple of different requests (page, ajax, form submit, link click). Similarly step through the hibernate code when you persist an entity. This gives invaluable insight into these products and will increase your own knowledge and experience without having to buy a book. Sure you won't grasp everything immediately, but that's ok. You'll learn the grand overview first, and the next time you'll understand even more things.

> Your book actually explains most of the
> applicationContext.xml file you mention in the reply
> below but not the "placeholderConfigurer" bean --
> what is that for?

Note the class="" attribute? Put the value of that in a google search box and it will take you to:

http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

Which claims (copy-pasted from the javadoc):

A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.

The default placeholder syntax follows the Ant / Log4J / JSP EL style:

${...}
Example XML context definition:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>jdbc:${dbname}</value></property>
</bean>
Example properties file:
driver=com.mysql.jdbc.Driver
dbname=mysql:mydb
PropertyPlaceholderConfigurer checks simple property values, lists, maps, props, and bean names in bean references. Furthermore, placeholder values can also cross-reference other placeholders, like:
rootPath=myrootdir
subPath=${rootPath}/subdir
In contrast to PropertyOverrideConfigurer, this configurer allows to fill in explicit placeholders in context definitions. Therefore, the original definition cannot specify any default values for such bean properties, and the placeholder properties file is supposed to contain an entry for each defined placeholder.
If a configurer cannot resolve a placeholder, a BeanDefinitionStoreException will be thrown. If you want to check against multiple properties files, specify multiple resources via the "locations" setting. You can also define multiple PropertyPlaceholderConfigurers, each with its own placeholder syntax.

Default property values can be defined via "properties", to make overriding definitions in properties files optional. A configurer will also check against system properties (e.g. "user.dir") if it cannot resolve a placeholder with any of the specified properties. This can be customized via "systemPropertiesMode".

Note that the context definition is aware of being incomplete; this is immediately obvious to users when looking at the XML definition file. Hence, placeholders have to be resolved; any desired defaults have to be defined as placeholder values as well (for example in a default properties file).

Property values can be converted after reading them in, through overriding the PropertyResourceConfigurer.convertPropertyValue(java.lang.String) method. For example, encrypted values can be detected and decrypted accordingly before processing them.
satar (32) [Avatar] Offline
#6
Re: MyDataBase?
Wow dude, what a thorough explanation on the PropertyPlaceholderConfigurer and not in vain because I not only follow it, but had been wondering in the back of my brain how I was going to vary attributes between types of builds such as development verses production for things like database settings. I see how I am going to do it now -- sweet!

I will take your reference to Spring in Action under advisement. Before I had discovered Wicket, I purchased a couple other "Spring" books that I should probably not mention the names of because they were so hard to follow and seem to never get to a working example that I went out looking for more resources. I discovered Wicket in that process so I guess they did me justice after all smilie

Thanks for spending what I can only imagine is extremely precious time to "enlighten" me so that I can move forward!

-Steve