Hi all!
Thanks for a great forum. Im pretty new to Hibernate and I'm having an annoying problem.
Task
I have two objects: Consultant and User. The consulant object exists in the database. Now I'm trying to adding a user (not in database) to a consultant (one-to-one related) and store it in the database. But it fails due to conversion errors.
In the mapping files i have tried both the native and identity generator but with no success. (the consultant table is linked to other tables but not the user table)
Problem
I get a conversion error from 108 to INTEGER (regardless of what consultant I use). I think (not sure at all) that the problem is related to the generation of keys, because I have no problems updating existing records (consultant / user combinations)
Does anyone know what this error could be and how to solve it I would be most thankful. Thanks in advance and a happy new Year!
Note
I know that I can combine the objects into a larger one due to the one-to-one relationship (but thats does not suit the rest of the task so well)
Database: MS SQL Server 2005
Hibernate Version: 3.0.5
Exception stack trace
Exception in thread "AWT-EventQueue-0" org.hibernate.exception.GenericJDBCException: could not insert: [test.evalLight.User]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:544)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:533)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:529)
at test.evalLight.admin.ca.ConsultantInfoPanel$1.actionPerformed(ConsultantInfoPanel.java:254)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Unsupported conversion from 108 to INTEGER
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getRowsetField(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at org.hibernate.id.IdentifierGeneratorFactory.get(IdentifierGeneratorFactory.java:50)
at org.hibernate.id.IdentifierGeneratorFactory.getGeneratedIdentity(IdentifierGeneratorFactory.java:35)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1970)
... 39 more
User Mapping File:
Code:
<?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 2005-dec-16 12:21:47 by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
<class name="test.evalLight.User" table="Användare" schema="dbo" catalog="Konsultutvardering">
<id name="id" type="integer" unsaved-value="0">
<column name="AnvändareID" />
<generator class="native" />
</id>
<many-to-one name="consultant" class="test.evalLight.Consultant" column="KonsultID" unique="true"/>
<property name="userName" type="string">
<column name="Användarnamn" length="50" not-null="true" unique="true"/>
</property>
<property name="password" type="string">
<column name="Lösenord" length="50" not-null="true" />
</property>
<property name="role" type="integer">
<column name="Roll" not-null="true" />
</property>
<property name="status" type="integer">
<column name="Status" not-null="true" />
</property>
<property name="created" type="timestamp">
<column name="Skapad" length="16" not-null="true" />
</property>
</class>
</hibernate-mapping>
Consultant mapping fileCode:
<?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 2005-dec-16 12:21:47 by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
<class name="test.evalLight.Consultant" table="Konsult" schema="dbo" catalog="Konsultutvardering">
<id name="id" type="int" unsaved-value="0">
<column name="KonsultID" />
<generator class="native" />
</id>
<one-to-one name="user" class="test.evalLight.User" property-ref="consultant"/>
<many-to-one name="level" class="test.evalLight.ConsultantLevel" fetch="select">
<column name="KonsultnivåID" not-null="true" />
</many-to-one>
<property name="name" type="string">
<column name="KonsultNamn" length="50" not-null="true" />
</property>
<property name="created" type="timestamp">
<column name="Skapad" length="16" not-null="true" />
</property>
<set name="questionnaires" inverse="true">
<key>
<column name="IfyllandeKonsultID" not-null="true" />
</key>
<one-to-many class="test.evalLight.Questionnaire" />
</set>
<set name="targetQuestionnaires" inverse="true">
<key>
<column name="GällerKonsultID" not-null="true" />
</key>
<one-to-many class="test.evalLight.Questionnaire" />
</set>
</class>
</hibernate-mapping>
// The code that caused the exceptionCode:
Session session = HibernateUtil.currentSession();
session.beginTransaction();
User u = new User();
Consultant c = new Consultant();
c = (Consultant) session.load(Consultant.class, model.getCurrentConsultant().getId());
u.setUserName(username.getText());
u.setPassword(password.getText());
u.setRole((Integer) role.getSelectedItem());
u.setStatus((Integer) status.getSelectedItem());
u.setCreated(Calendar.getInstance().getTime());
u.setConsultant(c);
c.setName(name.getText());
c.setLevel((ConsultantLevel) consultantLevel.getSelectedItem());
c.setCreated(Calendar.getInstance().getTime());
c.setUser(u);
session.save(c);
session.persist(u);
session.getTransaction().commit();
SQL
insert
into
Konsultutvardering.dbo.Användare
(KonsultID, Användarnamn, Lösenord, Roll, Status, Skapad)
values
(?, ?, ?, ?, ?, ?)