Hibernate version:2.1.6
I use the JBoss 4.0.0's datasource. I encountered a problem in hibernate source code, there's some lines code like these in net.sf.hibernate.id.TableGenerator.generate() method.
Code:
...
Connection conn = session.getBatcher().openConnection();
...
...
while (rows==0);
conn.commit();
...
When running to "conn.commit();", it thows a SQLException like the following:
Code:
java.sql.SQLException: You cannot commit with autocommit set!
at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:501)
at org.jboss.resource.adapter.jdbc.WrappedConnection.commit(WrappedConnection.java:451)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
It is obviously that the current connection(conn) object is in auto-commit mode, so, how to make the connection(conn) object to be in on-auto-commit mode? The following is my code and configuration files. Should I make some changes on the configuration files?
PO:
Code:
public class Company {
private String id;
private String name;
private String address;
public Company() {
}
........
}
CustomizedIdGenerator.java
Code:
public class CustomizedIdGenerator extends TableGenerator {
private String prefix;
private static final Log log = LogFactory.getLog(CustomizedIdGenerator.class);
public void configure(Type type, Properties params, Dialect d) {
super.configure(type, params, d);
prefix = params.getProperty("prefix") == null ? "Pre" :
params.getProperty("prefix");
}
public synchronized Serializable generate(SessionImplementor session,
Object obj) throws SQLException,
HibernateException {
Integer integer = (Integer)super.generate(session, obj);
return prefix+integer;
}
}
The mapping file:
Code:
...
<id name="id" column="id" type="java.lang.String">
<generator class="com.test.po.CustomizedIdGenerator">
<param name="table">uid_table</param>
<param name="column">next_hi_value_column</param>
<param name="prefix">C</param>
</generator>
</id>
...
The hibernate.cfg.xml
Code:
....
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- properties -->
<property name="connection.datasource">MSSQLDS</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
.....
The hibernate.properties:
Code:
hibernate.jndi.class=org.jnp.interfaces.NamingContextFactory
hibernate.jndi.url=jnp://localhost:1099
hibernate.jndi.java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces