I have now done quite a bit of testing in an effort to identify why this problem should occur in version 4.1.2.
Firstly, I tested against MySql on the basis that tis is main stream and should work
HilbernateUtil.java
Code:
package hibernatetest1;
//~--- non-JDK imports --------------------------------------------------------
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
private static final Configuration configuration;
private static final ServiceRegistry serviceRegistry;
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
configuration = new Configuration();
configuration.addClass(hibernatetest1.Countries.class);
configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
configuration.setProperty("hibernate.connection.url",
"jdbc:mysql://localhost:3306/mybusinessrecords");
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.password", "secret");
configuration.setProperty("show_sql", "false");
configuration.configure("hibernate.cfg.xml");
serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
@SuppressWarnings("unchecked")
public static void closeSession() {
Session s = session.get();
if (s != null) {
s.close();
}
session.set(null);
}
@SuppressWarnings("unchecked")
public static Session openSession() {
Session s = session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
}
hibernate.cfg.xml
Code:
<?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>
</session-factory>
</hibernate-configuration>
countries.java
Code:
package hibernatetest1;
/**
* Class description
*
*
* @version Enter version here..., 10/03/09
* @author Enter your name here...
*/
public class Countries implements java.io.Serializable {
private Integer country_id;
private String country;
/**
* Constructs ...
*
*/
public Countries() {
}
/**
* @return the country_id
*/
public Integer getCountry_id() {
return country_id;
}
/**
* @param country_id the country_id to set
*/
public void setCountry_id(Integer country_id) {
this.country_id = country_id;
}
/**
* @return the country
*/
public String getCountry() {
return country;
}
/**
* @param country the country to set
*/
public void setCountry(String country) {
this.country = country;
}
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("country_id = ");
sb.append(country_id);
sb.append("\n");
sb.append("country = ");
sb.append(country);
sb.append("\n");
return sb.toString();
}
}
countries.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class batch-size="10" name="hibernatetest1.Countries" table="COUNTRIES">
<id name="country_id" type="integer">
<column name="COUNTRY_ID" not-null="false"/>
<generator class="assigned"/>
</id>
<property name="country" type="string">
<column length="22" name="COUNTRY" not-null="false" />
</property>
</class>
</hibernate-mapping>
HibernateTest1.java
Code:
package hibernatetest1;
//~--- non-JDK imports --------------------------------------------------------
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
//~--- JDK imports ------------------------------------------------------------
/**
*
* @author DBolsover
*/
public class HibernateTest1 {
/**
* Method description
*
*/
private void init() {
System.out.println(System.getProperty("java.class.path"));
Session session = HibernateUtil.openSession();
Countries country = (Countries) session.createCriteria(Countries.class).add(Restrictions.eq("country_id",
1)).uniqueResult();
HibernateUtil.closeSession();
System.out.println(country.toString());
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
HibernateTest1 ht1 = new HibernateTest1();
ht1.init();
}
}
Result - no problems - Hibernate performs as expected and should - :-)
Next, I tested against a PervasiveSQL database:
HibernateUtil.java
Code:
package hibernatetest3;
//~--- non-JDK imports --------------------------------------------------------
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
private static final Configuration configuration;
private static final ServiceRegistry serviceRegistry;
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
configuration = new Configuration();
configuration.addClass(hibernatetest3.Currency.class);
configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
configuration.setProperty("hibernate.connection.driver_class", "com.pervasive.jdbc.v2.Driver");
configuration.setProperty("hibernate.connection.url",
"jdbc:pervasive://127.0.0.1:1583/JS2010R2;encoding=cp1252");
configuration.setProperty("hibernate.connection.username", "");
configuration.setProperty("hibernate.connection.password", "");
configuration.setProperty("show_sql", "false");
configuration.configure("hibernate.cfg.xml");
serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
@SuppressWarnings("unchecked")
public static void closeSession() {
Session s = session.get();
if (s != null) {
s.close();
}
session.set(null);
}
@SuppressWarnings("unchecked")
public static Session openSession() {
Session s = session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
}
hibernate.cfg.xml
Code:
<?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>
<property name="hibernate.dialect">hibernatetest3.PervasiveDialect</property>
</session-factory>
</hibernate-configuration>
HibernateTest3.java
Code:
package hibernatetest3;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
public class HibernateTest3 {
private void loadDriver() {
try {
Class.forName("com.pervasive.jdbc.v2.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
private void init() {
System.out.println(System.getProperty("java.class.path"));
Session session = HibernateUtil.openSession();
Currency currency = (Currency) session.createCriteria(Currency.class).add(Restrictions.eq("currency",
"£")).uniqueResult();
HibernateUtil.closeSession();
System.out.println(currency.toString());
}
public static void main(String[] args) {
HibernateTest3 ht3 = new HibernateTest3();
ht3.loadDriver();
ht3.init();
}
}
Using the Pervasive driver, I have to force loading of the driver with a call to Class.forName("com.pervasive.jdbc.v2.Driver");
Otherwise, an exception is thrown at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192) and the test fails.
Not quite sure where to go from here - clearly there is some issue with PervasiveSQL driver that prevents it from laoding in the same way as the MySql driver - but I have no idea why or where to go from here...
If required I can provide copies of the PervasiveDialect Currency class and mappings.
db