Discussion:
Problem Inserting with Hibernate
David Botterill
2005-05-16 18:54:26 UTC
Permalink
I have an application that uses McKoi and Hibernate. I'm converting
from an Access DB to the McKoi DB. My conversion class inserts fine
just using JDBC. However, when I try to use Hibernate to insert
records, the record does not get inserted. The problem seems to be
intermittent. I was able to get ONE insert to work. Here's some
relevant code.

Thanks!
-David

SessionHelper.java
...snip...
public class SessionHelper {



private static SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();


public static void init() throws ExceptionInInitializerError {
String amsHome = System.getProperty("AMS_HOME","c:\\data\\amsjava");
try {
Properties configProps = new Properties();

//configProps.put("hibernate.connection.driver_class","org.gjt.mm.mysql.Driver");

configProps.put("hibernate.connection.driver_class","com.mckoi.JDBCDriver");

configProps.put("hibernate.connection.url","jdbc:mckoi:local://" +
amsHome + "/database/db.conf");
configProps.put("hibernate.connection.username","*****");
configProps.put("hibernate.connection.password","*****");

//configProps.put("hibernate.dialect","net.sf.hibernate.dialect.MySQLDialect");

configProps.put("hibernate.dialect","org.hibernate.dialect.MckoiDialect");

configProps.put("hibernate.cglib.use_reflection_optimizer","false");
configProps.put("hibernate.show_sql","true");
Configuration config = new Configuration();
config.setProperties(configProps);
config.addClass(mil.utarng.ams.data.Academy.class);
config.addClass(mil.utarng.ams.data.Bed.class);
config.addClass(mil.utarng.ams.data.Colors.class);
config.addClass(mil.utarng.ams.data.HighSchool.class);
config.addClass(mil.utarng.ams.data.Position.class);


// Create the SessionFactory
sessionFactory = config.buildSessionFactory();

} catch(Exception e) {
throw new ExceptionInInitializerError(e);
}
}


public static Session currentSession() {
Session s=null;
try {
if(null == sessionFactory) {
SessionHelper.init();
}
s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
//s.connection().setAutoCommit(true);
session.set(s);

}
} catch(HibernateException he) {
System.err.println("HibernateException="+ he);

} catch(ExceptionInInitializerError eiie) {
System.err.println("ExceptionInInitializerError=" + eiie);
}
return s;
}
...snip...

Class where I'm doing the insert:

...snip...
private void addRecord() {
/**
* First we need to create a blank record.
*/
currentRecord = new Academy();
if(currentType.equals(DelegateType.DELEGATE)) {
currentRecord.setPosition(Position.DELEGATE);

this.cmbTitle.getModel().setSelectedItem(PositionFactory.getPosition(Position.DELEGATE));
}

clearFields(this.getContentPane());
this.txtLname.requestFocus();
this.cmbName.setSelectedItem(currentRecord);
this.txtLname.setBackground(Color.RED);
this.txtFname.setBackground(Color.RED);
}
...snip...
private void saveRecord() {
...snip...

Session currentSession = SessionHelper.currentSession();

try {
Transaction t = currentSession.beginTransaction();

currentSession.save(currentRecord);
currentSession.flush();
// currentSession.refresh(currentRecord);
t.commit();
} catch(HibernateException he) {
JOptionPane.showInternalMessageDialog(this,
"Error Saving Record! Write down the following
information and call support:\n" + he.getMessage(),
"Save Error!",JOptionPane.ERROR_MESSAGE);

}
...snip...
James
2005-05-19 02:45:40 UTC
Permalink
It's been a couple of days and no-one has offered any
help. So, foolishly I'll try.

I have used Hibernate Mckoi combination with success.
It has been my experience that any problems are
usually solved by making changes in the mapping files
and not in the code itself.

Are you using the same Academy.hbm.xml file for Mckoi
as you were for Access--i.e. without change?

You say one insert works. Sounds like you are trying
to use same primary key in two different inserts.
Where do the primary keys come from in Access? In
Mckoi, you have sequences. I suspect some
modification of your hbm file is in order.

here is a typical id entry in my hbm files.

<id name="id"
column="xxxxxx_ID"
type="long"
unsaved-value="0" >
<generator class="sequence">
<param
name="sequence">APP.hibernate_sequence</param>
</generator>
</id>

I use a single sequence defined in APP schema for all
tables in database instance.

Hope this gives you some clues as to where to make
adjustments in mapping files.

Best Regards, Jim



---------------------------------------------------------------
Mckoi SQL Database mailing list http://www.mckoi.com/database/
To unsubscribe, send a message to mckoidb-***@mckoi.com
David Botterill
2005-05-22 03:50:03 UTC
Permalink
I finally worked the problem out. It turns out I had to get a new
session each time I refreshed my DataFactory (Not to be confused with
the Hibernate SessionFactory).. Perhaps my "SessionHelper" is incorrect.

Well, thanks for the input. It's working now.

-David
Post by James
It's been a couple of days and no-one has offered any
help. So, foolishly I'll try.
I have used Hibernate Mckoi combination with success.
It has been my experience that any problems are
usually solved by making changes in the mapping files
and not in the code itself.
Are you using the same Academy.hbm.xml file for Mckoi
as you were for Access--i.e. without change?
You say one insert works. Sounds like you are trying
to use same primary key in two different inserts.
Where do the primary keys come from in Access? In
Mckoi, you have sequences. I suspect some
modification of your hbm file is in order.
here is a typical id entry in my hbm files.
<id name="id"
column="xxxxxx_ID"
type="long"
unsaved-value="0" >
<generator class="sequence">
<param
name="sequence">APP.hibernate_sequence</param>
</generator>
</id>
I use a single sequence defined in APP schema for all
tables in database instance.
Hope this gives you some clues as to where to make
adjustments in mapping files.
Best Regards, Jim
---------------------------------------------------------------
Mckoi SQL Database mailing list http://www.mckoi.com/database/
Loading...