JBoss with SQL Server and EJB3 is giving me a hard time

Today I had a bit of a fuss with JBoss and SQL Server. For some reason, when I declare an entity with an Id field, the persistency layer tries to push a bad query to the database.

Let me explain.

I’ve got an entity sort of looking like this:

public class Contract {

@GeneratedValue(strategy = GenerationType.Identity)
private int contractId;

When tryin to persist this through the entity manager SQL trace on MS SQL show a query somewhat looking like:

insert into Contract (contractid, ...) values (null, ...)

You see, a null value. Sounds reasonable, I didn’t set any value on the contractid field. BUT the persistency layer should not try to insert anything into the contractid column, because MSSQL doesn’t allow it. The above SQL results in a “DEFAULT or NULL are not allowed as explicit identity values.” I’m not sure what to try next though, everywhere I looked on the internet, all examples boil down too: “Something similar should work in MS SQL too, but I haven’t tested that…”

Better luck tomorrow. For now, I googled my brains out trying to find anything helpfull.

Anybody out there have any suggestions?

Update on the next day:
It turns out that the problem was the dialect configured in the Hibernate properties in the persistence.xml. By default our development environment enters the right value for Hypersonic DB, while it should actually be like this:

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>

And now it all works. A default SQL Server 2005 with JBoss and the latest MSSQL JDBC driver, all running EJB3.

2 Replies to “JBoss with SQL Server and EJB3 is giving me a hard time”

  1. You just made my night. I defined that in my persistence.xml file but it turns out, at the very bottom of the XML file, there was another dialect defined that overwrote the SQLServer dialect property. I was pulling my hair out for hours trying to figure out what was going on.

    Thank you much.

  2. thanks a ton. U saved my 4 more hours and lots of frustration. I think Hibernate should grow up and include this behaviour so that it is less pain for developers.

Comments are closed.