Showing posts with label JPA. Show all posts
Showing posts with label JPA. Show all posts

Thursday, June 27, 2013

JPQL: parameter using LIKE clause

Example named query:

    @NamedQuery(
        name = "BismarkPlan.findBismarkPlanByClientIdAndBismarkPlanNameOrBismarkPlanCode"
        ,query  = "SELECT bp FROM BismarkPlan bp"
                    + ", BusinessProposalFormalization bpf"
                    + ", BusinessProposalFormalizationItem bpfi"        
                + " WHERE bpfi.businessProposalFormalizationId = bpf.id"
                    + " AND bpfi.bismarkPlanId = bp.id"
                    + " AND bpf.clientId =:clientId"
                    + " AND (bp.code LIKE :code OR bp.name LIKE :name)"
    )

then calling the query:

Long clientId = 2;
String bismarkPlanCode = "PB";
String bismarkPlanName = "Plan";
        
// TODO: use JPA querybuilder instead of namedqueries
Query query = em.createNamedQuery("BismarkPlan.findBismarkPlanByClientIdAndBismarkPlanNameOrBismarkPlanCode");
query.setParameter("clientId", clientId);
query.setParameter("code", "%" + bismarkPlanCode + "%");
query.setParameter("name", "%" + bismarkPlanName + "%");
List<BismarkPlan> returnList = query.getResultList();

Resource:
http://stackoverflow.com/questions/1341104/parameter-in-like-clause-jpql


Tuesday, May 28, 2013

Netbeans, JPA, Rest: return attribute object in get response

My  orderRequest entity has the following attributes:

Long id (long)
Long orderRequestStatusId
String detail
Calendar purchaseOrderDate
String purchaseOrderNumber
String requestorName
String subject 
OrderRequestStatus orderRequestStatus

Note that orderRequest.orderRequestStatus is OrderRequestStatus type

Calling the entity get request response I got:

{"orderRequest":
 {"detail":"asdasdasd"
  ,"id":"1"
  ,"orderRequestStatusId":"1"
  ,"purchaseOrderDate":"2013-05-28T00:00:00-05:00"
  ,"purchaseOrderNumber":"sdasasd"
  ,"requestorName":"sdasasd"
  ,"subject":"asdasdasd"}
}

Adding the set and get method in the entity class:

public OrderRequestStatus getOrderRequestStatus() {
        return orderRequestStatus;
    }

    public void setOrderRequestStatus(OrderRequestStatus orderRequestStatus) {
        this.orderRequestStatus = orderRequestStatus;
    }

The get request response now has the orderRequestStatus attribute :

{"orderRequest":
  {"detail":"asdasdasd"
   ,"id":"1"
   ,"orderRequestStatus":
      {"id":"1"
      ,"name":"registrada solicitud de pedido"}
   ,"orderRequestStatusId":"1"
   ,"purchaseOrderDate":"2013-05-28T00:00:00-05:00"
   ,"purchaseOrderNumber":"sdasasd"
   ,"requestorName":"sdasasd"
   ,"subject":"asdasdasd"}
}

Monday, May 27, 2013

Netbeans entity - EclipseLink: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

I was getting this error:

Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`bismarksi/orders`, CONSTRAINT `FK_Orders_CLIENTID` FOREIGN KEY (`CLIENTID`) REFERENCES `clients` (`ID`))Error Code: 1452Call: INSERT INTO Orders (ID, CLIENTID, MODIFIEDDATE, ORDERSTATUSID, PROJECTID, RECEIPTDATE, SELLERID) VALUES (?, ?, ?, ?, ?, ?, ?)bind => [7 parameters bound]Query: InsertObjectQuery(com.expertix.bismark.si.entities.Order[ id=1 , projectId=1 , orderStatusId=1  ])

I solved the error adding to the entity the set and get method for the clientId Attribute:

   public int getClientId() {
        return clientId;
    }

    public void setClientId(int clientId) {
        this.clientId = clientId;
    }


JPA: handling date, time and timestamp

Here is a great article about handling date, time and timestamp

http://www.developerscrappad.com/228/java/java-ee/ejb3-jpa-dealing-with-date-time-and-timestamp/

Thursday, May 23, 2013