2

我正在使用 java (Netbeans IDE) 创建宁静的服务。我从两个相关的表中创建了两个实体 claees。而且我还从模式创建了宁静的服务。当我测试子表的 HTTP GET 方法(在本例中为类别)时,它会提供来自子表和父表的数据,但我只需要来自子表的数据以及外键的列值。到目前为止的步骤如下:

我在 mysql 1) 站点表中创建以下两个表

    create table `revise`.site
(
    siteId INT not null auto_increment primary key,
    shortName VARCHAR(255),
    longName VARCHAR(1024) ,
    addressLineFirst VARCHAR(2024)l,
    city VARCHAR(255) ,
    state VARCHAR(255),
    pincode VARCHAR(255),
    country VARCHAR(255) ,
    phoneNo VARCHAR(255),
    mobileNo VARCHAR(255),
    fax VARCHAR(255),
    emailId VARCHAR(255),
    logoSmall VARCHAR(1024),
    logoBig VARCHAR(1024),
    activeStatus VARCHAR(255),
    action VARCHAR(255)
)

2) 类别表

   create table `revise`.category
(
    categoryId INT not null auto_increment primary key,
    siteId INT, 
    shortName VARCHAR(255),
    longName VARCHAR(1024),
    description VARCHAR(8000),
    logoSmall VARCHAR(8000),
    logoBig VARCHAR(8000),
    CONSTRAINT fk_Site FOREIGN KEY (siteId) REFERENCES site(siteId) on delete cascade on update cascade
)

从这两个表中,我创建了以下两个实体类 1) Site.java

 @Entity
@Table(name = "site")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Site.findAll", query = "SELECT s FROM Site s"),
    @NamedQuery(name = "Site.findBySiteId", query = "SELECT s FROM Site s WHERE s.siteId = :siteId"),
    @NamedQuery(name = "Site.findByShortName", query = "SELECT s FROM Site s WHERE s.shortName = :shortName"),
    @NamedQuery(name = "Site.findByLongName", query = "SELECT s FROM Site s WHERE s.longName = :longName"),
    @NamedQuery(name = "Site.findByAddressLineFirst", query = "SELECT s FROM Site s WHERE s.addressLineFirst = :addressLineFirst"),
    @NamedQuery(name = "Site.findByCity", query = "SELECT s FROM Site s WHERE s.city = :city"),
    @NamedQuery(name = "Site.findByState", query = "SELECT s FROM Site s WHERE s.state = :state"),
    @NamedQuery(name = "Site.findByPincode", query = "SELECT s FROM Site s WHERE s.pincode = :pincode"),
    @NamedQuery(name = "Site.findByCountry", query = "SELECT s FROM Site s WHERE s.country = :country"),
    @NamedQuery(name = "Site.findByPhoneNo", query = "SELECT s FROM Site s WHERE s.phoneNo = :phoneNo"),
    @NamedQuery(name = "Site.findByMobileNo", query = "SELECT s FROM Site s WHERE s.mobileNo = :mobileNo"),
    @NamedQuery(name = "Site.findByFax", query = "SELECT s FROM Site s WHERE s.fax = :fax"),
    @NamedQuery(name = "Site.findByEmailId", query = "SELECT s FROM Site s WHERE s.emailId = :emailId"),
    @NamedQuery(name = "Site.findByLogoSmall", query = "SELECT s FROM Site s WHERE s.logoSmall = :logoSmall"),
    @NamedQuery(name = "Site.findByLogoBig", query = "SELECT s FROM Site s WHERE s.logoBig = :logoBig"),
    @NamedQuery(name = "Site.findByActiveStatus", query = "SELECT s FROM Site s WHERE s.activeStatus = :activeStatus"),
    @NamedQuery(name = "Site.findByAction", query = "SELECT s FROM Site s WHERE s.action = :action")})
public class Site implements Serializable {
    @OneToMany(mappedBy = "siteId", fetch = FetchType.EAGER)
    private List<Category> categoryList;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "siteId")
    private Integer siteId;
    @Size(max = 255)
    @Column(name = "shortName")
    private String shortName;
    @Size(max = 1024)
    @Column(name = "longName")
    private String longName;
    @Size(max = 2024)
    @Column(name = "addressLineFirst")
    private String addressLineFirst;
    @Size(max = 255)
    @Column(name = "city")
    private String city;
    @Size(max = 255)
    @Column(name = "state")
    private String state;
    @Size(max = 255)
    @Column(name = "pincode")
    private String pincode;
    @Size(max = 255)
    @Column(name = "country")
    private String country;
    @Size(max = 255)
    @Column(name = "phoneNo")
    private String phoneNo;
    @Size(max = 255)
    @Column(name = "mobileNo")
    private String mobileNo;
    // @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
    @Size(max = 255)
    @Column(name = "fax")
    private String fax;
    @Size(max = 255)
    @Column(name = "emailId")
    private String emailId;
    @Size(max = 1024)
    @Column(name = "logoSmall")
    private String logoSmall;
    @Size(max = 1024)
    @Column(name = "logoBig")
    private String logoBig;
    @Size(max = 255)
    @Column(name = "activeStatus")
    private String activeStatus;
    @Size(max = 255)
    @Column(name = "action")
    private String action;

    public Site() {
    }

    public Site(Integer siteId) {
        this.siteId = siteId;
    }

    public Integer getSiteId() {
        return siteId;
    }

    public void setSiteId(Integer siteId) {
        this.siteId = siteId;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public String getLongName() {
        return longName;
    }

    public void setLongName(String longName) {
        this.longName = longName;
    }

    public String getAddressLineFirst() {
        return addressLineFirst;
    }

    public void setAddressLineFirst(String addressLineFirst) {
        this.addressLineFirst = addressLineFirst;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getLogoSmall() {
        return logoSmall;
    }

    public void setLogoSmall(String logoSmall) {
        this.logoSmall = logoSmall;
    }

    public String getLogoBig() {
        return logoBig;
    }

    public void setLogoBig(String logoBig) {
        this.logoBig = logoBig;
    }

    public String getActiveStatus() {
        return activeStatus;
    }

    public void setActiveStatus(String activeStatus) {
        this.activeStatus = activeStatus;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (siteId != null ? siteId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Site)) {
            return false;
        }
        Site other = (Site) object;
        if ((this.siteId == null && other.siteId != null) || (this.siteId != null && !this.siteId.equals(other.siteId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.ikanksha.reviseplus.api.entity.Site[ siteId=" + siteId + " ]";
    }
}

2) 类别.java

   @Entity
@Table(name = "category")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
    @NamedQuery(name = "Category.findByCategoryId", query = "SELECT c FROM Category c WHERE c.categoryId = :categoryId"),
    @NamedQuery(name = "Category.findByShortName", query = "SELECT c FROM Category c WHERE c.shortName = :shortName"),
    @NamedQuery(name = "Category.findByLongName", query = "SELECT c FROM Category c WHERE c.longName = :longName"),
    @NamedQuery(name = "Category.findByDescription", query = "SELECT c FROM Category c WHERE c.description = :description"),
    @NamedQuery(name = "Category.findByLogoSmall", query = "SELECT c FROM Category c WHERE c.logoSmall = :logoSmall"),
    @NamedQuery(name = "Category.findByLogoBig", query = "SELECT c FROM Category c WHERE c.logoBig = :logoBig")})
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "categoryId")
    private Integer categoryId;
    @Size(max = 255)
    @Column(name = "shortName")
    private String shortName;
    @Size(max = 1024)
    @Column(name = "longName")
    private String longName;
    @Size(max = 8000)
    @Column(name = "description")
    private String description;
    @Size(max = 8000)
    @Column(name = "logoSmall")
    private String logoSmall;
    @Size(max = 8000)
    @Column(name = "logoBig")
    private String logoBig;
    @OneToMany(mappedBy = "categoryId", fetch = FetchType.EAGER)
    private List<Topic> topicList;
    @JoinColumn(name = "siteId", referencedColumnName = "siteId")
    @ManyToOne(fetch = FetchType.EAGER)
    private Site siteId;


    public Category() {
    }

    public Category(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public String getLongName() {
        return longName;
    }

    public void setLongName(String longName) {
        this.longName = longName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLogoSmall() {
        return logoSmall;
    }

    public void setLogoSmall(String logoSmall) {
        this.logoSmall = logoSmall;
    }

    public String getLogoBig() {
        return logoBig;
    }

    public void setLogoBig(String logoBig) {
        this.logoBig = logoBig;
    }


    public Site getSiteId() {
        return siteId;
    }

    public void setSiteId(Site siteId) {
        this.siteId = siteId;
    }



    @Override
    public int hashCode() {
        int hash = 0;
        hash += (categoryId != null ? categoryId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Category)) {
            return false;
        }
        Category other = (Category) object;
        if ((this.categoryId == null && other.categoryId != null) || (this.categoryId != null && !this.categoryId.equals(other.categoryId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.ikanksha.reviseplus.api.entity.Category[ categoryId=" + categoryId + " ]";
    }

}

我得到以下 json 数据作为请求

http://localhost:8080/ReviseAPIDemo-war/webresources/categories/1

{
  "categoryId": "1",
  "description": "fdgfd",
  "logoBig": "dfgf",
  "logoSmall": "fgdf",
  "longName": "lnhdr",
  "shortName": "shrt",
  "siteId": {
    "addressLineFirst": "Vijapurroad",
    "city": "Solapur",
    "country": "India",
    "emailId": "soni@dhb.com",
    "fax": "834343443",
    "logoBig": "biglogo",
    "logoSmall": "small",
    "longName": "DHB Soni College",
    "mobileNo": "9898123844",
    "phoneNo": "23203361",
    "pincode": "413004",
    "shortName": "DHB",
    "siteId": "1",
    "state": "Maharashtra"
  }

}

但我需要回应

 {
      "categoryId": "1",
      "description": "fdgfd",
      "logoBig": "dfgf",
      "logoSmall": "fgdf",
      "longName": "lnhdr",
      "shortName": "shrt",
      "siteId": "1",

}
4

1 回答 1

0

您可以做的事情是加载 Category 实体(在您的 JAX-RS 资源中?)从相应的 Site 读取 Id,然后创建一个所有属性为 null 的新 Site 对象,设置正确的 siteId,然后将其存储在 Category 对象中. 然后 JSON 编组器应该只返回 id。

于 2013-02-21T13:00:40.023 回答