Wednesday, 31 October 2012

sqlserver - conversion of a varchar data type to a datetime data type resulted in an out-of-range value

I have an app connecting to a microsoft sqlserver DB.

When I ran the app all was fine & when the tester ran the app it failed with the following Exception:


Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet$FetchBuffer.nextRow(SQLServerResultSet.java:4853)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.fetchBufferNext(SQLServerResultSet.java:1781)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(SQLServerResultSet.java:1034)
at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207)
at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207)
at org.hibernate.loader.Loader.doQuery(Loader.java:825)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2542)
... 41 more


It took me ages to work out what the issue was until I noticed that I was connecting to the database as 'sa' & he was using the schema name.

There is a lot of info on the web about this issue concerning changing the systems Regional settings but that was not my issue. In this case the sqlserver user 'sa' had a Default language of 'English' and the schema user had a language of 'British English'. Changing the Default Language to 'English' solved the problem.



Friday, 22 June 2012

hibernate ClassCastException and Transcient methods

This is an interesting little problem I had.

I have a domain object that has a char(1) column in the database but this is not a boolean. It contains one of two characters 'N' or 'H'.

I mapped this as follows with a handy @Transcient method:

 @Column(name="NHH_HH_IND", nullable=false)
 @Length( max = 1 )
 @NotNull
 @Type(type="java.lang.String")
  public String getHalfHourlyIndicator()
  {
    return mHalfHourlyIndicator;
  }

  public void setHalfHourlyIndicator(String aHalfHourlyIndicator)
  {
    //Integer y = new Integer((String)aHalfHourlyIndicator);
    this.mHalfHourlyIndicator = aHalfHourlyIndicator;
  }
  @Transient
  public boolean isHalfHourlyIndicator()
  {
    if (getHalfHourlyIndicator().equals("H")) {
      return true;
    } else {
      return false;
    }
  }


However when trying to persist this I got a ClassCastException:


java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
at org.hibernate.type.descriptor.java.StringTypeDescriptor.unwrap(StringTypeDescriptor.java:40) at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:52)

But I had no Boolean objects that were not transcient.

The problem appears to be because the Transcient method has the same body IE 'HalfHourlyIndicator'


Changing this method name to something else solved the problem. This is what I ended up with.

 @Column(name="NHH_HH_IND", nullable=false)
 @Length( max = 1 )
 @NotNull
 @Type(type="java.lang.String")
  public String getHalfHourlyIndicator()
  {
    return mHalfHourlyIndicator;
  }

  public void setHalfHourlyIndicator(String aHalfHourlyIndicator)
  {
    //Integer y = new Integer((String)aHalfHourlyIndicator);
    this.mHalfHourlyIndicator = aHalfHourlyIndicator;
  }
  @Transient
  public boolean isHalfHourlyIndicatorAsBoolean()
  {
    if (getHalfHourlyIndicator().equals("H")) {
      return true;
    } else {
      return false;
    }
  }

Tuesday, 12 June 2012

Copying files in eclipse was failing & creating a Snippet.java

When trying to copy a file from one project to another instead of copying the file it creates a file Snippet.java with the file name inside a main method.

WTF?

I did not suss out when this started & I have struggled to solve the issue.
Today I sussed it with some help from the wonderful stackoverflow.

Bizarrely the cause was the Skype 'Click to Call' extension in Chrome.
The stackoverflow solution suggests an upgrade to the extension might fix it but as I do not use skype from my work PC I simply deleted the extension

Wednesday, 28 March 2012

Cucumber-jvm - a first very tentative dabble

Before I start getting in to the meat of BDD & what on earth to do with it I wanted to create a basic standalone project and get the tests working.

I decided to take a look at cucumber-jvm because I have heard good things about it from the Ruby world.
Here is Cucumber-jvm on github from cukes.info.

The git hub link points to the whole shebang & is all maven based. I can't say I am a fan of maven at all.

So I have stripped out the hello world application and just included the jars that are needed for that application.
It can be found here on my github repository.

Included is all you need to run this as an eclipse project except the jars. I use ivy to download the required jars so they are defined in the ivy.xml file.  If you want to know more about ivy see this post I wrote a while ago.

Now I need to delve further in to the world of BDD & work out what I need to do to apply it to my current & new projects. Wish me luck.

Friday, 10 February 2012

hibernate fixes SQLServer char(n) mapping bug in 3.6.8

Hibernate have had an annoying bug where SQLServer columns mapped as char(n) where n > 1 are returned as a char instead of String.

varchar(n) columns are fine.

The hibernate bug is https://hibernate.onjira.com/browse/HHH-2304

It is fixed in 3.6.8 however 3.6.10 is already out so I have just downloaded & tried that.
excellent it works.

Hibernate4 is also out. I have not tried that yet but no doubt will be soon.

Wednesday, 4 January 2012

Multiple search windows in eclipse

This is a really simple but cute little feature.

If you pin one search window using the pin icon at the right hand end of buttons then the next time you do a search eclipse will open up a new Search window.

There is also a search history that you can look through just to the left of the pin icon.

Friday, 23 September 2011

finding the correct spring jar for a known class

With Spring 3 you now have to get all the individual jars that you need for an application.

One solution is obviously to download the lot.

But what if you just want to install what is needed.
How do you easily find which jar a class is in ?

Well I have just found this Spring Bundle Search tool.

You can enter just the class name or the class with its full package name.