Tag Archives: jpa

JPA aggregate root approach

Questions: I’ll use a blog post as an example. The BlogPost entity is an aggregate root, and it contains a BlogPostComment collection. That’s good because a post comment cannot exist without a parent blog post. Every operation on a BlogPostComment is taken care of by a BlogPost, or better by the service which manages BlogPost… Read More »

Spring, JPA and Rest and Duplicate entry for key 'PRIMARY'

Questions: I’m just starting to learn Spring and JPA and I’ve run into a bit of a wall trying to figure out what is causing my app to try and insert cascaded values in multiple times. I have the following structure: pom.xml <?xml version=”1.0″ encoding=”UTF-8″?> <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>core</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging>… Read More »

Spring @Transactional(readOnly = true) in Java JPA?

Questions: I couldn’t find similar annotation of Spring @Transactional(readOnly = true) in Java Jpa. I need to implement load balancing with MariaDb ConnectorJ driver (article https://mariadb.com/kb/en/library/failover-and-high-availability-with-mariadb-connector-j/#aurora-failover-implementation). I spent 2 days finding solution but i couldn’t find any. So what i am trying to do is setting connection to readOnly so that connection goes to read… Read More »

JPA JoinTable keeps on adding distinct

Questions: I’ve got the following tables: table0 tableA tableB tableA has property @ManyToMany @JoinTable(name=’table0′, joinColumns=@JoinColumn(name=’t_id’, referencedColumnName=’id’), inverseJoinColumns=@JoinColumn(…) bes // list of objects from tableB The problem is that when I am trying to fetch list of objects with criteria eg bes.name = ‘abc’ it keeps on sending query with distinct on all fields of tableA… Read More »

How can i using extends on JpaRepository?

Questions: I hope to using like this process. but that is not able to using ‘saveAll()’ or ‘save()’ method on Repository. but ‘find()’ method is able. JpaRepository<? extends Type, Long> buffRepository; HashSet<? extends DG15001886Bean> buff_list; switch(enumType) { case Type1: //type1Repository’s generic is <Type1, Long> buffRepository = type1Repository // type1 is extends Type break; case Type2:… Read More »

Update query using JPA in spring?

Questions: I am trying to update a column in a table in spring using JPA repository using following query @Modifying @Query(“UPDATE Price p SET p.price=:price where p.timePeriod=:timePeroid and p.outletVariety.outletProduct.outlet.id=:oId and p.outletVariety.variety.id=:vId”) Integer updatePriceForVarietyandOutlet(@Param(“price”) Double price,@Param(“timePeroid”) LocalDate timePeroid,@Param(“oId”) Long oId , @Param(“vId”) Long vId); But I am getting following exception org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL… Read More »

in JpaRepository if one parameter is common for many query method then how to make as good as possible in Repository and in Service also

Questions: public interface UserRepo extends JpaRepository<User, Long> { public List<User> findById(long id); public List<User> findByEmail(String email); public List<User> findByEmailAndCode(String email, Code code); public List<User> findByEmailAndClassType(String email, ClassType code); } public class UserService { @Autowired UserRepo userRepo; public List<user> fetchByClassType(ClassType ct) { return userRepo.findByEmailAndClassType(“email”, ct); } } here email need to fetch once how to avoid… Read More »

Can we change JPA metamodel location

Questions: Can we change our JPA Metamodel location? At default it should be located in same package as Entity classes are. So there is an opportunity to configure this stuff? Answers:

DB2 Auto increment primary key column not working with JPA entity in Spring Boot

Questions: I’m working on a Spring Boot project and using existing DB2 database for persistence. I’m getting an error when saving (persisting) an entity which has primary key with auto increment. I have tried everything I could find on google, with same result. Error code from DB2 SQL Error: -142 REF: https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/codes/src/tpc/n142.html -142 THE SQL… Read More »