Hibernate datasource

How do you define hibernate datasource? I have existing application with spring and hibernate. Datasource is defined as:

<DataSource 
	ID="user" 
	serverType="hibernate"
	autoDeriveSchema="true"  
	schemaBean="hibernateSessionFactoryBean"
           beanClassName="org.company.bo.User"
/>

what I’ve got is:

WARN  HibernateDataSource - Declared schemaBean 'hibernateSessionFactoryBean' was not found among existing Hibernate mappi
ngs. Falling back to examining the JDBC metadata.

followed by

java.lang.Exception: table DBTABLE does not exist or contains no columns.

What is the easiest way to integrate already written application/code/mappings into smartgwt? I’ve looked into examples and there are always fields in datasource definition. Do I need to clone hibernate mapping into field emelents ds.xml?

smartGWT version: 2.4.2011-01-06-EVAL

You’re close, just remove your setting of schemaBean. You would generally only use this property if you were not using Hibernate. For Hibernate, the combination of beanClassName and autoDeriveSchema already causes auto-derivation of fields (from org.company.bo.User in your case).

Thank you for your fast answer.

I’ve not described my situation properly:
I have definition of the hibernatesessionfactory springbean where there are all the mappings specified. So I have no annotated classes but *.hbm.xml files there, from which the classes are generated. I put autoderive and beanclassname into datasource definition as I wanted to bind it somehow with that already existing mapping.

Do I understand correctly that all is needed is to have following lines in the file server.properties:

 hibernate.config.lookupStyle: spring
 hibernate.config.bean: mySessionFactory

and mapping is done just by bean class name? Even then it is not annotated class?

<DataSource 
	ID="user" 
	serverType="hibernate"
           beanClassName="org.company.bo.User"
/>

There is something I can’t get as smartGWT is trying to load hibernate.cfg.xml even when I have configuration in the springbean. Do I need to copy setting from bean into hibernate.cfg.xml file? Or there is some flag I need to switch on for framework to stop looking for this file?

We’ll look for the file regardless because some people use both Spring and .cfg.xml concurrently. It is not an error for it to be absent (or you can have it present but empty).

Well, I 've got the error when it is missing. Do I need to switch on something in configuration files to not have it as an error? Because application ends in this point. also when I have it empty it is not reading configuration in the bean.

=== 2011-02-17 11:32:27,103 [80-3] ERROR DataSourceLoader - Exception while attempting to load a DataSource
java.io.FileNotFoundException: C:\dev\apache-tomcat-6.0.29\webapps\wep\WEB-INF\classes\hibernate.cfg.xml (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at com.isomorphic.io.ISCFile.getInputStream(ISCFile.java:346)
        at com.isomorphic.hibernate.HibernateDataSource.initStaticConfigAndSessionFactory(HibernateDataSource.java:425)
        at com.isomorphic.hibernate.HibernateDataSource.initConfigAndSessionFactory(HibernateDataSource.java:339)
        at com.isomorphic.hibernate.HibernateDataSource.init(HibernateDataSource.java:179)
        at com.isomorphic.datasource.BasicDataSource.fromConfig(BasicDataSource.java:165)
        at com.isomorphic.datasource.DataSource.fromConfig(DataSource.java:337)
        at com.isomorphic.datasource.FileSystemDSRepo.loadDS(FileSystemDSRepo.java:110)
        at com.isomorphic.datasource.DataSource.forName(DataSource.java:156)
        at com.isomorphic.datasource.DataSource.forName(DataSource.java:148)
        at com.isomorphic.datasource.DataSource.forName(DataSource.java:143)
        at com.isomorphic.datasource.PoolableDataSourceFactory.makeUnpooledObject(PoolableDataSourceFactory.java:95)
        at com.isomorphic.datasource.PoolableDataSourceFactory.makeObject(PoolableDataSourceFactory.java:102)
        at com.isomorphic.pool.PoolManager.borrowObject(PoolManager.java:82)
        at com.isomorphic.datasource.DataSourceManager.getDataSource(DataSourceManager.java:62)
        at com.isomorphic.servlet.DataSourceLoader.processRequest(DataSourceLoader.java:81)
        at com.isomorphic.servlet.DataSourceLoader.doGet(DataSourceLoader.java:64)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        at com.isomorphic.servlet.BaseServlet.service(BaseServlet.java:152)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

...

Hi,

Just to clarify - could you check your log for messages (just before your specified exception):

BTW: add autoDeriveSchema=“true” to your DataSource definition if you still want auto-derivation of fields.

Regards,
Alius

Hi,

I’ve just committed changes. It allows continuing execution if Spring configured Hibernate session factory is found but failed to create from .cfg.xml file.

You will be able to pick up these changes with next nightly build.

Regards,
Alius

Hi,

thank you, that’s great.

I still don’t understand this hibernate datasource: do I need to put there autoderriveschema if I want to reuse hibernate mapping from hbm.xml files? Is it possible, or the datasource is just using hibernate connection without field mapping? It seems to me, that I would copy field definition from *hbml.xml into *ds.xml and have them in two places.

Hi,

Here is a simple schema:

client tier<—>ds.xml<—>server tier<—>hbm.xml<—>DB

In most extreme case you can define POJOs and mapping (ds.xml) for client-server communications; POJO’s and mapping (hbm.xml) for server-DB communications; some logic on the server side which transfer data client-POJO<—>server-POJO.

Usually you reuse POJOs (client-POJO==server-POJO). Mapping files are still necessary because presentation can differ from persistence:
in hbm.xml:
id
nickName
firstName
lastName

in ds.xml (firstName and lastName will not be visible on the client)
id
nickName
formattedName (calculated field: firstName + lastName; declared in POJO but transient to Hibernate)

When you need same fields for presentation and persistence you can reuse POJO and can reuse Hibernate mapped fields (from hbm.xml) by specifying autoDeriveSchema=“true” (in ds.xml).
Specifying autoDeriveSchema in ds.xml will on fly copy field definitions from Hibernate configuration.

Alius