Categories
Mastering Development

Relationship entity used to create a hierarchy between objects of a single entity

I have an Organization entity which can have a single parent and/or children. The hierarchy between organizations need to have extra columns (such as relationship type), so I created an entity OrganizationRelationship which is used for parents and also for children. I was able to get the children working with a OneToMany association but couldn’t […]

Categories
Mastering Development

In JPA, insert a child and save and remove the child immediately does not remove the child in database

I have a Parent entity contains a list of Childs. Parent A contains Child A1 In Transaction Add Child A2 to the child list Save (without Flush) Clear the child list (by clear collection) Add Child A3 Save and Flush In the result: There are two Child records in database: (A1 is removed) A2 with […]

Categories
Mastering Development

Join Fetch JPQL returning a proxy object, used with Hibernate

Entity: public class PlayerDetails implements Serializable{ @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = “batting_stat_id”, referencedColumnName = “id”) private BattingStats battingStats; @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = “bowling_stat_id”, referencedColumnName = “id”) private BowlingStats bowlingStats; } Repository code: String queryString = “select p from PlayerDetails p inner join […]

Categories
Development

Pageable reader with join fetch to override FetchType.LAZY

I am using models written by someone else. These models are used by several people in my company and FetchType in relationships are LAZY. So I used a JpaPagingItemReader as follow : @Bean @StepScope public JpaPagingItemReader<Person> jpaPagingItemReader(EntityManagerFactory emf, @Value(“#{jobParameters[‘customer.id.start’]}”) long customerStart, @Value(“#{jobParameters[‘customer.id.end’]}”) long customerEnd){ String query = “select p ” + “from Person p ” […]

Categories
Development Kotlin Plugins

Kotlin SpringBoot and JPA – How to create relations OneToOne?

I am trying to create a relation OneToOne between two entities and always get the error (or very similar, Error creating bean with name ‘entityManagerFactory’ always): org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.moises.kotlinstoreapi.model.Image.product, referenced property unknown: […]

Categories
Development

Java JPA bidirectional ManyToOne mapping not working

I am trying to setup a new JPA bidirectional mapping between and existing table and a new one. It doesn’t seem to work. The entity manager doesn’t initialize during run-time. The other existing tables seem to work with no issues. Also, the app starts fine, if i remove this new table and its associated object […]

Categories
Development

JPA Spring ignores Lazy loading inside @Transacational

I have a spring service class where I’m loading a JPA object (target) via CRUD. This target class has a one-to-may mapping that is set to lazy loading. I would like to query this object inside a spring service method that is annotated with @Transactional and avoid that the childs are being loaded. When I […]

Categories
Development JSON

como recibir un objeto json con @RequestBody o @RequestParam

First of all I apologize, I am new using this technology and I really have many doubts. I am trying to send a json object to my controller class, the problem is that with @RequestBody all the data arrives but the foreign keys arrive null. Example enter a new user with the id of a […]

Categories
Development

How to combine DISTINCT with ORDER BY in JPA SPECIFICATION?

I want to build dynamic filter using Jpa Specification CriteriaBuilder. But have some problems: 1. My root class Product has relation with Images @OneToMany, so I fetch results with duplicates. I decided to use Distinct. It worked, but there is a new problem: 2. My root class product has also relation with ProductDetails @ManyToOne and […]

Categories
Development

JPA/Hibernate5: How to replace a child object mapped with @OneToOne and @MapsId?

I have a bidirectional @OneToOne association between Parent and Child entities, with @MapsId to share the primary key. With a Parent object, I can add a Child, remove it, then add a new Child object: Parent parent = new Parent(); parent.name = “parent”; Child child = new Child(); child.name = “child”; child.parent = parent; parent.child […]