0

I've setup a basic spring project with a single database connection.

In the application.properties file I have the database settings:

spring.datasource.url = jdbc:mysql://192.168.1.19/ticket
spring.datasource.username = dbusername
spring.datasource.password = dbpassword

I've created a base DAO class which other DAOs extend:

@Transactional
public class Dao<E> {

    @PersistenceContext
    private EntityManager entityManager;

    private Class<E> entityClass;

    public Dao(Class<E> entityClass) {
        this.entityClass = entityClass;
    }

    public void create(E object) {
        entityManager.persist(object);

        return;
    }

    public void delete(E object) {
        if (entityManager.contains(object)) {
            entityManager.remove(object);
        } else {
            entityManager.remove(entityManager.merge(object));
        }

        return;
    }

    @SuppressWarnings("unchecked")
    public List<E> getAll() {
        return entityManager.createQuery("from " + entityClass.getName()).getResultList();
    }

    public E get(long id) {
        return entityManager.find(entityClass, id);
    }

    public void update(E object) {
        entityManager.merge(object);
        return;
    }
}

Here's a sample entity that extends the base DAO:

@Repository
public class PersonDao extends Dao<Person> {

    public PersonDao() {
        super(Person.class);
    }
}

Currently this uses a single database, but I need to be able to add a second database, and somehow define in each DAO which datasource to use. Each DAO will only use a single database, so there's no requirement for a DAO to be able to connect to multiple databases.

I've done some research, and that seems to suggest I need to use JdbcTemplate? but I can't seem to find a tutorial that matches my need. Also, at the minute the entityManager is injected into the DAO, but the JdbcTemplate examples I've looked at don't seem to use the entityManager, which is slightly confusing.

2
  • 1
    Since you're starting with Spring, take a look at Spring Data repositories; they'll basically generate your entire DAO class for you, plus automatically build most simple queries. Commented Nov 21, 2015 at 9:17
  • Thanks for the suggestion, I'll take a look Commented Nov 21, 2015 at 9:23

2 Answers 2

1
database.password1=<password1>
database.url1=jdbc\:mysql\://localhost\:3306/twodbone
database.username1=<username1>
database.password2=<password1>
database.url2=jdbc\:mysql\://localhost\:3306/twodbtwo
database.username2=<username2>
database.driverClassName=com.mysql.jdbc.Driver

In this way you can add the multiple databases and configure both hibernate.cfg.xml file and applicationContext.xml file also..

Sign up to request clarification or add additional context in comments.

3 Comments

my project doesn't have a hibernate.cfg.xml or applicationcontext.xml file as I'm using springboot which handles most of this for me I believe.
how will that work when the repository extends CrudRepository and makes use of findOne(), save() methods? How will the CrudRepository know to use the jdbcTemplate and not the entityManager? I think this is where I'm getting confused as the example link doesn't mention the use of CrudRepository, but seems to make it's own queries on the database.
0
@Repository
public class FooRepository
{
    @PersistenceContext
    private EntityManager entityManager;

    @Autowired(required = true)
    private JdbcTemplate jdbcTemplate;

    public void saveFoo(Foo foo)
    {
         this.entityManager.persist(foo);
    }

    public List<SomeReportPojo> getSomeReport()
    {
         return this.entityManager.queryForList("SELECT .. ",SomeProjectPojo.class); 
    }
}

this.jdbcTemplate should be kept rather than this.entityManager for jdbc templetes

this is simple example

1 Comment

Sorry, I'm still not sure I understand. Are you saying keep using the EntityManager for the current database connection, and use a JdbcTemplate for the 2nd connection? How will the spring/hibernate ORM with with the JDBC template?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.