Not as crazy as you think. Yes, its difficult to get a answer from SO since all hibernate folks here use spring or maven or some very fancy tool to ease hibernate configuration.
Here is what I did.
Copied all library's to classpath. Created a hibernate.properties and hibernate.xml file in my src folder.
The properties file has
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
In your java main you can programmatically specify the mysql server, username and password (mind you took me 2 days to get this damn thing working, with little help from SO).
synchronized (this) {
if (sessionFactory == null) {
try {
String connection = "jdbc:mysql://"
+ Globals.DBSERVER.trim()
+ "/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
log.debug("Connection URL "+connection) ;
Configuration configuration = new Configuration();
configuration
.setProperty("hibernate.connection.username", Globals.DB_USER_NAME.trim())
.setProperty("hibernate.connection.password", Globals.DB_PASSWORD.trim());
configuration.configure();
sessionFactory = configuration
.buildSessionFactory(new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry());
} catch (Exception e) {
log.fatal("Unable to create SessionFactory for Hibernate");
log.fatal(e.getMessage());
log.fatal(e);
e.printStackTrace();
}
}
if (sessionFactory == null) {
log.fatal("Hibernate not configured.");
System.exit(0);
}
The XML file has
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- other mappings -->
<mapping resource="com/mcruiseon/server/hibernate/UserDetails.hbm.xml" />
</session-factory>
</hibernate-configuration>
Make sure you have those hbm.xml file in a folder (inside of src) com.mcruiseon.server.hibernate (and /carpool in some cases).
The same folder should also have POJO's corresponding to the hbm file. I suggest that you keep your db column names EXACTLY same as your variable names makes life very simple (Contrary to what some silly people may advice). Dont use names like t_age
instead use age
(no acronyms).
Example of hbm file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 9 Jun, 2010 11:14:41 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="com.mcruiseon.common.concrete.UserDetailsConcrete"
table="userDetails">
<id name="identityHash" type="java.lang.String">
<column name="identityHash" />
<generator class="assigned" />
</id>
<property name="fullName" type="java.lang.String">
<column name="fullName" />
</property>
<!-- other property -->
</class>
</hibernate-mapping>
Create a UserDetailsConcrete in com/mcruiseon/common/concrete folder
Ensure that you have all variables private (identityHash, fullName... etc). Ensure that you have getters and setters all public. Infact auto generate it (if you have eclipse, sorry). DONT have spelling mistakes and capitalization mistakes. Copy paste to make sure.
You should have it working.