-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Trouble with liftoff using SQL Server
PostPosted: Sat May 02, 2009 2:09 pm 
Newbie

Joined: Sat May 02, 2009 1:57 pm
Posts: 6
Hey guys!

I'm trying to get my first Hibernate / SQL Server (express) project off the ground and I'm having a really weird time getting going. At this point, I can do the reveng strategy and select objects without issue; however, I cannot get any object to INSERT into the database. I don't get any errors or questionable behavior, but it just will not insert, either through merge, save or saveOrUpdate.

It's very curious because the select works fine.

The table is a very simple, one field varchar( 50 ) and the mapping file looks correct. I thought it might be a permission issue on the SQL Server end of some kind, but I am able to insert through a JDBC Connection without issue, so I think it might be something in my hibernate file.

Has anyone had this problem before? Thanks for any help getting me on track?

Hibernate Cfg:
Code:
<hibernate-configuration>
    <session-factory name="SQLServerFactory">
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.password">mysqlserverpass</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=FGPartsMaster</property>
        <property name="hibernate.connection.username">rroot</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <mapping resource="com/hs/fga/common/client/model/EvanTest.hbm.xml" />
    </session-factory>
</hibernate-configuration>


EvanTest mapping:
Code:
<hibernate-mapping>
    <class name="com.hs.fga.common.client.model.EvanTest" table="evanTest" schema="dbo" catalog="FGPartsMaster">
        <id name="name" type="string">
            <column name="Name" length="50" />
        </id>
    </class>
</hibernate-mapping>


Table:
Code:
CREATE TABLE [dbo].[evanTest](
   [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


Hibernate Java:
Code:
EvanTest eT = new EvanTest();
eT.setName( "My Name Test" );
         
Session session = HibernateFactory.getSessionFactory().openSession();   
session.save( eT ); // Also tried .merge(), .saveOrUpdate() to no avail
session.close();


Top
 Profile  
 
 Post subject: Re: Trouble with liftoff using SQL Server
PostPosted: Tue May 05, 2009 10:24 am 
Newbie

Joined: Sat May 02, 2009 1:57 pm
Posts: 6
Hey guys,

I'm still really struggling with this issue. I have another hibernate instance set up locally running on MySQL and I am able to commit fine.

I'm a little perplexed on where to even start trying to debug this one.

Thanks for your help!

Evan


Top
 Profile  
 
 Post subject: Re: Trouble with liftoff using SQL Server
PostPosted: Wed May 06, 2009 2:22 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
try using transactions: begin a transaction before the save and commit it after, before you close the session.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Trouble with liftoff using SQL Server
PostPosted: Wed May 06, 2009 8:26 am 
Newbie

Joined: Sat May 02, 2009 1:57 pm
Posts: 6
Sanne,

That certainly made it LOOK like it was trying to do something! Thanks for that tip! I think it's definately trying to INSERT now; however, I get an odd looking SQL exception:

SQLServerException: Cannot insert explicit value for identity column in table 'evanTest' when IDENTITY_INSERT is set to OFF.

I'm doing:
Code:
         
EvanTest eT = new EvanTest();
eT.setName( "My Name Test" );
   
Session session = HibernateFactory.getSessionFactory().openSession();
Transaction trans = session.beginTransaction();
         
session.saveOrUpdate( eT );         
trans.commit();


So I'm not explicitly setting the identifier value (name: id, type: long) anywhere in the code.

In the mapping file, I have:
Code:
<id name="id" type="long">
     <column name="id" />
     <generator class="assigned" />
</id>


which looks the same as my previous hibernate classes that worked in MySQL.

Thanks for your help!

E


Top
 Profile  
 
 Post subject: Re: Trouble with liftoff using SQL Server
PostPosted: Wed May 06, 2009 4:22 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Using "assigned" you have to assign the identifier
http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#mapping-declaration-id-assigned

and you shouldn't use an identity column in that case; you probably want to use another identifiergenerator like identity

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Trouble with liftoff using SQL Server
PostPosted: Wed May 06, 2009 5:18 pm 
Newbie

Joined: Sat May 02, 2009 1:57 pm
Posts: 6
Oh well shoot, that's exactly it! Can't believe I didn't catch that.

Thanks again Sanne!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.