Categories
Coding

ClassCastExceptions and Hibernate mappings

I just had an issue where Hibernate was throwing ClassCastExceptions when trying to persist an entity to the database. I tracked the problem down a specific property – a byte[]. I had just switched my entity mappings from a hbm.xml file to an annotations-based approach. The solution was fairly straightforward, as it turned out – just specify the mapping type explicitly as “binary”. It seems that Hibernate may be attempting to map it as a blob type, which doesn’t map transparently to primitive byte arrays (yet – I see there is a PrimitiveByteArrayBlobType in the Hibernate Annotations API). Meanwhile, I just declare the mapping like so:




  @Column
  @Type(type="binary")
  public byte[] getRawData() {
    return rawData;
  }

Categories
Coding

Eclipse 3.1 M6 and Generics

The Eclipse compiler seems to choke on the following code, which is some generics-related issue. Consider the following:

private Set<ActionState> stateHistory = new TreeSet<ActionState>();

Now, I know a TreeSet will be stored in natural ascending order, but just consider the case where you might try to do this:

@Transient
public ActionState getCurrentState() {
if (stateHistory == null || stateHistory.size() == 0) {
return null;
}
return Collections.max(stateHistory);
}

Eclipse chokes with the error:


Bound mismatch: The generic method max(Collection< ? extends T>) of type Collections is not
applicable for the arguments (Collection< ? extends ActionState>) since the type ActionState is not
a valid substitute for the bounded parameter >

This is confusing, as ActionState implements Comparable.

If I change the offending line to read:

return ((TreeSet<ActionState>)stateHistory).last();

It works fine. It looks like it may be a bug in Eclipse’s compiler, as colleagues using IntelliJ IDEA have no such problems.

Categories
Coding

Log4j XML Configuration

For some reason, I can’t seem to retain a key piece of information – how to configure the logging level per-package in the log4j XML configuration. I’m putting this snippet here to cater for the likely event that I’ll forget it again. It’s all to do with the category element, as it happens.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.SimpleLayout"/>
  </appender>

  <category name="org.apache">
        <priority value="warn"/>
  </category>

  <root>
    <priority value="debug" />
    <appender-ref ref="ConsoleAppender"/>
  </root>

</log4j:configuration>