0

我对 Spring 中的 Hibernate 和 JPA 实现非常陌生,所以基本上已经建立了一个 MySQL 5 数据库,它映射到下面的域对象。

我试图LocationCompany_Details桌子上搜索,但我不知道如何同时搜索Product_Details班级Product_Name

如果有人可以提供帮助,那就太好了。

公司_详情

@Entity
public class Company_Details implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 3336251433829975771L;


@Id
@GeneratedValue
private Integer Id;
private String Company;
private String Address_Line_1;
private String Address_Line_2;
private String Postcode;
private String County;
private String Country;
private String Mobile_Number;
private String Telephone_Number;
private String URL;
private String Contact;
private String Logo_URL;
private double Amount_Overall;
private double Amount_Paid;
private Timestamp Date_Joined;
private int Account_Details;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "Company_Id")
private List<Product_Details> products;

public Integer getId() {
    return Id;
}

public void setId(Integer id) {
    Id = id;
}

public String getCompany() {
    return Company;
}

public void setCompany(String company) {
    Company = company;
}

public String getAddress_Line_1() {
    return Address_Line_1;
}

public void setAddress_Line_1(String address_Line_1) {
    Address_Line_1 = address_Line_1;
}

public String getAddress_Line_2() {
    return Address_Line_2;
}

public void setAddress_Line_2(String address_Line_2) {
    Address_Line_2 = address_Line_2;
}

public String getPostcode() {
    return Postcode;
}

public void setPostcode(String postcode) {
    Postcode = postcode;
}

public String getCounty() {
    return County;
}

public void setCounty(String county) {
    County = county;
}

public String getCountry() {
    return Country;
}

public void setCountry(String country) {
    Country = country;
}

public String getMobile_Number() {
    return Mobile_Number;
}

public void setMobile_Number(String mobile_Number) {
    Mobile_Number = mobile_Number;
}

public String getTelephone_Number() {
    return Telephone_Number;
}

public void setTelephone_Number(String telephone_Number) {
    Telephone_Number = telephone_Number;
}

public String getURL() {
    return URL;
}

public void setURL(String uRL) {
    URL = uRL;
}

public String getContact() {
    return Contact;
}

public void setContact(String contact) {
    Contact = contact;
}

public String getLogo_URL() {
    return Logo_URL;
}

public void setLogo_URL(String logo_URL) {
    Logo_URL = logo_URL;
}

public double getAmount_Overall() {
    return Amount_Overall;
}

public void setAmount_Overall(double amount_Overall) {
    Amount_Overall = amount_Overall;
}

public double getAmount_Paid() {
    return Amount_Paid;
}

public void setAmount_Paid(double amount_Paid) {
    Amount_Paid = amount_Paid;
}

public Timestamp getDate_Joined() {
    return Date_Joined;
}

public void setDate_Joined(Timestamp date_Joined) {
    Date_Joined = date_Joined;
}

public int getAccount_Details() {
    return Account_Details;
}

public void setAccount_Details(int account_Details) {
    Account_Details = account_Details;
}

public List<Product_Details> getProducts() {
    return products;
}

public void setProducts(List<Product_Details> products) {
    this.products = products;
}

产品详情

@Entity
   public class Product_Details implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 6477618197240654478L;

@Id
@GeneratedValue
private Integer Id;
private Integer Company_Id;
private String Product_Name;
private String Description;
private String Main_Photo_URL;
private String Other_Photo_URLS;
private Integer Total_Bookings;
private double Average_Rating;
private Integer Number_Of_Slots;
private Integer Time_Per_Slot;
private double Price_Per_Slot;
private String Monday_Open;
private String Tuesday_Open;
private String Wednesday_Open;
private String Thursday_Open;
private String Friday_Open;
private String Saturday_Open;
private String Sunday_Open;
private String Dates_Closed;

public Integer getId() {
    return Id;
}
public void setId(Integer id) {
    Id = id;
}
public Integer getCompany_Id() {
    return Company_Id;
}
public void setCompany_Id(Integer company_Id) {
    Company_Id = company_Id;
}
public String getProduct_Name() {
    return Product_Name;
}
public void setProduct_Name(String product_Name) {
    Product_Name = product_Name;
}
public String getDescription() {
    return Description;
}
public void setDescription(String description) {
    Description = description;
}
public String getMain_Photo_URL() {
    return Main_Photo_URL;
}
public void setMain_Photo_URL(String main_Photo_URL) {
    Main_Photo_URL = main_Photo_URL;
}
public String getOther_Photos_URLS() {
    return Other_Photo_URLS;
}
public void setOther_Photos_URLS(String other_Photo_URLS) {
    Other_Photo_URLS = other_Photo_URLS;
}
public Integer getTotal_Bookings() {
    return Total_Bookings;
}
public void setTotal_Bookings(Integer total_Bookings) {
    Total_Bookings = total_Bookings;
}
public double getAverage_Rating() {
    return Average_Rating;
}
public void setAverage_Rating(double average_Rating) {
    Average_Rating = average_Rating;
}
public Integer getNumber_Of_Slots() {
    return Number_Of_Slots;
}
public void setNumber_Of_Slots(Integer number_Of_Slots) {
    Number_Of_Slots = number_Of_Slots;
}
public Integer getTime_Per_Slot() {
    return Time_Per_Slot;
}
public void setTime_Per_Slot(Integer time_Per_Slot) {
    Time_Per_Slot = time_Per_Slot;
}
public double getPrice_Per_Slot() {
    return Price_Per_Slot;
}
public void setPrice_Per_Slot(double price_Per_Slot) {
    Price_Per_Slot = price_Per_Slot;
}
public String getMonday_Open() {
    return Monday_Open;
}
public void setMonday_Open(String monday_Open) {
    Monday_Open = monday_Open;
}
public String getTuesday_Open() {
    return Tuesday_Open;
}
public void setTuesday_Open(String tuesday_Open) {
    Tuesday_Open = tuesday_Open;
}
public String getWednesday_Open() {
    return Wednesday_Open;
}
public void setWednesday_Open(String wednesday_Open) {
    Wednesday_Open = wednesday_Open;
}
public String getThursday_Open() {
    return Thursday_Open;
}
public void setThursday_Open(String thursday_Open) {
    Thursday_Open = thursday_Open;
}
public String getFriday_Open() {
    return Friday_Open;
}
public void setFriday_Open(String friday_Open) {
    Friday_Open = friday_Open;
}
public String getSaturday_Open() {
    return Saturday_Open;
}
public void setSaturday_Open(String saturday_Open) {
    Saturday_Open = saturday_Open;
}
public String getSunday_Open() {
    return Sunday_Open;
}
public void setSunday_Open(String sunday_Open) {
    Sunday_Open = sunday_Open;
}
public String getDates_Closed() {
    return Dates_Closed;
}
public void setDates_Closed(String dates_Closed) {
    Dates_Closed = dates_Closed;
}   

}

@Service
public class ProductServiceImpl implements ProductService{

private final static Logger LOG =   Logger.getLogger(ProductServiceImpl.class.getName()); 

@PersistenceContext
EntityManager em;

@Transactional
public List<Company_Details> search(Search search) {
    LOG.info("Entering search method");
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Company_Details> c = builder.createQuery(Company_Details.class);
    Root<Company_Details> companyRoot = c.from(Company_Details.class);
    c.select(companyRoot);
    c.where(builder.equal(companyRoot.get("County"),search.getLocation()));
    return em.createQuery(c).getResultList();
}

}

4

1 回答 1

0

您需要两个标准:Company_Details 类的标准和 Product_Details 类的标准

然后

Criteria crit1 = session.createCriteria(Company_Details.class);
crit1.add(restriction) 

Criteria crit2 = crit1.createCriteria("products");
crit2.add(restriction)

// Then query crit1
List results = crit1.list();
于 2013-01-05T00:42:06.623 回答