-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Many-to-many data gets deleted on insert - why?
PostPosted: Thu Apr 02, 2009 9:58 am 
Newbie

Joined: Thu Mar 12, 2009 9:38 am
Posts: 6
Hello,
I have a child entity, which references a number of parent entities with ManyToOne relationships. Some of the parent entities reference each other with ManyToMany relationships.
When I persist the child entity, I can see in log that right after insert statement, there are several delete statements addressing many-to-many database tables. Why is that and how can I switch this behaviour off?

Code:
Hibernate: insert into rpay (amount, asset_id, client_id, comment, contragent_account_id, doc_num, external, external_id, firm_account_id, purpose, status, vat_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: delete from client_contragent where client_id=?
Hibernate: delete from client_firm where client_id=?
Hibernate: delete from user_client where client_id=?


Code:
public class Rpay implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

   ...<some @Basic fields>...

    @JoinColumn(name = "asset_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Asset assetId;
    @JoinColumn(name = "client_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Client clientId;

    @JoinColumn(name = "contragent_account_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private ContragentAccount contragentAccountId;
    @JoinColumn(name = "firm_account_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private FirmAccount firmAccountId;
    @JoinColumn(name = "vat_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Vat vatId;
....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 6:50 am 
Newbie

Joined: Thu Mar 12, 2009 9:38 am
Posts: 6
Anyone? Please?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 8:22 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Could you post the code, where you load, change and store your entities?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 9:23 am 
Newbie

Joined: Thu Mar 12, 2009 9:38 am
Posts: 6
Sure. The StoreRpay method is part of GatewaySessionBean class, which along with ieb field, is declared like this:

Code:
]
@Stateless(name = "GatewaySessionBean")
public class GatewaySessionBean implements GatewaySessionBeanLocal {

    @EJB
    InteractionExtBeanLocal ieb;
...


Here is StoreRpay method, which is the place where actual storing happens. After this method, rpay instance is returned to client by means of a caller. No calls to database made that I know of, but obviously there are some, that I dont.

Code:
   
public StoreRpayResponseData StoreRpay(SessionContext sessionContext, StoreRpayRequestData requestData) {
        try {
            Client client = (Client) ieb.findById(requestData.getClientId(), Client.class);
            if (null == client) {
                StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_ERROR_BL,
                        "Cannot find Client with id#" + requestData.getClientId());
                return rd;
            }
            FirmAccount firmAccount = (FirmAccount) ieb.findById(requestData.getFirmAccountId(), FirmAccount.class);
            if (null == firmAccount) {
                StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_ERROR_BL,
                        "Cannot find FirmAccount with id#" + requestData.getFirmAccountId());
                return rd;
            }
            ContragentAccount contragentAccount = (ContragentAccount) ieb.findById(requestData.getContragentAccountId(), ContragentAccount.class);
            if (null == contragentAccount) {
                StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_ERROR_BL,
                        "Cannot find ContragentAccount with id#" + requestData.getContragentAccountId());
                return rd;
            }
            Asset asset = (Asset) ieb.findById(requestData.getAssetId(), Asset.class);
            if (null == asset) {
                StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_ERROR_BL,
                        "Cannot find Asset with id#" + requestData.getAssetId());
                return rd;
            }
            Vat vat = (Vat) ieb.findById(requestData.getVatId(), Vat.class);
            if (null == vat) {
                StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_ERROR_BL,
                        "Cannot find Vat with id#" + requestData.getVatId());
                return rd;
            }
            Rpay rpay = new Rpay();
            Integer id = Math.abs(new Random().nextInt());
            rpay.setId(id);
            rpay.setClientId(client);
            rpay.setFirmAccountId(firmAccount);
            rpay.setContragentAccountId(contragentAccount);
            rpay.setAssetId(asset);
            rpay.setVatId(vat);
            rpay.setAmount(BigDecimal.valueOf(requestData.getAmount()));
            rpay.setPurpose(requestData.getPurpose());
            rpay.setComment(requestData.getComment());
            rpay.setExternal(Rpay.EXTERNAL);
            rpay.setStatus(Rpay.Status.CREATED.value());
            rpay.setExternalId(id);

            if (!ieb.persist(rpay)) {
                StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_ERROR_BL, "Cannot persist Rpay");
                return rd;
            } else {
                StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_SUCCESS, "Accomplished");
                rd.setRpay(rpay);
                return rd;
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, null, e);
            StoreRpayResponseData rd = new StoreRpayResponseData(ResponseResult.RR_ERROR_PROCESS, e.getMessage());
            return rd;
        }
    }


This is calling web service method. Setting some collections to null required for SAX parser, complaining on cycle in XML graph.

Code:
    @WebMethod(operationName = "StoreRpay")
    public StoreRpayResponseData StoreRpay(@WebParam(name = "sessionId") String sessionId,
            @WebParam(name = "clientId") Integer clientId,
            @WebParam(name = "firmAccountId") Integer firmAccountId,
            @WebParam(name = "contragentAccountId") Integer contragentAccountId,
            @WebParam(name = "assetId") Integer assetId,
            @WebParam(name = "vatId") Integer vatId,
            @WebParam(name = "amount") Double amount,
            @WebParam(name = "purpose") String purpose,
            @WebParam(name = "comment") String comment) {
        try {
            StoreRpayRequestData requestData = new StoreRpayRequestData(clientId, firmAccountId, contragentAccountId,
                    assetId, vatId, amount, purpose, comment);
            if (!requestData.validate()) {
                StoreRpayResponseData responseData = new StoreRpayResponseData(ResponseResult.RR_ERROR_VALIDATE, "Invalid request parameters");
                //TODO: requestData.getErrors();
                Logger.getLogger(WsFrontend.class.getName()).log(Level.WARNING, "StoreRpay validation failed");
                return responseData;
            }

            if (!CheckSession(sessionId)) {
                StoreRpayResponseData responseData = new StoreRpayResponseData(ResponseResult.RR_NOT_AUTHORISED, "Not authorised");
                Logger.getLogger(WsFrontend.class.getName()).log(Level.WARNING, "Unauthorized call to StoreRpay");
                return responseData;
            }
            SessionContext sessionContext = new SessionContext();
            StoreRpayResponseData responseData = gwBean.StoreRpay(sessionContext, requestData);
            Logger.getLogger(WsFrontend.class.getName()).log(Level.INFO, "StoreRpay request processed");
            if (responseData.getResponseResult() == ResponseResult.RR_SUCCESS) {
                Rpay rpay = responseData.getRpay();
                rpay.getClientId().setUserCollection(null);
                rpay.getClientId().setFirmCollection(null);
                rpay.getClientId().setContragentCollection(null);
                //rpay.getClientId().setRpayCollection(null);
                rpay.getFirmAccountId().getBankId().setContragentAccountCollection(null);
                rpay.getFirmAccountId().getBankId().setFirmAccountCollection(null);
                rpay.getFirmAccountId().getFirmLegalId().setClientCollection(null);
                rpay.getFirmAccountId().getFirmLegalId().setFirmAccountCollection(null);
                rpay.getContragentAccountId().getBankId().setContragentAccountCollection(null);
                rpay.getContragentAccountId().getBankId().setFirmAccountCollection(null);
                rpay.getContragentAccountId().getBankId().setName("ั‚ะตัั‚");
                rpay.getContragentAccountId().getContragentLegalId().setClientCollection(null);
                rpay.getContragentAccountId().getContragentLegalId().setContragentAccountCollection(null);
            }
            return responseData;
        } catch (Exception ex) {
            Logger.getLogger(WsFrontend.class.getName()).log(Level.SEVERE, null, ex);
            StoreRpayResponseData responseData = new StoreRpayResponseData(ResponseResult.RR_ERROR_PROCESS, ex.getMessage());
            return responseData;
        }
    }


This is the findById method:

Code:
    public Object findById(Integer id, Class clazz) {
        Object result = null;
        NamedQueries queriesList = (NamedQueries) clazz.getAnnotation(NamedQueries.class);
        if (queriesList == null) {
            logger.severe("No named queries found for entity + " + clazz.getName());
            return null;
        }
        NamedQuery[] queries = queriesList.value();
        for (NamedQuery query : queries) {
            if (query.name().endsWith("findById")) {
                try {
                    Query namedquery = em.createNamedQuery(query.name());
                    namedquery.setParameter("id", id);
                    result = namedquery.getSingleResult();
                } catch (NoResultException ex) {
                    return null;
                } catch (NonUniqueResultException ex) {
                    logger.warning("Non-unique result in findById: " + clazz.getName() + " id " + id.toString());
                    return null;
                } catch (Exception ex) {
                    logger.severe("Exception in findById " + ex.toString() + " " + clazz.getName() + " id " + id.toString());
                    ex.printStackTrace();
                }
                return result;
            }
        }
        logger.severe("No findById query found for entity + " + clazz.getName());
        return null;
    }


This is the persist method:

Code:
    public boolean persist(Object object) {
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                em.getTransaction().rollback();
            } catch (Exception ex) {
            }
        }
        return false;
    }


Rpay class described in previous message. Here is Client class:

Code:
@Entity
@Table(name = "client")
@NamedQueries({@NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"), @NamedQuery(name = "Client.findById", query = "SELECT c FROM Client c WHERE c.id = :id"), @NamedQuery(name = "Client.findByName", query = "SELECT c FROM Client c WHERE c.name = :name")})
public class Client implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @JoinTable(name = "client_contragent", joinColumns = {@JoinColumn(name = "client_id", referencedColumnName = "id", updatable = false)}, inverseJoinColumns = {@JoinColumn(name = "contragent_id", referencedColumnName = "id", updatable = false)})
    @ManyToMany
    private List<Contragent> contragentCollection;
    @JoinTable(name = "user_client", joinColumns = {@JoinColumn(name = "client_id", referencedColumnName = "id", updatable = false)}, inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id", updatable = false)})
    @ManyToMany
    private List<User> userCollection;
    @JoinTable(name = "client_firm", joinColumns = {@JoinColumn(name = "client_id", referencedColumnName = "id", updatable = false)}, inverseJoinColumns = {@JoinColumn(name = "firm_id", referencedColumnName = "id", updatable = false)})
    @ManyToMany
    private List<Firm> firmCollection;
...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.