How to Spring Cache JPA nested Objects? - spring

Have Domain Object that has a series of Nested Objects thus. B and C are reference Data Objects. The values are static data that we never update in the table - ever.
Class A {
B b;
C c;
}
class B {
}
class C {
}
B, C etc are quite "heavy" and take some time to return from the DB.
I have a repository written thus:
class MyRepository extends CrudRepository<A, Long> {
}
I need a way to cache B, C so they are not queried everytime whenever I do this. Currently everytime I lookup A both B and C are retrieved from the DB even though they are static data:
MyRepository.findById(1L)
I KNOW I can use Spring Cache at the Service level. But what I'm interested in is DATABASE level Cache. B, C etc are using in many different places and more to come (including PUT/POST) so they all can make best use of database level caching of these reference data since using Spring Cache : Save operations dont get much benefits.

Please check if it is what you want.
maven add
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.2.Final</version>
</dependency>
application.properies add
hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
entity modify
#Entity
#Cacheable
#org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Foo {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private long id;
#Column(name = "NAME")
private String name;
// getters and setters
}
https://www.baeldung.com/hibernate-second-level-cache

Related

How to do lazy loading of the non-owning entity in a unidirectional one-to-one mapping?

#Entity
#Table(name = "...")
public class A
{
private Integer id;
...
...
private B b;
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumn(name = "b_id")
getB() {
return b;
}
setB(B b) {
this.b = b;
}
...other methods...
}
#Entity
#Table(name = "...")
public class B
{
private Integer id;
...
...
...methods...
}
When using
Query query = sessionFactory.getCurrentSession().createQuery()
with
query.uniqueResult()
or
query.list()
to fetch an instance of object A from the db,
I am getting the following error message:
org.hibernate.LazyInitializationException: could not initialize proxy [com.project.core.entity.B#261493] - no Session
The above query method is deprecated, however, I am working on an old project that uses Spring (not Spring Boot), and this is the only way I can go about it.
Now, I know that this may be because the hibernate session is already closed, however, I am using join fetch in my hibernate queries above.
I cannot use a shared primary key for the entities because in the future more entities like C, D, etc. will be added which will also have a unidirectional one-to-one mapping with B.
How do I go about fixing this problem? Thank you.

How to translate #ManyToOne relation in Monolith app into a Microservice App?

I have a monolith app where its models are joined to each others(OnetOne, ManyToMany..).
I was able to create the different Microservices, but I got stuck on how to transition these relationships into Microservices.
Here is my first Class:
#Entity
#Table
public class A {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
#ManyToOne
#JoinColumn(name = "ID",referencedColumnName="ID")
private B b;
//getters and setters
}
#Entity
#Table
public class B{
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
//getters and setters
}
I also Created a microservice for A (controller,repository, service...) and a separate microservice for B.
I am trying to call the Class Model B from the microservice B. But I am not sure how to do it?
I also wonder if it is write to link two classes by joint in microservices or not ?
Thanks
The join relations such as #OneToOne or #ManyToMany are JPA specific and there is no straightforward way to make them work in microservice world.
In general, in microservice world you give up the ACID transactions for cross-service relations and replace them with BASE transactions (eventual consistency behaviour).
In your example, you can achieve this by implementing one of the following strategies.
Fetch the required entity using rest API from the other service.
As you divide your domain into different bounded contexts (services), you will eventually create two different stores with the following entities:
Service A
#Entity
#Table
public class A {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
#Column(name = "B_REFERENCE_ID")
private Integer bId;
//getters and setters
}
And Service B:
#Entity
#Table
public class B{
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
#ElementCollection
private List<Integer> aIds;
//getters and setters
}
Then, you create your target DTO in the the service (example for service B):
public class BDto{
private int id;
private String name;
private List<ADto> aIds;
//getters and setters
}
Then, you need to fetch the dto you want to expose/consume yourself:
#RestController
public class BController {
private final WebClient webClient;
public BController() {
this.webClient = WebClient.builder()
.baseUrl(SERVICE_A_URL)
.build();
}
#GetMapping(path = "/{id}")
public Mono<BDto> getB(#PathVariable int id) {
Optional<B> bEntity = fetchBFromDatabase();
if (bEntity.isPresent()) {
var b = bEntity.get();
var aEntityIds = b.getaIds();
return webClient
.method(HttpMethod.GET)
.uri(GET_A_URL, aEntityIds)
.exchangeToFlux(response -> response.bodyToFlux(ADto.class))
.collect(Collectors.toList()).map(aDtos -> new BDto(b.getId(), b.getName(), aDtos));
}
return Mono.empty();
}
}
If you are unfimiliar with WebClient and reactive concepts, reference spring boot docs https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html
Of course, the good old RestTemplate can be used here as well.
In order to provide data consistency, you will need to implements eventing system with a message broker in between such as Kafka, Apache Pulsar or RabbitMQ.
As an alternative approach, you can keep both A and B entities in both microservices. In service A, you store only the information of B entity that is required in the service A domain and vice versa. In microservice world it is rare that you will require all the B data in service A.
Then, you can keep your join relations as they are in A and B services for fetching purposes.
Remember that you will still require only single source of truth. So, if data changes in service B, then you will need to update your B_ENTITY data in service A and vice versa. Thus, eventing system will still be required to properly updates states in both your services.
The topic of state management in microservices is a complex one, so I recommend to read more about it to get more comfortable with the topic:
https://phoenixnap.com/kb/acid-vs-base
https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215
https://samnewman.io/books/building_microservices_2nd_edition/
Microservices are meant to be indepedent, so that if one service failed, it will not affect the others.
But if you are using multi-module structure, then add the module using the following code to your pom.xml:
<modules>
<module>module1</module>
<module>module2</module>
</modules>
but I am not sure this will work with Jpa.

Service cannot auto-wire in Entity class

I needed a RoleMappingService class(which is annotated by #Service) object into a Employee class (which is annotated by #Entity)
below are my classes
********************* RoleMappingsService class **********************
#Service
public class RoleMappingsService {
#Autowired
RolesMappingDao rolesMappingDao;
public List<RolesMappings> getRolesMappingByauthSystemRole(String authSystemRole) {
return rolesMappingDao.getRolesMappingByauthSystemRole(authSystemRole);
}
}
############### Employee class
#Configurable
#Component
#Entity
#NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
public class Employee implements Serializable, UserDetails {
#Autowired
#Transient
RoleMappingsService roleMappingsService;
public static final String STATUS_ACTIVE = "ACTIVE";
public static final String STATUS_INACTIVE = "INACTIVE";
public static final String STATUS_LOCKED = "LOCKED";
public static final String STATUS_ONLEAVE = "ONLEAVE";
public static final String STATUS_EXPIRED = "EXPIRED";
private static final long serialVersionUID = 1L;
#Id
#Column(name = "emp_id")
private String empId;
#Column(name = "emp_password")
private String empPassword;
#Column(name = "emp_email")
private String empEmail;
#Column(name = "emp_address")
private String empAddress;
#Column(name = "emp_age")
private int empAge;
#Column(name = "emp_firstname")
private String empFirstname;
}
Here Autowire is not working for roleMappingsService and the object is always found null. However I tried to autowire same object in some other service and there Autowire is perfectly working.
( I know Entity class is only used for representing database table but in my case I need to set some field values which depend on another table so need to fetch data using service)
JB Nizet is totally right
I'll try to provide more explanations here.
Spring can Autowire only beans, objects that it manages, and not arbitrary objects.
Entities are usually created from within a JPA (Hibernate) and are not something that you want to manage by Spring.
There is a related discussion available here but bottom line you should never do something like this.
Why not?
Here are a couple of questions/reasons:
Maybe these entities will go outside spring context at all (serialization), what should that reference contain? Should we also serialize the service? How?
What will happen if the method that turns to the service will be called "outside" the spring driven application (maybe even in different JVM)?
If there are, say 1000 objects returned by that query, do you really want all of them to reside in application context? Or maybe should they be of "prototype" scope?
As you see, it doesn't play nice with spring concepts. I think the reason for it is that Hibernate and JPA do not "support" an idea of methods inside entities, it's just a different framework. I know there are other frameworks that do allow such a concept, but Hibernate/JPA just doesn't, period
So instead of trying to inject the service into the entity bean, probably you should redesign the application so that the service method will be called from outside, maybe via some facade, and entities will be just populated by query, and then "enriched" with additional information if we're talking about SELECT queries, or, alternatively, some information should be pre-set on entity objects, generated by the Business Logic Layer and only then the entity object should be stored in DB

Spring Data Couchbase put #Id attribute to json when embedded

I have a one to many relationship namely A and B. A may refer to many B instances. B instances also can be managed independently.
That's why, B class looks like this:
public class B {
#Id
private String id;
private String appId;
}
A class will refer to a list of B instances. So it looks like this:
public class A {
#Id
private String id;
private int age;
private List<B> bInstances;
}
When bInstances are filled with B instances and then A instance is saved, id fields of the B instances are removed from the JSON document since it is annotated with #Id.
I simply need to add this field to JSON when B is embedded into another class.
And when B instance is saved independently, #Id field can be used as the regular key.
How may I do this?
To answer your question specifically, yes the field annotated #Id won't get written to the serialized json. Your best bet here is to duplicate the field and set its value in either the constructor or a setter, for example:
public class B {
#Id
private String metaId;
private String id;
private String appId;
public setMetaId(String metaId) {
this.metaId = metaId;
this.id = metaId;
}
}
You can #JsonIgnore one of them if you don't want to see the duplication in your serializations.
Note: I totally agree with Robin's answer, but there are cases where you want to save the data as-it-was and you want to refer to it in the future.. so you don't care if B instance changed one week after you saved the data. So it really depends on your scenario.
I would strongly advise you to not store the instances of B directly in a document of A. Doing this would result in data inconsistencies if an instance of B is changed in another context than A. Additionally you duplicate data unnecessarily if an instance of B is stored in multiple documents of class A.
The desired way to store instances of one class in another is to save the document ids instead. So your class A should look like this:
public class A {
#Id
private String id;
private int age;
private List<String> bInstanceIds;
}

Spring JPA OneToOne out of a OneToMany with only the fist entry

my problem can be broken down to this little example:
I have a entity class A and a entity class B. A has a List of B objects. Now there is always only one B relevant. So I do not want to load all B's of an A, only to access this one B (last inserted B inside a A).
The question: Can I manipulate an entity without an service, so that there is a #Transient variable, that is always the newest B? And also without saving the newest B separately in A. Is there a way to achieve this?
class B{
#Id
#GeneratedValue
private Long id;
#Column(nullable=false)
private String name;
#Column(nullable=false)
private Date created = new Date();
}
class A{
#Id
#GeneratedValue
private Long id;
#OneToMany
#OrderBy("created ASC")
private List<B> b;
#Transient
private B newestB; // Here should be only the newest B
}
Yes. Forget storing the newest B as a variable and instead simply add a getter for it:
#Transient
public B getNewestB() {
return b.get(b.size() -1);
}
This will solve your problem under the assumption that b is set to FetchType.EAGER. Fetching using b's getter and FetchType.LAZY may not be so straight forward as Spring may rely on an AOP proxy call to trigger the lazy load (you'd need to experiment).
However, I'd discourage both these approaches. You're effectively trying to fit business logic into your Entity. Why not keep your entity clean and perform this query using B's repository?
E.g.
public interface BRepository extends CrudRepository<B, Long> {
#Query(...) //query to get newest B for specified A
B getNewest(A a)
}

Resources