Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7963635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:39:08+00:00 2026-06-04T05:39:08+00:00

I have a generic DAO class that looks like this: public class GenericDaoJpa <T

  • 0

I have a generic DAO class that looks like this:

public class GenericDaoJpa <T extends DomainObject> implements GenericDao<T> {

    private final Class<T> type;

    @PersistenceContext(type=PersistenceContextType.TRANSACTION, unitName="myPersistenceUnit")
    protected EntityManager entityManager;

    public GenericDaoJpa(Class<T> type) {
        super();
        this.type = type;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public T get(Object id) {
        return (T) entityManager.find(type, id);
    }
}

The implementation DAO class looks like this:

    @Repository("appDao")
    public class ProductDaoJpa extends GenericDaoJpa<Product> implements ProductDao{

    public ProductDaoJpa() {
        super(Product.class);
    }

    public List<Product> getAllProducts() {
        return getAll();
    }   
}

I have configured another persistentUnit called mySecondPersistenceUnit for a different database. I would like to create a new DAO class that will also extend the GenericDaoJpa class but use a different persistent unit. How can i extend the GenericDaoJpa class but use a different persisitent unit for each DAO?

I tried moving this declaration to each of the DAO classes but this causes the parent class not to compile because it has no reference to the entityManager.

@PersistenceContext(type=PersistenceContextType.TRANSACTION, unitName="myPersistenceUnit")
protected EntityManager entityManager;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:39:10+00:00Added an answer on June 4, 2026 at 5:39 am

    Try to use method injection instead:

    public class GenericDaoJpa <T extends DomainObject> implements GenericDao<T> {
    
        @PersistenceContext(type=PersistenceContextType.TRANSACTION, unitName="myPersistenceUnit")
        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }
    }
    

    While child class that need use different PU:

    @Repository("appDao")
    public class ProductDaoJpa extends GenericDaoJpa<Product> implements ProductDao{
    
        @Override
        @PersistenceContext(type=PersistenceContextType.TRANSACTION, unitName="mySecondPersistenceUnit")
        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have generic type that looks like: public class GenericClass<T, U> where T :
I have class: public class GenericDAO<T, ID extends Serializable> { private final EntityManager em;
With the generic dao interface: public interface GenericDAO<T extends DataObject, ID extends Serializable> {
I have a problem with sessionFactory in Spring. public abstract class GenericDao<T,PK extends Serializable>
I have code that looks like follows: public interface BaseDAO{ // marker interface }
I have a generic interface for a mathematics library, something like this: [ContractClass(typeof(MathsDoubleContracts))] public
If I have the following class: public class ObjectDAOMongoDBImpl<T> extends GenericDAOMongoDBImpl<T, ObjectId> implements ObjectDAO<T>
I'm using Spring 3.0 with a genericDAO. I have this: public interface GenericDAO<T, ID
I have a Generic Base Class that I want to allow one of two
Currently I have one dao, that uses generic methods and hibernate criteria to perform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.