Skip to content

jerik609/dual-entity-managers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Demonstrates the usage of two entity managers for two distinct JPA repositories

Solution

Basically this:

JPA repository autoconfiguration takes case or many things, even in a partially manual mode.

It creates JPA repositories automatically if a default (named in a standard way) data source, entity manager (factory) and JPA transaction manager are present.

If a more distinctive setup is used, then we need to prepare all the beans manually and instruct the JPA scanning mechanism how to assemble the individual repositories - this is what this example does.

The Spring Data JPA will still wrap the repositories in transactional proxies using the provided platform transaction managers (they must be of JPA type!).

Database

CREATE TABLE example.first (
  id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  data VARCHAR(256) NOT NULL,
  extended_data VARCHAR(256) NOT NULL,
  modified timestamp not null
);

CREATE TABLE example.first (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
data VARCHAR(256) NOT NULL,
extended_data VARCHAR(256) NOT NULL,
modified timestamp not null
);

Bits and snippets

  1. @EnableTransactionManagement is not needed in spring boot, see: https://stackoverflow.com/questions/40724100/enabletransactionmanagement-in-spring-boot
  2. @Transactional is not needed when working with JPA repositories, as they are automatically wrapped in proxies
  3. Constructor binding cannot be used with certain bean creation mechanisms, e.g. @Component, see: https://docs.spring.io/spring-boot/docs/3.0.13-SNAPSHOT/api/org/springframework/boot/context/properties/ConstructorBinding.html

Links

General guides to dual entity manager setup

Links from debugging

About

This repository demonstrates the usage of two EntityManagers for two distinct JPA repositories.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages