0

我有以下表格 品牌 (brand_id, brand_name) 网站 (website_id, brand_id, etc...)

一个网站可以有很多品牌。以下是我的品牌和网站课程的副本:

public class Website  implements java.io.Serializable {
private int websiteId;
     private WebsiteType websiteType;
     private Brand brand;
     private Serializable websiteTradingName;
     private Serializable websiteDomain;
     private Serializable analyticsId;
     private String countryId;
     private Boolean websiteStatus;
     private Serializable websiteHomeFolder;
     private Serializable websitePrivacyPolicy;
     private Set cmsPages = new HashSet(0);
     private Set websiteDomainNames = new HashSet(0);
     private Set cmsLogins = new HashSet(0);
     private Set websiteImages = new HashSet(0);

@Id 

    @Column(name="website_id", unique=true, nullable=false)
    public int getWebsiteId() {
        return this.websiteId;
    }

    public void setWebsiteId(int websiteId) {
        this.websiteId = websiteId;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="website_type_id", nullable=false)
    public WebsiteType getWebsiteType() {
        return this.websiteType;
    }

    public void setWebsiteType(WebsiteType websiteType) {
        this.websiteType = websiteType;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="brand_id", nullable=false)
    public Brand getBrand() {
        return this.brand;
    }

    public void setBrand(Brand brand) {
        this.brand = brand;
    }

    @Column(name="website_trading_name", nullable=false)
    public Serializable getWebsiteTradingName() {
        return this.websiteTradingName;
    }

    public void setWebsiteTradingName(Serializable websiteTradingName) {
        this.websiteTradingName = websiteTradingName;
    }

    @Column(name="website_domain", nullable=false)
    public Serializable getWebsiteDomain() {
        return this.websiteDomain;
    }

    public void setWebsiteDomain(Serializable websiteDomain) {
        this.websiteDomain = websiteDomain;
    }

    @Column(name="analytics_id")
    public Serializable getAnalyticsId() {
        return this.analyticsId;
    }

    public void setAnalyticsId(Serializable analyticsId) {
        this.analyticsId = analyticsId;
    }

    @Column(name="country_id", nullable=false, length=2)
    public String getCountryId() {
        return this.countryId;
    }

    public void setCountryId(String countryId) {
        this.countryId = countryId;
    }

    @Column(name="website_status")
    public Boolean getWebsiteStatus() {
        return this.websiteStatus;
    }

    public void setWebsiteStatus(Boolean websiteStatus) {
        this.websiteStatus = websiteStatus;
    }

    @Column(name="website_home_folder")
    public Serializable getWebsiteHomeFolder() {
        return this.websiteHomeFolder;
    }

    public void setWebsiteHomeFolder(Serializable websiteHomeFolder) {
        this.websiteHomeFolder = websiteHomeFolder;
    }

    @Column(name="website_privacy_policy")
    public Serializable getWebsitePrivacyPolicy() {
        return this.websitePrivacyPolicy;
    }

    public void setWebsitePrivacyPolicy(Serializable websitePrivacyPolicy) {
        this.websitePrivacyPolicy = websitePrivacyPolicy;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="website")
    public Set getCmsPages() {
        return this.cmsPages;
    }

    public void setCmsPages(Set cmsPages) {
        this.cmsPages = cmsPages;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="website")
    public Set getWebsiteDomainNames() {
        return this.websiteDomainNames;
    }

    public void setWebsiteDomainNames(Set websiteDomainNames) {
        this.websiteDomainNames = websiteDomainNames;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="website")
    public Set getCmsLogins() {
        return this.cmsLogins;
    }

    public void setCmsLogins(Set cmsLogins) {
        this.cmsLogins = cmsLogins;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="website")
    public Set getWebsiteImages() {
        return this.websiteImages;
    }

    public void setWebsiteImages(Set websiteImages) {
        this.websiteImages = websiteImages;
    }
}


public class Brand  implements java.io.Serializable {
private static final long serialVersionUID = 1L;

     private int brandId;
     private String brandName;
     private Set websites = new HashSet(0);

    public Brand() {
    }


    public Brand(int brandId, String brandName) {
        this.brandId = brandId;
        this.brandName = brandName;
    }
    public Brand(int brandId, String brandName, Set websites) {
       this.brandId = brandId;
       this.brandName = brandName;
       this.websites = websites;
    }

    @Id 
    @Column(name="brand_id", unique=true, nullable=false)
    public int getBrandId() {
        return this.brandId;
    }

    public void setBrandId(int brandId) {
        this.brandId = brandId;
    }

    @Column(name="brand_name", nullable=false, length=50)
    public String getBrandName() {
        return this.brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="brand")
    public Set getWebsites() {
        return this.websites;
    }

    public void setWebsites(Set websites) {
        this.websites = websites;
    }

}

我想做的是“从brand_id = X的网站中选择*”

我是 Hibernate 的新手,不知道该怎么做。有人可以帮忙吗。谢谢

4

1 回答 1

0

如果您可以获得休眠会话工厂的引用,则可以构建一些 DAO 来检索数据,如下所示:

public class BrandDAO {
    SessionFactory sessionFactory

    public Brand getBrand(Serializable ID) {
        return sessionFactory.getCurrentSession().load(Brand.class, ID);
    }
}

-

public class WebsiteDAO {
    SessionFactory sessionFactory

    public Website getWebsiteByBrand(Brand brand) {
        return (Website)sessionFactory.getCurrentSession().createCriteria(Website.class).add(Restrictions.eq("brand", brand)).uniqueResult()
    }
}

那么你可以做

Brand brand = brandDAO.getBrand(1l);
Website website = websiteDAO.getWebsiteByBrand(brand);
于 2013-06-18T03:48:01.347 回答