Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Short

The table in derby is:

id                        bigint
title                    varchar(255)
price                  double
illustrations      smallint
description      varchar(2000)
isbn                   varchar(255)
nbofpage         integer

The stack trace = null. The client code is as follows:

  public static void main(String[] args) {
    Book b = new Book();
    b.setTitle("Beginning Java EE 6");
    b.setPrice(12.5f);
    b.setDescription("An introduction to Java EE 6");
    b.setIsbn("1-222222-8");
    b.setNbOfPage(522);
    b.setIllustrations(false);
   
    EntityManagerFactory factory = null;
    EntityManager manager = null;
    EntityTransaction trans = null;
    try {
      factory = Persistence.createEntityManagerFactory("WarrenPU");
      manager = factory.createEntityManager();
     
      trans = manager.getTransaction();
      trans.begin();
      manager.persist(b);   
      trans.commit();          //>>>>>>>>>>>>>>>>>>>> Error thrown here.
    } catch(Throwable ex) {
      System.out.println(ex.toString());
      if(trans != null) {
        trans.rollback();
      }
    } finally {
      if(manager != null) {
        manager.close();
      }
      if(factory != null) {
        factory.close();
      }
    }

Regards,
Warren Tang

On 9/22/2011 1:54 AM, Tom Ware wrote:
Hi Warren,

  What does the table for Book look like?  How is the column for illustrations
defined?

  What is the exception trace?

-Tom

Warren Tang wrote:
  Hi everyone,

I've just began to learn EclipseLink. I've got into trouble on a sample 
built with JPA and Derby. When calling entitymanager.persist and then 
committing, an error is thrown:

Rollback Exception [cause = java.lang.ClassCastException: 
java.lang.Boolean cannot be cast to java.lang.Short]

The problem seems to be the Boolean type. If I change it to Short the 
error goes away. But cannot I use Boolean type? Isn't EclipseLink 
supposed to do the conversion for me? Or is there some special syntax to 
do it?

The entity class is as follows:

@Entity
@NamedQuery(name = "findAllBooks", query="select b from Book b")
public class Book {
  @Id @GeneratedValue
  private Long id;
  @Column(nullable = false)
  private String title;
  private Float price;
  @Column(length = 2000)
  private String description;
  private String isbn;
  private Integer nbOfPage;
  private Boolean illustrations;

  //getters/setters omitted.
}

The sample is compiled against Java 1.6 and the dependencies defined in 
pom.xml are as follows:

<dependencies>
    <!-- JPA interface -->
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>javax.persistence</artifactId>
      <version>2.0.0</version>
    </dependency>
    <!-- Persistence provider -->
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>eclipselink</artifactId>
      <version>2.0.0</version>
    </dependency>
    <!-- Derby JDBC driver -->
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derbyclient</artifactId>
      <version>10.8.1.2</version>
    </dependency>
    <!-- Derby embedded jar -->
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derby</artifactId>
      <version>10.8.1.2</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.9</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

-- 
Regards,
Warren Tang <http://blog.tangcs.com>


------------------------------------------------------------------------

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top