-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Array of type Enum
PostPosted: Mon Jul 07, 2008 12:34 pm 
Newbie

Joined: Mon Jul 07, 2008 9:47 am
Posts: 2
Hello,

I have an Enum type "Month"

public enum Month {
January, February, March, April, May, June, July, August, September, October, November, December;
};

In my class I have a variable that is an array of these enum types.

Month[] months;

On trying to persist this class, it errors out.

Caused by: java.lang.ClassCastException: [Lcom.company.poll.gwt.client.Recurrence$Month; cannot be cast to java.lang.Enum
at org.hibernate.type.EnumType.nullSafeSet(EnumType.java:117)

Looking at the source where this is occurring the code is.

int ordinal = ( (Enum) value ).ordinal();

This is converting they array of Enum types to the Enum object itself. Any ideas if this is in fact a bug or is there an Enum array limitation that I have not been able to find? I tried both the development and production releases and it doesn't make a difference.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 5:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

how have you annotated/configured your months property? I believe a simple OneToMany won't work, but this could:

Code:
   @CollectionOfElements
   @IndexColumn(name = "pos", base=1)
        private Month[] months;


--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 10:07 am 
Newbie

Joined: Thu Aug 02, 2007 9:15 am
Posts: 2
I met the same ClassCastException recently, and I too think this is likely a but. Different usage scenario. In my case, at least, proper configuration of property mapping is not an issue.

I'm trying to pass Array of enum as a parameter to an HQL query.

Stacktrace:
Code:
Caused by: java.lang.ClassCastException: [Lweblock.model.ValidationStatus;
   at org.hibernate.type.EnumType.nullSafeSet(EnumType.java:125)
   at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:156)
   at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
   at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
   at org.hibernate.loader.Loader.doQuery(Loader.java:673)


Here's the set up:
HQL where clause: (obligor.validationStatus in (:validationStatus))
SQL translation: where obligor0_.VALIDATION_STATUS in (?)

Code:
ValidationList[] list;
...
Query obligorQuery = entityManager.createQuery(hql);
obligorQuery.setParameter("validationStatus", list);
...
List<Obligor> obligors = obligorQuery.getResultList(); // Exception here


Enum definition:
Code:
public enum ValidationStatus {
   CONVERTED("Converted"),PENDING("Pending"), VALID("Valid"), INVALID("Invalid");

   private ValidationStatus(String displayValue) {
      this.displayValue = displayValue;
   }

   private String displayValue;

   public String getDisplayValue() {
      return displayValue;
   }
   public void setDisplayValue(String displayValue) {
      this.displayValue = displayValue;
   }


Property Mapping:
Code:
    @Enumerated (EnumType.STRING)
    @Column(name="VALIDATION_STATUS",length=10)
    public ValidationStatus getValidationStatus() {
        return validationStatus;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 5:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

maybe you could create a Jira issue and attach a test case. This would ensure the quickest resolution to the problem.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 3:25 pm 
Newbie

Joined: Mon Jul 07, 2008 9:47 am
Posts: 2
I apologize, I was occupied before I could get back to test this.

hardy.ferentschik was right that adding those annotations to an array of Enums made it work properly.

Thanks!

Still it seems a bit odd that a ClassCastException would be thrown. Seems like a simple check.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.