Spring Cache -- can't to get a list of data - java

I can't to get list of data through ehcache.
servlet-context:
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="/WEB-INF/spring/appServlet/ehcache.xml" p:shared="true" />
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<cache name="category"
maxEntriesLocalHeap="5000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="200"
timeToLiveSeconds="500"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
I have an entity of Category:
#Entity
#Table(name = "categories")
public class Category implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long categoryId;
#Column(nullable = false)
private String name;
private long level;
private long parentId;
#ManyToMany(fetch = FetchType.EAGER, mappedBy = "categories")
private Set<Post> posts;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userId")
private User user;
public Category() {
}
public Category(User user, long level, String name, long parentId) {
this.user = user;
this.level = level;
this.name = name;
this.parentId = parentId;
}
// ...
}
Then, I try to cache this methods:
#Service
#Repository
#Transactional
public class CategoryServiceImpl implements CategoryService {
#Resource
private CategoryRepository categoryRepository;
#Override
#CachePut(value = "category", key = "#category.categoryId")
public Category add(Category category) {
return categoryRepository.saveAndFlush(category);
}
#Override
#Cacheable("category")
public Category findById(long id) {
return categoryRepository.findOne(id);
}
#Override
#Cacheable("category")
public Category findByParentIdThroughCategoryId(long prntId, long userId) {
return categoryRepository.findByParentIdThroughCategoryId(prntId, userId);
}
#Override
#Cacheable("category")
public List<Category> findByParentId(long prntId, long userId) {
return categoryRepository.findByParentId(prntId, userId);
}
#Override
#Cacheable("category")
public List<Category> findAll(long userId) {
return categoryRepository.findAll(userId);
}
#Override
#CacheEvict("category")
public void remove(long id) {
categoryRepository.delete(id);
}
}
When adding a category, and then try to bring it to view all categories that have user findByParentId/findall, it returns an empty list
[]
It has something to do that for example I cache the categories, and then take the list of categories, I tried to get the category with ID 11 -- findById, and found:
ru.mrchebik.model.Category#f0b8277

Your setup has a number of problems both at Ehcache and Spring levels.
For Ehcache, you should not have a smaller disk size than the heap size. For a while now the tiering model in ehcache means that all entries must be in the disk tier, so you are effectively artificially constraining the heap size to the disk size with this setup.
On the Spring side, you are using a single cache to hold different things, but they will conflict.
Your #CachePut will store a single category, mapped to a long key which matches what your #Cacheable on findById does.
However, this will conflict with the #Cacheable on findAll which also uses a long as the key but caches a List<Category>. So any collision between userId and categoryId can cause unexpected class cast exceptions in client code - expecting a List getting a Category or the opposite.
Finally you have two methods which take two long as parameters but do a different service call while the caching is configured the same. And that service call returns again disjoint types - List<Category> and Category. Which will cause similar issues as in the previous point.
Also, I am under the impression that you expect single entry caching to magically append entries to matching lists. This is not going to happen.
I strongly recommend reviewing your caching needs and better understand what the Spring abstraction offers ... and what it does not.

Related

#Transactional not working in Spring Boot with CrudRepository

I was trying to implement bi-directional relationships bettwen my entities.
Student
#Table(name = "students")
#Entity
public class Student {
#Id
// #GeneratedValue(strategy = GenerationType.AUTO)
private long album;
#NotNull
private String name;
#NotNull
private String surname;
#OneToMany(mappedBy = "student", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
private List<StudentSection> studentSections;
#Transactional(propagation=Propagation.REQUIRED, readOnly=true, noRollbackFor=Exception.class)
public void addSection(Section section){
if(this.studentSections == null){
this.studentSections = new ArrayList<>();
}
StudentSection studentSectionToAdd = new StudentSection();
studentSectionToAdd.setStudent(this);
studentSectionToAdd.setSection(section);
this.studentSections.add(studentSectionToAdd); //here
section.addStudentSection(studentSectionToAdd);
}
}
the connecting entity in a ManyToMany relationship
#Table(name = "student_section")
#Entity
public class StudentSection {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Integer grade;
private Date date;
#NotNull
#ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
#JoinColumn(name = "student_id")
private Student student;
#NotNull
#ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
#JoinColumn(name = "section_id")
private Section section;
}
and Section
#Table(name = "sections")
#Entity
public class Section {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#NotNull
private String name;
#NotNull
private Integer sizeOfSection;
#NotNull
private Boolean isActive;
#OneToMany(mappedBy = "section", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
private List<StudentSection> studentSections;
void addStudentSection(StudentSection studentSection){
if(this.studentSections == null){
this.studentSections = new ArrayList<>();
}
this.studentSections.add(studentSection);
}
}
I ran into a problem with the Student.addSection() method. When trying to execute it I got an error on the this.studentSections.add(studentSectionToAdd); line, saying
failed to lazily initialize a collection of role: Student.studentSections, could not initialize proxy - no Session
I read about it and found out that the best way to fix this is to add the #Transactional annotation to the method, however it didnt change anything and I cant get it to work.
I also tried moving the Student.addSection() method to
StudentServiceImpl
#Service
#Primary
public class StudentServiceImpl implements StudentService {
protected StudentRepository studentRepository;
#Autowired
public StudentServiceImpl(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
#Override
#Transactional(propagation = Propagation.REQUIRED, readOnly = true, noRollbackFor = Exception.class)
public void addSection(Student student, Section section) {
if (student.getStudentSections() == null) {
student.setStudentSections(new ArrayList<>());
}
StudentSection studentSectionToAdd = new StudentSection();
studentSectionToAdd.setStudent(student);
studentSectionToAdd.setSection(section);
student.getStudentSections().add(studentSectionToAdd);
//section.addStudentSection(studentSectionToAdd);
}
}
but I still got the error.
I am also using CrudRepository to retrive entities from the database.
#Repository
public interface StudentRepository extends CrudRepository<Student, Long> {
Student findByName(String name);
}
This is where I call the method
#Component
public class DatabaseLoader implements CommandLineRunner {
private final StudentRepository studentRepository;
private final SectionRepository sectionRepository;
private final StudentSectionRepository studentSectionRepository;
private final StudentService studentService;
#Autowired
public DatabaseLoader(StudentRepository studentRepository, SectionRepository sectionRepository, StudentSectionRepository studentSectionRepository,
StudentService studentService) {
this.studentRepository = studentRepository;
this.sectionRepository = sectionRepository;
this.studentSectionRepository = studentSectionRepository;
this.studentService = studentService;
}
#Override
public void run(String... strings) throws Exception {
//Testing entities
Student student = new Student();
student.setAlbum(1L);
student.setName("student");
student.setSurname("test");
this.studentRepository.save(student);
Section section = new Section();
section.setName("section");
section.setSizeOfSection(10);
section.setIsActive(true);
this.sectionRepository.save(section);
//end
//Adding Student to a Section test
Student student1 = studentRepository.findByName("student");
//student1.setStudentSections(this.studentSectionRepository.findAllByStudent(student1));
Section section1 = sectionRepository.findByName("section");
//section1.setStudentSections(this.studentSectionRepository.findAllByStudent(student1));
studentService.addSection(student1, section1);
this.studentRepository.save(student1);
//end test
}
}
Also when I retrive StudentSection lists from the database here and set them im both objects before adding a new relationship it works fine, but this is not really the solution I am going for.
The problem is that every call from the run() method to studentRepository and studentService are separate sessions/transactions.
It's virtually as-if you did this:
...
beginTransaction();
this.studentRepository.save(student);
commit();
...
beginTransaction();
this.sectionRepository.save(section);
commit();
beginTransaction();
Student student1 = studentRepository.findByName("student");
commit();
beginTransaction();
Section section1 = sectionRepository.findByName("section");
commit();
// This does it's own transaction because of #Transactional
studentService.addSection(student1, section1);
beginTransaction();
this.studentRepository.save(student1);
commit();
Since transaction = session here, it means that student1 is detached, and that the lazy-loaded studentSections collection cannot be loaded on-demand outside the session, and hence the code fails.
Inserting a new student and a new section and associating them should really be one transaction, so if a later step fails, it's all rolled back.
Which basically means that you want the entire run() method to be one transaction, so in your case, it is the run() method that should be #Transactional, not the addSection() method.
Generally, in a 3-tiered approach, you would put transaction boundaries on service layer:
Presentation tier. This is #Controller classes, or the run() method for a simple command-line program.
Logic tier. This is #Service classes. This is where you put #Transactional, so each service call is an atomic transaction, i.e. it either succeeds or it fails, as far as the database updates are concerned, no half updates.
Data tier. This is #Repository and #Entity classes.
As such, you should keep the instantiation and initialization of the Student and Section objects in the run() method, but the rest of the code, incl. save(), should be moved to a single method in a #Service class.
About this
#Transactional(propagation=Propagation.REQUIRED, readOnly=true, noRollbackFor=Exception.class)
public void addSection(Section section){
#Transactional works only for spring-managed beans and Entities are not managed by spring.
You get this exception because you try load a lazy relations outside a session (because your entity is actually in detached-state).
To re-attach --> entityManager.merge(student);
But the best thing to do is to load the relation at query-time. By using EntityGraph for example -->
#EntityGraph(attributePaths="studentSections")
Student findByName(String name);

No session during serialization of lazy objects in Spring Boot 2

I'm migrating my Spring Boot REST API from 1.5.4 to 2.0.3.
These are my two entities, a repository for one of them and a controller for accessing them:
Parent.java
#Entity
#Table(name = "PARENT")
public class Parent implements Serializable {
#Id
#GeneratedValue
#Column(name = "ID")
private Long id;
#OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private List<Child> children;
}
Child.java
#Entity
#Table(name = "CHILD")
public class Child implements Serializable {
#Id
#GeneratedValue
#Column(name = "ID")
private Long id;
#Column(name = "PARENT_ID")
private Long parentId;
#JsonIgnore
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "PARENT_ID")
private Parent parent;
#Column(name = "name")
private String name;
}
ParentRepository.java
public interface ParentRepository extends JpaRepository<Parent, Long> {
}
ParentController.java
#RestController
#RequestMapping("/parents")
public class ParentController {
#Autowired
private ParentRepository parentRepository;
#RequestMapping(method = RequestMethod.GET)
public List<Parent> getParents() {
return parentRepository.findAll();
}
}
It appears that there is no longer an active session in the #RestController classes since
parentRepository.findAll().get(0).getChildren().get(0).getName();
now throws a
LazyInitializationException: failed to lazily initialize a collection of role: com.mycompany.myapplication.entity.Parent.children, could not initialize proxy - no Session
This can be fixed by setting a #Transactional annotation on either the controller method or the controller class.
However, the problem I have regards the lazily loaded children.
If I run the example code above, even with the #Transactional annotation, I get the same exception but with a nested
com.fasterxml.jackson.databind.JsonMappingException
This is due to the serialization to JSON happens outside of the controller, hence outside the active session.
There is an ugly fix for this, by reading some data from each child before exiting the method:
#RequestMapping(method = RequestMethod.GET)
public List<Parent> getParents() {
List<Parent> parents = parentRepository.findAll();
parents.stream()
.flatMap(p -> p.getChildren().stream())
.forEach(Child::getName);
return parents;
}
This works, but is terribly ugly and adds a lot of boilerplate.
Another solution would be to map all entities to DTOs before returning them to the client. But this solution adds another layer to my application which I don't want.
Is there a way to make sure that there is an active session during the automagical serialization of the entities?
Soo yeaah...
During migration I had previously set
spring.jpa.open-in-view = false
because I saw a new warning about it in the log. This setting removes the active session I wanted help adding...
Removing this setting and using the default (true) fixed my problem entirely.

Hibernate One to many Annotation Mapping

Hi I have a two tables like below .
1) Task - id,name
2) Resource - id,name,defaultTask(foreign key to Task.id)
The mapping is one to Many - one task can have many resource.
The code for Task is like below.
#Entity
public class Task implements Serializable {
private long m_id;
private String m_name;
#Id
#GeneratedValue(
strategy = GenerationType.AUTO
)
public long getId() {
return this.m_id;
}
public void setId(long id) {
this.m_id = id;
}
public String getName() {
return this.m_name;
}
public void setName(String name) {
this.m_name = name;
}
#OneToMany
#JoinColumn(
name = "defaultTask"
)
private List<Resource> m_relatedResources;
public List<Resource> getrelatedResources() {
return m_relatedResources;
}
public void setrelatedResources(List<Resource> relatedResources) {
m_relatedResources = relatedResources;
}
And the code for Resource class is like below.
#Entity
public class Resource implements Serializable {
private Long m_id;
private String m_name;
#Id
#GeneratedValue(
strategy = GenerationType.AUTO
)
public Long getId() {
return this.m_id;
}
public void setId(Long id) {
this.m_id = id;
}
public String getName() {
return this.m_name;
}
public void setName(String name) {
this.m_name = name;
}
Task m_task;
#ManyToOne
#JoinColumn(
name = "defaultTask"
)
public Task getTask() {
return this.m_task;
}
public void setTask(Task task) {
this.m_task = task;
}
}
When i execute it I am getting an error like
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: java.util.List, for columns: [org.hibernate.mapping.Column(relatedResources)]
What have i done wrong ?How can i fix the problem ?
You can't apply annotations to methods or fields randomly. Normally, you should apply your annotations the same way as #Id..
In Task class OneToMany should be like
#OneToMany
#JoinColumn(
name = "defaultTask"
)
public List<Resource> getrelatedResources() {
return m_relatedResources;
}
Field access strategy (determined by #Id annotation). Put any JPA related annotation right above each method instead of field / property as for your id it is above method and it will get you away form exception.
Also there appears to be an issue with your bidrectional mapping metntioned by #PredragMaric so you need to use MappedBy which signals hibernate that the key for the relationship is on the other side. Click for a really good question on Mapped by.
Many mistakes here:
you're annotating fields sometimes, and getters sometimes. Half of the annotation will be ignored: you must be consistent. It's one or the other.
You're not respecting the Java Bean naming conventions. The getter must be getRelatedResources(), not getrelatedResources().
A bidirectional association must have an owner side and an inverse side. In a OneToMany, the One is always the inverse side. The mapping should thus be:
.
#ManyToOne
#JoinColumn(name = "defaultTask")
public Task getTask() {
return this.m_task;
}
and
#OneToMany(mappedBy = "task")
public List<Resource> getRelatedResources() {
return m_relatedResources;
}
I also strongly advise you to respect the Java naming conventions. Variables should be named id and name, not m_id and m_name. This is especially important if you choose to annotate fields.
You're mixing annotating fields and getters in the same entity, you should move your #OneToMany to a getter
#OneToMany
#JoinColumn(mappedBy = "task")
public List<Resource> getrelatedResources() {
return m_relatedResources;
}
and yes, as the others mentioned, it should be mappedBy = "task". I'll upvote this teamwork :)
#JoinColumn is only used on owner's side of the relation, ToOne side, which is Resource#task in your case. On the other side you should use mappedBy attribute to specify bidirectional relation. Change your Task#relatedResources mapping to this
#OneToMany(mappedBy = "task")
private List<Resource> m_relatedResources;
Also, as #Viraj Nalawade noticed (and others, obviously), mapping annotations should be on fields or properties, whatever is used for #Id takes precedence. Either move #Id to field, or move #OneToMany to getter.

Hibernate #Filter does not work in JPA?

I'm using JPA with Hibernate as a JPA provider. I cannot figure out how to configure my entities to apply a hibernate filter to a One-to-Many association.
I have a Master with a collection of Details. Here are my entity definitions:
#Entity
public class Master extends Base {
private List<Detail> details;
#OneToMany
#OrderColumn
#JoinTable(name = "master_details")
#Filter(name = "notDeleted")
// #Where(clause = "deleted = 'false'")
public List<Detail> getDetails() {
return details;
}
public void setDetails(List<Detail> details) {
this.details = details;
}
}
#Entity
#FilterDef(name = "notDeleted", defaultCondition = "deleted = false")
public class Detail extends Base {
private Boolean deleted = false;
public Boolean getDeleted() {
return deleted;
}
public void setDeleted(Boolean deleted) {
this.deleted = deleted;
}
}
The Base is nothing special but a simple MappedSuperClass:
#MappedSuperclass
public class Base {
private Long id;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
When loading a Master by entityManager.find(Master.class, mid), the filter should prvent all Details from loading but I checked the sql queries generated by hibernate (by show_sql=true) and no where clause is added when loading details of the master !!! A sample query generated by hibernate is:
select
details0_.Master_id as Master1_6_1_,
details0_.details_id as details2_1_,
details0_.details_ORDER as details3_1_,
detail1_.id as id7_0_,
detail1_.deleted as deleted7_0_,
from
master_details details0_
inner join
Detail detail1_
on details0_.details_id=detail1_.id
where
details0_.Master_id=?
After some search there was some hints that "loading by id will not use filters, use queries" so I tried the following but no gain :(
entityManager.createQuery("from Master where id=" + mid).getSingleResult();
But just if the #Where above getDetails is uncommented (instead of #Filter), its clause is added to the query generated by hibernate (but I cannot use #Where)
The Hibernate #Filter needs to be manually activated via enableFilter method:
session.enableFilter("myFilter").setParameter("myFilterParam", "some-value");
However, filters are useful when you need to parameterize the filtering condition. And, you don't seem to need a dynamic filtering clause.
For this reason, you could use the Hibernate #Where filter, like this:
#org.hibernate.annotations.Where(clause="deleted=false")
public List<Detail> getDetails() {
return details;
}
This way, you should get the list of non-deleted Detail entities.

How to add/delete elements from a collection on an entity in Hibernate?

Basically, how would I make it so that I can add a new TestEntity to the test set after the person has already been created? Also, how can I add a person that has a collection of TestEntity? I'm new to Hibernate so I feel I must be missing something since this would seem like a very common use case.
Some things I've tried:
Attempt #1:
PersonEntity person = createPerson("username");
TestEntity test = new TestEntity();
test.setTestId("2342");
test.setTestName("test name");
personDao.add(person);
person.addTest(test);
This results in the person being saved but no test information. Switching add and addTest does not change anything.
Attempt #2:
Adding a method like this to my Dao (based on http://docs.jboss.org/hibernate/core/3.3/reference/en/html/example-parentchild.html):
public void addTest(String personId, TestEntity test)
{
PersonEntity entity = (PersonEntity) getHibernateTemplate().getSessionFactory().getCurrentSession().load(PersonEntity.class, personId);
if (entity != null)
{
test.setPerson(entity);
entity.getTest().add(test);
getHibernateTemplate().getSessionFactory().getCurrentSession().save(entity);
getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
}
}
And calling like this:
personDao.add(person);
personDao.addTest("username", test);
However, I get this error: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
Attempt #3:
Added #Transaction annotation to my dao and entity classes and added the following config to my app context:
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- org.springframework.transaction.jta.JtaTransactionManager org.springframework.jdbc.datasource.DataSourceTransactionManager -->
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
Now, using that method I created in attempt #2 and calling it in the same way, I get a stackoverflow error.
Edit Update: However, if I remove the test set from my hashCode method in PersonEntity, it works. I can also use person.addTest(test) and that will take care of adding a collection to the person entity before persisting the person entity. However, this really doesn't seem like it would be the best way to do it, no? What would be the best way to make this work? That dao method I added seems like it would be making more calls than necessary?
My classes:
PERSON
#Entity
#Table(name = "PERSON")
public class PersonEntity implements Serializable
{
private static final long serialVersionUID = -1699435979266209440L;
#Id
#Column(name = "PERSON_ID", length = 25, nullable = false)
private String personId;
#LazyCollection(value = LazyCollectionOption.FALSE)
#Cascade(CascadeType.ALL)
#OneToMany(targetEntity = TestEntity.class, mappedBy = "person")
#Where(clause="1=1")
private Set<TestEntity> test;
public void addTest(TestEntity testEntity)
{
testEntity.setPerson(this);
test.add(testEntity);
}
}
TEST
#Entity
#Table(name = "TEST")
public class TestEntity implements Serializable
{
private static final long serialVersionUID = -6524488155196023818L;
#Id
#Column(name = "TEST_ID", length = 36, nullable = false)
private String testId;
#ManyToOne
#Cascade(CascadeType.ALL)
#Index(name = "TEST_PERSON_ID_INDEX")
#JoinColumn(name = "PERSON_ID")
#ForeignKey(name = "FKT1_PERSON_ID")
private PersonEntity person;
#Column(name = "TEST_NAME", length = 60, nullable = false)
private String testName;
}
PersonDaoHibernate
public class PersonDaoHibernate extends HibernateDaoSupport implements PersonDao
{
public String add(PersonEntity person)
{
getHibernateTemplate().merge(person);
return person.getPersonId();
}
public void delete(String id)
{
Object entity = getHibernateTemplate().get(PersonEntity.class, id);
if (entity != null)
{
getHibernateTemplate().delete(entity);
}
}
public PersonEntity getById(String id)
{
return getHibernateTemplate().get(PersonEntity.class, id.toUpperCase());
}
}
I think you will have to change the method name to setTest(...), since hibernate follows java bean convention while trying to do the operation on properties.
Change that and I hope it should work fine.
Rest of code looks fine.

Categories

Resources