简体   繁体   中英

hibernate generic dao overloading removeById

Im implementing hibernate ORM in my java project. Ive downloaded source from github Ive compiled hibernate-maven-web included in samples. Now lets assume i want to overload method in one of the implementations For example CitizenDAOImpl.java Before my changes it looked like that.

package sample.googlecode.genericdao.oldworld.dao;

import org.springframework.stereotype.Repository;

import sample.googlecode.genericdao.oldworld.model.Citizen;

/**
 * <p>
 * This is the implementation of the Citizen DAO. You can see that we don't
 * actually have to implement anything, it is all inherited from GenericDAOImpl
 * through BaseDAO. We just specify the entity type (Citizen) and its identifier
 * type (Long).
 * 
 * <p>
 * The @Repository allows Spring to recognize this as a managed component (so we
 * don't need to specify it in XML) and also tells spring to do DAO exception
 * translation to the Spring exception hierarchy.
 * 
 * @author dwolverton
 * 
 */
@Repository
public class CitizenDAOImpl extends BaseDAO<Citizen, Long> implements CitizenDAO {


}

All i did was override removeById that would just call super class. Later on im planing to add some ecrypt functionality so id will all be scrambled.

package sample.googlecode.genericdao.oldworld.dao;

import org.springframework.stereotype.Repository;

import sample.googlecode.genericdao.oldworld.model.Citizen;

/**
 * <p>
 * This is the implementation of the Citizen DAO. You can see that we don't
 * actually have to implement anything, it is all inherited from GenericDAOImpl
 * through BaseDAO. We just specify the entity type (Citizen) and its identifier
 * type (Long).
 * 
 * <p>
 * The @Repository allows Spring to recognize this as a managed component (so we
 * don't need to specify it in XML) and also tells spring to do DAO exception
 * translation to the Spring exception hierarchy.
 * 
 * @author dwolverton
 * 
 */
@Repository
public class CitizenDAOImpl extends BaseDAO<Citizen, Long> implements CitizenDAO {

    @Override
    public boolean removeById(java.io.Serializable id) {
        return super.removeById(id);
    }
}

when i run mvn clean install it gives me error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.
1:compile (default-compile) on project hibernate-maven-web: Compilation failure
[ERROR] /C:/Dev/spike/hibernate-maven-web/src/main/java/sample/googlecode/generi
cdao/oldworld/dao/CitizenDAOImpl.java:[26,24] name clash: removeById(java.io.Ser
ializable) in sample.googlecode.genericdao.oldworld.dao.CitizenDAOImpl and remov
eById(ID) in com.googlecode.genericdao.dao.hibernate.GenericDAO have the same er
asure, yet neither overrides the other
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
eption

I understand that its ambiguous call and both BaseDAO and CitizenDAO do inherit GenericADO at some point. What is good solution in this case? Thanks

Actually your method doesn't override the superclass method, you should use Long id instead of Serializable id as a parameter to make it overridden.

The superclass is a bounded generic class, since it has ID extends Serializable restriction on ID's generic type. Note, generic information is not stored in the runtime and would be erased. It's just a compile time information. See https://docs.oracle.com/javase/tutorial/java/generics/erasure.html for details.

Here your custom method doesn't override superclass method, moreover, both method's are generic and have the same erasure. So in runtime, after erasure, these methods would have the same signature and JVM would be confused which one to call. That's why compiler fails with error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM