Search code examples
jpaspring-roo

deleting rows from database using jpa


I have a table named as ACCOUNT which consists of account_id,position_id and few more columns. Another table I have PositionCombination which consists of colums position_id and few more.

This is the account class which i have

@RooJavaBean
@RooToString
@RooJpaActiveRecord
@RooJpaEntity
public class Account {

@OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
List<PositionCombination> allPositions = new ArrayList<PositionCombination>();

@OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<Message> messages = new ArrayList<Message>();

 /*@OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<PremiumPlayPositionCombination> premiumPlayPosition = new ArrayList<PremiumPlayPositionCombination>();*/

@OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<PositionCombinationArc> allPositionsArc = new ArrayList<PositionCombinationArc>();



@ManyToOne
@JoinColumn(name="user_id")
private Userr user;

public List<PositionCombination> getOrders() {
return null;
}



public List<PositionCombination> getPositions() {
return null;
}

}

This is the PositionCombinatin Class

package com.uforic.optionstrader.domain;

@RooJavaBean
@RooToString
@RooJpaEntity
@RooJpaActiveRecord(finders = { "findPositionCombinationsByOrderStatus", "findPositionCombinationsByAccount", "findPositionCombinationsByStrategyType" })
public class PositionCombination {

private int positionType;

/*
 * Strategy type = 1 - Short Term Exit
 * 2 - Calendar Exit
 * 3 - Intraday Exit
 * Currently undefined for normal exit
 */
@ManyToOne
@JoinColumn(name = "strategy_id")
private Strategy strategy;


@OneToMany(mappedBy = "positionCombination", targetEntity = AbstractPosition.class, cascade = CascadeType.ALL, orphanRemoval = true, fetch=FetchType.EAGER)
private List<AbstractPosition> positions = new ArrayList<AbstractPosition>();

@ManyToOne
@JoinColumn(name = "account_id")
private Account account;

@NotNull
int orderStatus;


/* 1 - Short term Exit
 * 2 - Calendar Exit 
 */
int strategyType;

@OneToMany(mappedBy="positionCombination", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<StrategyExitReport> exitReports = new ArrayList<StrategyExitReport>();

@OneToMany(mappedBy="positionCombination", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<StrategyExitTracker> exitTrackers = new ArrayList<StrategyExitTracker>();

@OneToMany(mappedBy="positionCombination", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<IntradayExitDecision> intradayExitDecisions = new ArrayList<IntradayExitDecision>();

@OneToMany(mappedBy="positionCombination", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<ExitDecision> exitDecisions = new ArrayList<ExitDecision>();

@OneToMany(mappedBy="positionCombination", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
List<Message> messages = new ArrayList<Message>();

public BigDecimal getTotalCostWithMarginPerContract() {
    BigDecimal totalCostWithMargin = BigDecimal.ZERO;
    for (AbstractPosition position : positions) {
        totalCostWithMargin = totalCostWithMargin.add(position.getTotalCostPerContract().add(position.getMarginAmount()));
    }
    return totalCostWithMargin;
}

public BigDecimal getTotalCostWithMargin() {
    BigDecimal totalCostWithMargin = BigDecimal.ZERO;
    for (AbstractPosition position : positions) {
        totalCostWithMargin = totalCostWithMargin.add(position.getTotalCostWithMargin());
    }
    return totalCostWithMargin;
}

public static Integer deletePositions(long accid) {
    return new PositionCombination().deletePositionCombinationByAccount(accid);
}

@Transactional
private Integer deletePositionCombinationByAccount(Long accountid) {
    System.out.println("acc id :" + accountid);
    EntityManager em = PositionCombination.entityManager();
    int result = em.createQuery("DELETE FROM PositionCombination o where o.account.id = :accountid").setParameter("accountid", accountid).executeUpdate();
    return result;
}

public static TypedQuery<PositionCombination> findPositionCombinationsByStrategyTypes(List strategyTypes) {
    EntityManager em = PositionCombination.entityManager();
    TypedQuery<PositionCombination> q = em.createQuery("SELECT o FROM PositionCombination AS o WHERE o.strategyType IN (:strategyTypes)", PositionCombination.class);
    q.setParameter("strategyTypes", strategyTypes);
    return q;
}

}

Now when i run this code. It deletes all the entereis from positionCombination Table of that particular account.

public void createSquareOff(User user,Account acc)
{
 List<PositionCombination> positionCombinationList = acc.getAllPositions();
 positionCombinationList.clear();
 acc.persist();
}

But when I change the code to this . It's not deleting the rows from PositionCombination table. Here I have just done small changes, instead of geting AllPositions i am getting particular Positions from DB on basis of position_id and storing into that positionCombinationList.

 public void createSquareOff(User user,Account acc,PositionCombination posComb )
 {
  List<PositionCombination> positionCombinationList = PositionCombination.findPoscomByPoscombination(posComb);
  positionCombinationList.clear();
  acc.persist();
 }

Solution

  • There is a big difference between the two method implementations.

    In the first one, you are getting the allPositions attribute from an Account. Then you clear all the related PositionCombination entities from that relationship.

    Take into account the allPositions attribute is annotated with @OneToMany with the following parameter:

    orphanRemoval=true
    

    It tells the JPA provider to remove the related entities when they are removed from the relationship. That's why all the related PositionCombination get removed when you clear the allPositions attribute.

    In the second implementation you are performing a JPA Query and getting the result as a List. That List is not an entity attribute, so clearing it won't affect the database or the entities state. You have to remove those entities explicitly. Also you don't need the Account instance anymore.