views:

171

answers:

2

I'm getting this Hibernate error:

org.hibernate.MappingException: Could not determine type for: 
a.b.c.Results$BusinessDate, for columns: [org.hibernate.mapping.Column(businessDate)]

The class is below. Does anyone know why I'm getting this error??

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "businessDate"
})
@XmlRootElement(name = "Results")
@Entity(name = "Results")
@Table(name = "RESULT")
@Inheritance(strategy = InheritanceType.JOINED)
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Results implements Equals, HashCode
{

    @XmlElement(name = "BusinessDate", required = true)
    protected Results.BusinessDate businessDate;

    public Results.BusinessDate getBusinessDate() {
        return businessDate;
    }

    public void setBusinessDate(Results.BusinessDate value) {
        this.businessDate = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "raw",
        "display"
    })
    @Entity(name = "Results$BusinessDate")
    @Table(name = "BUSINESSDATE")
    @Inheritance(strategy = InheritanceType.JOINED)
    public static class BusinessDate implements Equals, HashCode
    {

    ....

Update: This code was generated by HyperJaxB. So I don't claim to understand it all, just trying to make some changes to it!


Update2: Here's the full (yah it's big) src file

A: 

Same comment as Kevin Crowell. You might also look at not using inner classes for entity types. I've actually never seen someone do that with Hibernate, so I'm not sure if it's even possible, or how you would map it.

The @Inheritance annotation on the BusinessDate inner class seems a little fishy too - the inner class is static, and does not inherit from another entity, unless Hibernate treats inner classes as "inherited."

Overall, not really sure what you're trying to accomplish, but you might be making your life harder than it should be. I would recommend not using inner classes, and just mapping all the entities in a more simple/straightforward fashion.

Andy White
From the documentation (http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html): "(...) You may persist any *static* inner class. You should specify the class name using the standard form ie. `eg.Foo$Bar`."
Pascal Thivent
Fair enough. I would personally just stay away from that though. Hibernate is supposed to make your life easier, and I've found that doing (what I would consider to be) "obscure" things just gives you lots of problems.
Andy White
There is nothing obscure and wrong IMHO. There are valid use cases for *static nested classes* (that you CAN instantiate from the outside).
Pascal Thivent
+2  A: 

Using a static nested class as a field type is fine and supported. But Hibernate won't know how to map such a complex type to a column type (which is what the error message says). So you'll need either to create a user type to handle this or to annotate the Results.BusinessDate field with a @OneToOne annotation to persist it in another table (I would also remove the @Inheritance which is useless but this is not the problem here).

Update: Just to clarify, using a user type or mapping the complex type with @OneToOne does work. The following code works perfectly (tested):

@Entity
public class EntityWithStaticNestedClass implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    @OneToOne
    private EntityWithStaticNestedClass.StaticNestedClass nested;

    public Long getId() { return id; }

    public void setId(Long id) { this.id = id; }

    public EntityWithStaticNestedClass.StaticNestedClass getNested() { 
        return nested;
    }

    public void setNested(EntityWithStaticNestedClass.StaticNestedClass nested) {
        this.nested = nested;
    }

    @Entity
    public static class StaticNestedClass implements Serializable {
        @Id
        @GeneratedValue
        private Long id;

        public Long getId() { return id; }

        public void setId(Long id) { this.id = id; }
    }
}

And both entities get well persisted in their respective tables. But you're not showing the entire code nor the exact error so I can't say why it didn't for you (maybe you're missing @Id etc).

That being said, if you don't want businessDate to be persisted at all, annotate it with @Transient (with JPA, fields are persistent by default):

Update: You can't mix field and property access. So you need to annotate getBusinessDate() with @Transienthere. Sorry, I couldn't guess that from the shown code and I thought it would be obvious.

Pascal Thivent
Why does Hibernate even care about the `Results.BusinessDate` field (`businessDate`)? It's not annotated with a Hibernate/JPA annotation (only a JAXB one). Note that if I remove the Hibernate/JPA annotations from the actual inner class I get the same error.
Marcus
I tried the `@OneToOne`, didn't work sadly..
Marcus
@Marcus see my update.
Pascal Thivent
I'm trying to use `@Transient` but am still getting the same error.. hmm...
Marcus
@Marcus You're very likely not using it in the right way. Can you show your code?
Pascal Thivent
I will post the code.. sadly this is a 2000 line file that was generated by HyperJaxB! Thanks for your help..
Marcus
@Marcus See my update of the update
Pascal Thivent
Brilliant, thanks!
Marcus