Hibernate MappingException

2.9k Views Asked by At

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

2

There are 2 best solutions below

3
On BEST ANSWER

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.

3
On

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.