Announcement Announcement Module
Collapse
No announcement yet.
java.lang.ClassCastException: java.lang.Long when saving an instance in a tx context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.lang.ClassCastException: java.lang.Long when saving an instance in a tx context

    Hi,

    I have a base Service class called Service :
    Code:
    public class Service<K extends Serializable,V,D extends IDao<K, V>> implements IService<K, V,D>{
    
    	protected D dao;
    	
    	public Service(D dao) {
    		this.dao = dao;
    	}
    
    	public void delete(V object) {
    		dao.delete(object);
    	}
    
    	public List<V> findAll() {
    		return dao.findAll();
    	}
    
    	public V findById(K id) {
    		return dao.findById(id);
    	}
    
    	@Transactional
    	public V save(V object) {
    		return dao.save(object);
    	}
    
    }
    I've wrote a specialized service for entity A, let's called it AService which implementation is :
    Code:
    @org.springframework.stereotype.Service
    public class AService extends Service<Long, A, IADao> implements IAService {
    
    	@Autowired
    	public AService(@Qualifier("aDao") IADao dao) {
    		super(dao);
    	}
    
    	@Override
    	public A save(A object) {
    		if(a.getId()==null){
    			object.setModified(Boolean.FALSE);
    		} else {
    			object.setModified(Boolean.TRUE);
    		}
    		return super.save(object);
    	}
    }
    When I call aservice.save(), I have the following exception:
    Code:
    java.lang.ClassCastException: java.lang.Long
    	at X.service.impl.AService.save(AService.java:34)
    	at X.service.impl.AService.save(AService.java:1)
    	at X.TestDao.test(TestDao.java:46)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    If I remove the overrided method in AService, the exception disappeared...
    Any ideas?


    Thanks,

    Mayeul

  • #2
    Hello

    I am not an expert about generics and assuming you are working with Hibernate (due the save method name) I have some observations

    Code:
    @Override
    public A save(A object) {
    	if(a.getId()==null){
    		object.setModified(Boolean.FALSE);
    	} 
            else {
    		object.setModified(Boolean.TRUE);
            }
    	return super.save(object);
    }
    the save method according with the API

    Code:
    Returns:
        the generated identifier 
    it returns the PK value created or generated

    Seems that your Entity's PK is long Type, your A type (orange part above) could be consider or fit like a Long object?, realize yourself that in your super class the method return a V type instead of an A type

    Comment

    Working...
    X