• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA Class cast exception for same class

 
Debanjan Sarkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am learning the java persistence API and I have run into a problem.
I get this exception:
[ main] Version INFO HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
[ main] Version INFO HHH000412: Hibernate Core {4.2.7.Final}
[ main] Environment INFO HHH000206: hibernate.properties not found
[ main] Environment INFO HHH000021: Bytecode provider name : javassist
[ main] rManagerConnectionProviderImpl INFO HHH000402: Using Hibernate built-in connection pool (not for production use!)
[ main] rManagerConnectionProviderImpl INFO HHH000115: Hibernate connection pool size: 20
[ main] rManagerConnectionProviderImpl INFO HHH000006: Autocommit mode: true
[ main] rManagerConnectionProviderImpl INFO HHH000401: using driver [oracle.jdbc.OracleDriver] at URL [jdbc:oracle:thin:[dev/dev123]@//localhost:1521/sysdba]
[ main] rManagerConnectionProviderImpl INFO HHH000046: Connection properties: {user=dev, password=****, autocommit=true, release_mode=auto}
[ main] Dialect INFO HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
[ main] TransactionFactoryInitiator INFO HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
[ main] ASTQueryTranslatorFactory INFO HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.ClassCastException: com.onetomany.test.ErrorLogTwoEntity cannot be cast to java.util.List
at com.onetomany.test.TestOneToManyMain.main(TestOneToManyMain.java:23)


when I run my code:
package com.onetomany.test;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class TestOneToManyMain {
private static final String PERSISTENCE_UNIT_NAME = "myModel";
private static EntityManagerFactory factory;
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
List<ErrorLogTwoEntity> errorLogTwoEntityList = (List<ErrorLogTwoEntity>) em.find(ErrorLogTwoEntity.class, 2L);
//some printing
em.close();
}
}

Please help regarding this issue. Thanks in advance.
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EntityManager.find returns a single entity for the given class and primary key, not a List of entities. The exception is quite clear that the cast of an entity to a List fails.
 
Debanjan Sarkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rob,
Thanks a ton for the quick response to my post. Now that you mention it, it seems only natural. I was under the impression that it would return a list of size one with the one entry in it. Thanks for the clarification.
👍
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just thinking that you should have gotten a compiler error that List and ErrorLogTwoEntity are not compatible, but then I remembered that List is an interface, and therefore the compiler can't throw an error because there just might be a sub class of ErrorLogTwoEntity that implements List. If ErrorLogTwoEntity were final you would have gotten the error though.
 
Debanjan Sarkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The only time I got a compile error was when I hadn't put in the List<ErrorLogTwoEntity> typecast for em.find. Thanks for the insight.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic