2013-06-18 2 views
0

У меня есть следующие таблицы Марка (Brand_ID, BRAND_NAME) Веб-сайт (website_id, Brand_ID, и т.д ...)Hibernate присоединиться таблицы запроса

Если веб-сайт может иметь много брендов. Ниже приводится копия моего бренда и вебсайта классов:

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»

Я новичок в спящий режим и не могу понять как это сделать. Может кому-то помочь. благодаря

ответ

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); 
Смежные вопросы