Showing posts with label hibernate. Show all posts
Showing posts with label hibernate. Show all posts

Thursday, 25 June 2015

Transient and still getting the error "MappingException: Could not determine type"

So I have a POJO with a field annotated as @Transient as in

private Workbook workbook;
    @Transient
    public Workbook getWorkbook() {
        return workbook;
    }
    public void setWorkbook(Workbook workbook) {
        this.workbook = workbook;
    }


But I am still getting the error:

Caused by: org.hibernate.MappingException: Could not determine type for: org.apache.poi.ss.usermodel.Workbook,


Solution: Check your import for @Transient.

If you are using 'import java.beans.Transient;' then you have the wrong one.

It wants to be 'import javax.persistence.Transient'


It looks like this error depends on the type of object you are making transient. If the object is String then it does not complain. In my case I had a poi Workbook class.

Thursday, 2 October 2014

Hibernate - annotation mappings and misleading SerializationException

Took me a few hours to get to the bottom of this.
I was getting the following Exception, which did not make sense as the deSerialization test passed OK.
I have a domain object that I had proved through a unit test that I could serialize & deserialize successfully.


My domain object contained the following object which if I removed then worked OK:


But tomcat & my integration test were both failing with the following Exception.


It turns out I had missed out an annotation, obvious really but I could not spot the error & the Exception was totally misleading. What had I missed? The '@manyToOne' annotation. Twit.

Wednesday, 13 November 2013

Enhancing Apache DBUtils when column names do not match getter & setter names

In a recent post on running stored procedures I made use of Apache's DBUtils package as a basic ORM.

I had a problem where certain fields in my POJO were not getting populated.
The reason being that DBUtils does not make use of the '@Column' annotation and therefore some extra code is needed.

In our case it occurred for every column that had an underscore in the name but not in the POJO getters & setters.

A snipped from my POJO could look like this.


Using the same Dao in the post I refferred to above the ResultSetHandler will now be created as follows:


Friday, 8 November 2013

Hibernate - accessing database data using views

I have recently been working on a database for which I had no control
of the structure and it certainly was not laid out as I would have desired.

Interestingly the application that we were replacing had created convenience Views to access the data.
These Views were often complex typically including JOINS across several tables.

So it may be obvious but I decided to map some of my POJOs to these views.

A few provisos:

  • The view needs to be designed such that there is still a column(s) that can be mapped to an ID.
  • The views are clearly critical to the application so just as in the creation of tables the creation of these views MUST be under version control

Stored Procedures or functions that return POJO objects using hibernate

I have an application that needs to run some stored procedures that want to return List but I struggled with how to get this working.

To quote from the hibernate docs this is an area that they need to work on. It says:

Warning

This is an area in Hibernate in need of improvement. In terms of portability concerns, this function handling currently works pretty well from HQL; however, it is quite lacking in all other aspects.

This is my eventual solution.

First off a Dao class

Here is the interface that the Dao will implement

Then the generic Dao MetOfficeDao
Note the following:
  1. The use of ResultSetHandler from Apache is a basic ORM
  2. JDBC parameters start from '1'
  3. getSession().connection(); is deprecated


Just for completeness - here is an example stored procedure for SQLServer
Note the line:
SET NOCOUNT ON
This is needed so that the procedure just returns the data and not the number of rows returned





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;
    }
  }

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.

Thursday, 10 March 2011

Logging bind variables with hibernate

You could use something like p6spy as suggested by a friend of mine Andy, but I wanted something that was trivial to configure and potentially available to the live application and easy to set up.

After a bit of digging I found Sathya's blog with the solution I needed.

I already had the log4j jar.
What I was missing was the slf4j-log4j12 jar

and the following line in my log4j.properties

log4j.logger.org.hibernate.type=TRACE



Tuesday, 17 March 2009

org.hibernate.DuplicateMappingException and tomcat

There are probably many reasons why you might get a DuplicateMappingException from an application using hibernate. Not least because you have a mapping defined twice.

However I have just found this reason and it is new to me.

In the tomcat logs I found this:
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping uk.co.formfill.dfcommon.domain.dfwv.Action
at org.hibernate.cfg.Mappings.addClass(Mappings.java:118)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:154)
at org.hibernate.cfg.Configuration.add(Configuration.java:386)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:427)
at org.hibernate.cfg.Configuration.addFile(Configuration.java:267)

Looking further up the tomcat logs to just before the first Exception stack trace there was this:
17-03-2009 10:44:22 [Thread-1] INFO  - Reading mappings from file: D:\apache-tomcat-6.0.14\work\Catalina\localhost\dfweb\loader\uk\co\formfill\dfcommon\hibernate\mappings\dfwv\actions.hbm.xml
17-03-2009 10:44:22 [Thread-1] INFO - duplicate import: uk.co.formfill.dfcommon.domain.dfwv.Action->uk.co.formfill.dfcommon.domain.dfwv.Action
17-03-2009 10:44:22 [Thread-1] INFO - duplicate import: uk.co.formfill.dfcommon.domain.dfwv.Action->Action
17-03-2009 10:44:22 [Thread-1] INFO - Mapping class: uk.co.formfill.dfcommon.domain.dfwv.Action -> ACTIONS

So I looked in D:\apache-tomcat-6.0.14\work\Catalina\localhost\dfweb and that was empty. Strange.

The same war worked fine on a different installation and the cause was in the tomcat configuration.

The offending line was in tomcat's context.xml with the problem being the overriding of the antiJARLocking property which had been done beacuse of some historical apps..
<Context antiJARLocking="true">

Removing this and all is fine. Looking at the tomcat documentation I can not see why this problem might occurr but hey ho, it is fixed.

Saturday, 5 April 2008

hibernate3 - hibernateQueryException - Class is not mapped

I have been having a problem for a while trying to get a basic hibernate3 and Spring 2.5 example working. Using old style hibernate CLASS_NAME.hbm.xml files worked fine. When I changed the context.xml file to try & use annotations instead I got the following error:

Exception in thread "main" org.springframework.orm.hibernate3.HibernateQueryException:
Test is not mapped [from Test t where t.name = ?];
nested exception is org.hibernate.hql.ast.QuerySyntaxException:
Test is not mapped [from Test t where t.name = ?] at
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:640)


Check the imports of your '@Entity' annotations in your beans

If it is:
    org.hibernate.annotations.Entity;
Then change it to:
    import javax.persistence.Entity;
and life will be sweet.