Spring Data JPA. Delete not reflected in mysql database - spring

I am using Spring JPA to store a many-to-many relationship between User and Service with the table Acquisition. Since the bridge table contains additional columns I modelled it as having two many-to-one relationships. Both are bidirectional. Additionally the Acquisition entity has a one-to-one relationship with ServiceConfiguration.
There is no problem with saving or retrieving any of these entities. However when I try to delete the acquisition like this:
#Override
#Transactional
public void removeUsersServiceAcquisition(Long serviceId, User user) {
Service service = getService(serviceId);
Acquisition acquisition = findAcquisitionByServiceAndUser(service, user);
acquisitionRepository.delete(acquisition.getId());
log.info("\n retrieved acquisition {} ", acquisitionRepository.findOne(acquisition.getId()));
}
The change is not reflected in the database. The subsequent find within the above method returns null. But later in the code and in the database the record exists. There are no exceptions being thrown.
#Entity
#Table(name="ACQUISITION")
public class Acquisition implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="id")
public Long getId() {
return id;
}
#ManyToOne
#JoinColumn(name="user_id")
public User getUser() {
return user;
}
#ManyToOne
#JoinColumn(name="service_id")
public Service getService() {
return service;
}
#OneToOne(mappedBy="acquisition")
public ServiceConfiguration getConfiguration() {
return configuration;
}
}
#Entity
#Table(name="USER")
public class User implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="id")
public Long getId() {
return id;
}
#OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval=true)
public Set<Acquisition> getAcquisitions() {
return acquisitions;
}
}
#Entity
#Table(name="SERVICE")
public class Service implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="id")
public Long getId() {
return id;
}
#OneToMany(mappedBy="service", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval= true)
public Set<Acquisition> getAcquisitions() {
return acquisitions;
}
}
#Entity
#Table(name="SERVICE_CONFIGURATION")
public class ServiceConfiguration implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="id")
public Long getId() {
return id;
}
#OneToOne
#JoinColumn(name="acquisition_id")
public Acquisition getAcquisition() {
return acquisition;
}
public void setAcquisition(Acquisition acquisition) {
this.acquisition = acquisition;
}
}

Here is what I did to get this to work.
The remove method changed to first remove the acquisition from both relationships in which it participated:
#Transactional
public void removeUsersServiceAcquisition(Long serviceId, User user) {
Service service = getService(serviceId);
Acquisition acquisition = findAcquisitionByServiceAndUser(service, user);
service.getAcquisitions().remove(acquisition);
user.getAcquisitions().remove(acquisition);
acquisitionRepository.delete(acquisition.getId());
log.info("\n retrieved acquisition {} ", acquisitionRepository.findOne(acquisition.getId()));
}
This resulted in "no Session" Hibernate exception.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.netstellar.sitesuite.serviceregistry.site.model.User.acquisitions, could not initialize proxy - no Session
Which I dealt with by adding fetch property to the User mapping. Not sure if this is the only way of addressing this exception.
#OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
public Set<Acquisition> getAcquisitions() {
return acquisitions;
}

Related

How to correctly describe entities with many-to-many relationship, Spring Boot JPA

I have those entities:
#Entity
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class Tender {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(nullable = false, updatable = false)
private Long id;
private String source;
private String sourceRefNumber;
private String link;
private String title;
#Column(columnDefinition="TEXT")
private String description;
private String field;
private String client;
private LocalDate date;
private LocalDate deadline;
#ManyToMany
private List<Cpv> cpv;
}
And CPV:
#Entity
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class Cpv {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String code;
private String description;
}
Each Tender can have list of Cpv-s.
In my DB I have already list of all CPV codes with description, so when I add new Tender to DB, it should add record to tender_cpv table with tender_id and cpv_id.
But when I'm using this method in my TenderServiceImpl to set Cpv id-s from DB I got error after that when try to save Tender:
#Override
public Tender addNewTender(Tender tender) {
if(tender.getCpv() != null) {
for(Cpv cpv : tender.getCpv()) {
cpv = cpvRepository.findCpvByCode(cpv.getCode());
}
}
tenderRepository.save(tender);
return tender;
}
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.supportportal.domain.Cpv;
I understand that somewhere in the description of the entities a mistake was made, because earlier I did not have a database with all the CPV codes and before saving the tender I saved all the CPVs, but now I need to redo the logic to use the existing CPV database.
Please advise how can I change the entity description.
addNewTender method changes solved my problem:
#Override
public Tender addNewTender(Tender tender) {
if(tender.getCpv() != null) {
List<Cpv> dbCpvs = new ArrayList<>();
for(Cpv cpv : tender.getCpv()) {
dbCpvs.add(cpvRepository.findCpvByCode(cpv.getCode()));
}
tender.setCpv(dbCpvs);
}
tenderRepository.save(tender);
return tender;
}
In order for the existing entities from the database to bind to the new object, we had to first get each of them from the database and bind to the new entity.

When does the hibernate session gets closed

I have created the following entities.
#Entity
public class Student {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#OneToMany(mappedBy = "student")
private List<Book> books;
}
#Entity
public class Book {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#ManyToOne
#JoinColumn(name = "STUDENT_ID")
private Student student;
}
My controller looks like this
#RestController
public class Controller {
MyService myService;
public Controller(MyService myService) {
this.myService = myService;
}
#GetMapping("student")
public List<Book> getBooksForStudent(Long id) {
return myService.getBooks(id);
}
}
The service is as follows.
public class MyService {
#Autowired
private StudentRepo studentRepo;
public List<Book> getStudent(Long id) {
Optional<Student> studentOptional = studentRepo.findById(id);
return studentOptional.map(Student::getBooks).orElseThrow(IllegalArgumentException::new);
}
}
I am getting the list of books as expected. But as I'm having lazy loaded list for books I should be getting a LazyInitializationException. I have not added transnational to the method and I'm returning the list of books from the entity itself without mapping it to a DTO. Why is the hibernate session not getting closed after the end of the method?
#RestController is transactional by default. Spring boot automatically registers an OpenEntityManagerInViewInterceptor when you use a web application/you use JPA. Refer #RestController methods seem to be Transactional by default, Why?

SpringBoot CascadeType ALL vs MERGE and detached entities

I have the following entities:
#Entity
#Getter #Setter #NoArgsConstructor #RequiredArgsConstructor
public class Link extends Auditable {
#Id
#GeneratedValue
private Long id;
#NonNull
private String title;
#NonNull
private String url;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "link")
private List<Comment> comments = new ArrayList<>();
#Transient
#Setter(AccessLevel.NONE)
private String userAlias ;
public String getUserAlias() {
if(user == null)
return "";
return user.getAlias();
}
#ManyToOne
private User user;
public Long getUser() {
if(user == null)
return -1L;
return user.getId();
}
public void addComment(Comment c) {
comments.add(c);
c.setLink(this);
}
}
#Entity
#Getter #Setter #NoArgsConstructor #RequiredArgsConstructor
public class Comment extends Auditable{
#Id
#GeneratedValue
private Long id;
#NonNull
private String comment;
#ManyToOne(fetch = FetchType.LAZY)
private Link link;
public Long getLink() {
return link.getId();
}
}
If I create a comment and a link, associate the link to the comment and then save that works.
Eg:
Link link = new Link("Getting started", "url");
Comment c = new Comment("Hello!");
link.addComment(c);
linkRepository.save(link);
However, if I save the comment first:
Link link = new Link("Getting started", "url");
Comment c = new Comment("Hello!");
commentRepository.save(c);
link.addComment(c);
linkRepository.save(link);
I get
Caused by: org.hibernate.AnnotationException: #OneToOne or #ManyToOne on uk.me.dariosdesk.dariodemo.domain.Comment.link references an unknown entity: uk.me.dariosdesk.dariodemo.domain.Link
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1815) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1759) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1646) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:903) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:934) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
Changing the cascade type from ALL to MERGE seems to fix the problem and accept both implementations. (Ie: Adding a pre-existing comment or creating both and then saving via the link)
1) Why is this?
2) Is there anything I should be aware of in using MERGE rather than ALL?
Repository save method checks if entity exist. For new entity persist is called, for persisted entity merge is called.
#Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
In 2nd use-case Link is new entity, therefore persist() is called. With CascadeType.ALL persist() is cascaded to Comment entity. Comment is already persisted and needs to be merged, persist() fails.
If you use CascadeType.MERGE persist() is not cascaded down to Comment. It does not fail.

JPA doesn't fetch the updated data

I am facing a very weird issue in JPA entity manager. I have tow Entities
1) Incident
2) Country
Country is master and Incident is child with ManyToOne.
Incident.java
#Entity
#Table(name = "Incident")
public class Incident {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "incidentID")
private Integer incidentID;
#Column(name = "incidentTitle")
private String incidentTitle;
#ManyToOne
#JoinColumn(name = "countryID")
private Country country;
#Transient
#ManyToOne
#JoinColumn(name = "countryID")
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
// Getter and setters
}
Country.Java
#Entity
#Table(name="Country")
public class Country {
#Id
#Column(name="id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
#Column(name = "name")
private String name;
#OneToMany(mappedBy = "country", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Incident> incident;
#OneToMany
#JoinColumn(
name="countryID",nullable=false)
public List<Incident> getIncident() {
return incident;
}
public void setIncident(List<Incident> incident) {
this.incident = incident;
}
//getter and setter
}
RepositoryImpl.java
#Repository
#Transactional
public class IncidentRepositoryImpl implements IncidentRepository{
#PersistenceContext
private EntityManager em;
#Autowired
public void setEntityManager(EntityManagerFactory sf) {
this.em = sf.createEntityManager();
}
#Override
public Incident addIncident(Incident incident) {
try {
em.getTransaction().begin();
em.persist(incident);
em.getTransaction().commit();
return incident;
} catch (HibernateException e) {
return null;
}
}
public Incident findById(int id) {
Incident incident = null;
incident = (Incident) em.find(Incident.class, id);
return incident;
}
}
When i add Incident, incident added successfully with countryID in Incident table, But when i try to fetch the same incident, country name comes null. But when i take restart of server or redeploy the application country name also comes. Hope there is cache issue with JAP entity manager. I try to use em.refresh(incident) in findById method, then country name comes successfully. But this refresh method is very expensive call.
Please suggest some alternate solution, how to update jpa cache automatically.
On your EntityManager em, add
#PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager em;
With PersistenceContextType.TRANSACTION, Spring takes control of the life cycle of EntityManager

How to save child entities without saving parent for each transaction

I am using Spring Data JPA repositories. I have a Card entity and a Transaction entity. When user perform a transaction with card then i would like to save Card and transaction(purchase/refund) entities both. But when user performs next transaction then i want to save Transaction entity only. My Entities are :
Card Entity
#Entity
#Table(name = "CARD")
public class Card {
#Id
private Long card_id;
public Long getCard_id() {
return card_id;
}
public void setCard_id(Long card_id) {
this.card_id = card_id;
}
private String type;
}
Transaction Entity
#Entity
#Table(name="Transaction")
public class Transaction {
#Id
#SequenceGenerator( name="TRAN_SEQ1", initialValue=5,sequenceName="TRAN_SEQ1", allocationSize=1 )
#GeneratedValue( strategy=GenerationType.SEQUENCE, generator="TRAN_SEQ1")
private long id;
#ManyToOne(cascade=CascadeType.ALL)
#JoinColumn(name = "card_id")
private Card card;
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
}
I have tried with below approach but it throws below exception on save:
Transaction t = new Transaction();
Card c = cardRepository.getOne(123L);
t.setCard(c);
transactionRepository.save(t);
**Exception :
org.hibernate.PersistentObjectException: uninitialized proxy passed to persist()**
I am not sure what I am missing. Can anyone guide me here..
Have you tried to add the reverse relationship?
#Entity
#Table(name = "CARD")
public class Card {
#Id
private Long card_id;
#OneToMany
private List<Transaction> transactions = new ArrayList<>();
// Getters and Setters
}

Resources