I’m new to Hibernate, I downloaded a sample Java app and modified it to be more useful, so far in my app I have two db classes that are defined in my Java app : Actor.java and City.java
Each of them has a .hbm.xml file look like the following :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 17, 2009 11:42:48 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="sakila.entity.Actor" table="actor" catalog="sakila">
<id name="actorId" type="java.lang.Short">
<column name="actor_id" />
<generator class="identity" />
</id>
<property name="firstName" type="string">
<column name="first_name" length="45" not-null="true" />
</property>
<property name="lastName" type="string">
<column name="last_name" length="45" not-null="true" />
</property>
<property name="lastUpdate" type="timestamp">
<column name="last_update" length="19" not-null="true" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="sakila.entity.City" table="city" catalog="sakila">
<id name="cityId" type="java.lang.Short">
<column name="city_id" />
<generator class="identity" />
</id>
<property name="city" type="string">
<column name="city" length="45" not-null="true" />
</property>
<property name="countryId" type="string">
<column name="country_Id" length="45" not-null="true" />
</property>
<property name="lastUpdate" type="timestamp">
<column name="last_update" length="19" not-null="true" />
</property>
</class>
</hibernate-mapping>
The app works great so far. But my question is, what if I need to change my app so that it can handle hundreds of objects like Actor and City in the db, do I need to define all the mapping files ? What if I don’t know at the time of programming what db objects user might query, do I need to define all of them, that seems impossible, so my question is : is the *.hbm.xml files necessary ? In the case I need to write an app that lets user query whatever objects he needs, and I may not even know what’s in the db, how to write such an app ? Doable with Hibernate ?
If your system is so dynamic that you don’t know the tables that a user will be querying then I cannot see how you would be mapping these tables to corresponding POJOs. The primary emphasis of Hibernate is mapping tables in your schema to objects in your Java application, primarily through the Hibernate Query Language (HQL) or one of the programatic APIs that are available.
However, it is possible to use straight SQL in hibernate and return non-managed objects, although this is far more limited in capability than using HQL or the query APIs. To do this you would use code similar to the following:
This transformer maps columns in your SQL query, by name, to properties of the specified POJO. In practice, type conversions are challenging and .setScalar is required in many cases. You can also use Transformers.aliasToEntityMap which returns a list of maps, where each map is a mapping of column name to value.
This is covered, briefly, in section 18.1.5 of the current Hibernate Core documentation.