Announcement Announcement Module
Collapse
No announcement yet.
i need problems jpa query of spring roo( 도와주세요) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • i need problems jpa query of spring roo( 도와주세요)

    Code:
    120725 16:37:41  2520 Query     SELECT 1
                     2520 Query     select bidlicense0_.Idx as Idx4_, bidlicense0_.BidIdx as BidIdx4_, bidlicense0_.LicenseCode as LicenseC2_4_, bidlicense0_.LicenseGroup as LicenseG3_4_, bidlicense0_.LicenseName as LicenseN4_4_ from BidLicense bidlicense0_ where bidlicense0_.BidIdx=1560
                     2520 Query     SELECT 1
                     2520 Query     SELECT 1
                     2520 Query     select bid0_.Idx as Idx1_1_, bid0_.BasicAmount as BasicAmo2_1_1_, bid0_.BidGroup as BidGroup1_1_, bid0_.BidNo as BidNo1_1_, bid0_.BidRepeat as BidRepeat1_1_, bid0_.BidStyle as BidStyle1_1_, bid0_.BidTitle as BidTitle1_1_, bid0_.BidType as BidType1_1_, crawlingsi1_.Name as Name5_0_, crawlingsi1_.Url as Url5_0_ from Bid bid0_ inner join CrawlingSite crawlingsi1_ on bid0_.CrawlingSiteIdx=crawlingsi1_.Idx where bid0_.Idx=1560



    see below my code

    com.test.roo.domain.BidLicense
    Code:
    mport org.springframework.roo.addon.json.RooJson;
    import org.springframework.roo.addon.tostring.RooToString;
    
    @RooJavaBean
    @RooToString
    @RooDbManaged(automaticallyDelete = true)
    @RooJson
    @RooJpaActiveRecord(versionField = "", table = "BidLicense")
    public class BidLicense {
    
        public static List<com.test.roo.domain.BidLicense> findBidLicensesByBidIdx(int bidIdx) {
            CriteriaBuilder cb = BidLicense.entityManager().getCriteriaBuilder();
            CriteriaQuery<BidLicense> cq = cb.createQuery(BidLicense.class);
            Root<BidLicense> tableBL = cq.from(BidLicense.class);
            cq.select(tableBL).where(cb.equal(tableBL.get("bidIdx"), bidIdx));
            return entityManager().createQuery(cq).getResultList();
        }
    }





    ASPECTJ

    Code:
    package com.test.roo.domain;
    
    import javax.persistence.Column;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.validation.constraints.NotNull;
    import com.test.roo.domain.Bid;
    import com.test.roo.domain.BidLicense;
    
    privileged aspect BidLicense_Roo_DbManaged {
        
        @ManyToOne
        @JoinColumn(name = "BidIdx", referencedColumnName = "Idx", nullable = false)
        private Bid BidLicense.bidIdx;
        
        @Column(name = "LicenseCode", length = 20)
        @NotNull
        private String BidLicense.licenseCode;
        
        @Column(name = "LicenseName", length = 20)
        @NotNull
        private String BidLicense.licenseName;
        
        @Column(name = "LicenseGroup")
        @NotNull
        private Character BidLicense.licenseGroup;
        
        public Bid BidLicense.getBidIdx() {
            return bidIdx;
        }
        
        public void BidLicense.setBidIdx(Bid bidIdx) {
            this.bidIdx = bidIdx;
        }
        
        public String BidLicense.getLicenseCode() {
            return licenseCode;
        }
        
        public void BidLicense.setLicenseCode(String licenseCode) {
            this.licenseCode = licenseCode;
        }
     .
    .
    .
    .

    com.test.roo.domain.Bid
    Code:
    package kr.co.nanoomi.editor.domain;
    
    import java.util.Date;
    import java.util.Set;
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.ManyToOne;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.validation.constraints.NotNull;
    import com.roo.test.domain.Bid;
    import com.roo.test.domain.BidArea;
    import com.roo.test.domain.BidFiles;
    import com.roo.test.domain.BidItem;
    import com.roo.test.domain.BidLicense;
    import com.roo.test.domain.BidOpening;
    import com.roo.test.BidSuccessful;
    import com.roo.test.BudgetPriceInfo;
    import com.roo.test.domain.CrawlingSite;
    import org.springframework.format.annotation.DateTimeFormat;
    
    privileged aspect Bid_Roo_DbManaged {
        
        @OneToMany(mappedBy = "bidIdx", cascade = CascadeType.ALL)
        private Set<BidArea> Bid.bidAreas;
        
        @OneToMany(mappedBy = "bidIdx", cascade = CascadeType.ALL)
        private Set<BidFiles> Bid.bidFileses;
        
        @OneToMany(mappedBy = "bidIdx", cascade = CascadeType.ALL)
        private Set<BidItem> Bid.bidItems;
        
        @OneToMany(mappedBy = "bidIdx", cascade = CascadeType.ALL)
        private Set<BidLicense> Bid.bidLicenses;
        
        @OneToMany(mappedBy = "bidIdx", cascade = CascadeType.ALL)
        private Set<BidOpening> Bid.bidOpenings;
    .
    .
    .
    .
    .


    and
    com.test.roo.web.BidLicenseController
    Code:
    import org.springframework.http.HttpHeaders;
    .
    .
    .
    .
    
    
    @RooWebJson(jsonObject = BidLicense.class)
    @Controller
    @RequestMapping("/bidlicenses")
    public class BidLicenseController
    {
    @RequestMapping(value = "/{idx}", headers = "Accept=application/json")
        @ResponseBody
        public ResponseEntity<String> showJson(@PathVariable("idx") Integer idx)
        {
    		List<BidLicense> listBL  = BidLicense.findBidLicensesByBidIdx(idx);
    		
            HttpHeaders headers = new HttpHeaders();
            
            headers.add("Content-Type", "application/json; charset=utf-8");
            if (listBL == null)
            {
                return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
            }
            
            String serializer	= new JSONSerializer().rootName("license").serialize(listBL);
            
            return new ResponseEntity<String>(serializer, headers, HttpStatus.OK);
        }
    }
    1:N (Bid:BidLicense)


    i wanna only one query


    Code:
                   2520 Query     select bidlicense0_.Idx as Idx4_, bidlicense0_.BidIdx as BidIdx4_, bidlicense0_.LicenseCode as LicenseC2_4_, bidlicense0_.LicenseGroup as LicenseG3_4_, bidlicense0_.LicenseName as LicenseN4_4_ from BidLicense bidlicense0_ where bidlicense0_.BidIdx=1560
    But

    I think the this query is a waste of resource.

    Code:
    120725 16:37:41  2520 Query     SELECT 1
                     2520 Query     select bidlicense0_.Idx as Idx4_, bidlicense0_.BidIdx as BidIdx4_, bidlicense0_.LicenseCode as LicenseC2_4_, bidlicense0_.LicenseGroup as LicenseG3_4_, bidlicense0_.LicenseName as LicenseN4_4_ from BidLicense bidlicense0_ where bidlicense0_.BidIdx=1560
                     2520 Query     SELECT 1
                     2520 Query     SELECT 1
                     2520 Query     select bid0_.Idx as Idx1_1_, bid0_.BasicAmount as BasicAmo2_1_1_, bid0_.BidGroup as BidGroup1_1_, bid0_.BidNo as BidNo1_1_, bid0_.BidRepeat as BidRepeat1_1_, bid0_.BidStyle as BidStyle1_1_, bid0_.BidTitle as BidTitle1_1_, bid0_.BidType as BidType1_1_, crawlingsi1_.Name as Name5_0_, crawlingsi1_.Url as Url5_0_ from Bid bid0_ inner join CrawlingSite crawlingsi1_ on bid0_.CrawlingSiteIdx=crawlingsi1_.Idx where bid0_.Idx=1560

    What should I do to solve this problem?

    Who is able to do this problem?

  • #2
    Who is able to do this problem?
    You have a query:

    Code:
    public static List<com.test.roo.domain.BidLicense> findBidLicensesByBidIdx(int bidIdx) {
            CriteriaBuilder cb = BidLicense.entityManager().getCriteriaBuilder();
            CriteriaQuery<BidLicense> cq = cb.createQuery(BidLicense.class);
            Root<BidLicense> tableBL = cq.from(BidLicense.class);
            cq.select(tableBL).where(cb.equal(tableBL.get("bidIdx"), bidIdx));
            return entityManager().createQuery(cq).getResultList();
        }
    Not a problem there...

    I believe that the JSON serializer could be the issue:

    Code:
            String serializer	= new JSONSerializer().rootName("license").serialize(listBL);        
            return new ResponseEntity<String>(serializer, headers, HttpStatus.OK);
    It probably tries to dig into the reference to the parent entity and serialize that.

    You should be able to use FlexJson annotations on the collection to NOT serialize it when converting to JSON.

    You would need to either:

    a) push-in refactor all code from the aspect of your DBRE generated entity into the java class, then use the @JSON(serialize = false) from http://flexjson.sourceforge.net/ to turn off serializing the reference to the parent entity.

    b) write a JPA query that fetches your data into a rows that are built using a constructor of a standard JavaBean, like this:

    "select new com.foo.bar.Thing(col1, col2, col3) from thing t where t.id = ?"

    (and thereby turn it into a simple JavaBean, not an Entity, so there is no serialization and you de-couple your entity from the front end, usually not a bad choice). This is actually easy to create and reminds me - we may someday want a @RooConstructors() annotation that can make this easier... Hmm...

    c) write a JPA SQL Query and return the data as a List of Maps, see if that is serializable in the way you want.

    So, I think you either de-couple your result data from your entity design, or you push in your entity and add the JSON annotations to stop serialization.

    Hope this helps.. Best of luck!

    Ken

    Comment


    • #3
      ok thanks a lot

      Comment

      Working...
      X