如何为多个@One到多个关系创建条件

时间:2014-12-09 20:13:17

标签: java hibernate

我是hibernate的新手,我在创建从数据库获取数据的标准方面遇到了一些麻烦。所以我们在这里:我在数据库中有以下表格: enter image description here 用户实体

 @Entity
 @Table(name = "usertbl")
 public class User implements DomainModel {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private String address;
    private Date dob;
    private String email;
    private String phonenumber;
    private int rank;
    private String paypalAccount;
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
    @JsonManagedReference
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    private Set<Item> items = new HashSet<Item>();

    //getters & setters
}

拍卖实体

@Entity
@Table(name = "auctiontbl")
public class Auction implements DomainModel {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @ManyToOne
    @JoinColumn(name = "itemid")
    @JsonBackReference
    @Fetch(FetchMode.JOIN)  
    private Item item;
    private Date startDate;
    private Date endDate;
    private Float reservedPrice;

    //getters and setters
}

物品实体

@Entity
@Table(name = "itemtbl")
public class Item implements DomainModel {



    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;
    private String description;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "userid", nullable = false)
    @JsonBackReference
    private User user;

    private String picture1;
    private String picture2;
    private String picture3;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "categoryid", nullable = false)
    @JsonBackReference
    private Category category;

    private Status status;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "item")
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    @JsonManagedReference
    @BatchSize(size = 10)
    @Fetch(FetchMode.JOIN)
    private Set<Auction> auctions = new HashSet<Auction>();

    //getters and setters
}

然后我必须创建一个查询以获得符合特定条件的所有拍卖:具有2个边界之间的价格 - 实体拍卖限制,具有特定描述 - 对实体项目的限制以及属于某个特定项目类别 - 实体类别的限制。我无法正确执行此操作,我的应用程序因错误而失败:could not resolve property: price of: online_auction.domain_model.Item;这是我到目前为止所尝试的内容:

@Override
public List<Aution> getAuctionsByRefineAndSearch(Integer categoryId,
        String text, Float startPrice, Float endPrice)
        throws EntityNotFoundException {
    List<Auction> foundAuctions = new ArrayList<Auction>();
    Criteria criteria = getCurrentSession()
            .createCriteria(Auction.class).createCriteria("item");

    // check if the parameters are not null
    // check if the text is not null
    if (text != null) {
        criteria.add(Restrictions
                .disjunction()
                .add(Restrictions.like("name", "%" + text + "%",
                        MatchMode.ANYWHERE))
                .add(Restrictions.like("description", "%" + text + "%",
                        MatchMode.ANYWHERE)));

    }
    if (categoryId != null) {
        criteria.createCriteria("category").add(
                Restrictions.eq("id", categoryId));
    }
    // check if the startPrice are not null
    if (startPrice != null) {
        criteria.add(Restrictions.ge("price", startPrice));
    }
    // check if endPrice is not null
    if (endPrice != null) {
        criteria.add(Restrictions.le("price", endPrice));
    }
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    foundAuctions = criteria.list();

    if (foundAuctions.size() > 0) {
        return foundAuctions;
    } else {
        throw new EntityNotFoundException(
                "Fixed prices matching criteria can not be found");
    }
}

我也从数据库中获取重复数据。我想问题在于映射。您能否提及一些如何正确执行此操作的建议?

2 个答案:

答案 0 :(得分:0)

您的错误消息是您的price课程中没有名为Item的专栏,因为没有。您需要在price课程中添加一个名为Item的字段才能生效。

除上述问题外,您的代码看起来对于实施这些标准是正确的。有一点可能是有意义的。你不应该在DAO中进行空检查。这就是您的服务类的用途。你的DAO应该只关心实现你的查询,而不必担心空值检查。

这只是我的0.02c,但它使你的代码更难以阅读并因此维护。

答案 1 :(得分:0)

我想到了如何摆脱错误,但我不确定这是否是正确的方法。这就是我所做的:

@Override
public List<Auction> getAuctionByRefineAndSearch(Integer categoryId,
        String text, Float startPrice, Float endPrice)
        throws EntityNotFoundException {

    boolean notNull = false;
    List<Auction> foundAuctions = new ArrayList<Auction>();

    Criteria criteria = getCurrentSession().createCriteria(Auction.class);
    // check if the parameters are not null

    // check if the startPrice are not null
    if (startPrice != null) {
        System.out.println("startPrice=" + startPrice);
        criteria.add(Restrictions.ge("reservedPrice", startPrice));
    }
    // check if endPrice is not null
    if (endPrice != null) {
        System.out.println("endPrice=" + endPrice);
        criteria.add(Restrictions.le("reservedPrice", endPrice));
    }
    // check if the text is null
    if (text != null) {
        notNull = true;
        criteria.createCriteria("item").add(
                Restrictions
                        .disjunction()
                        .add(Restrictions.like("name", "%" + text + "%",
                                MatchMode.ANYWHERE))
                        .add(Restrictions.like("description", "%" + text
                                + "%", MatchMode.ANYWHERE)));
    }
    // check if categoryId is not null
    if (categoryId != null) {
        if (notNull) {
            criteria.createCriteria("category").add(
                    Restrictions.eq("id", categoryId));
        } else {
            criteria.createCriteria("item").createCriteria("category")
                    .add(Restrictions.eq("id", categoryId));
        }
    }

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    foundAuctions = criteria.list();

    if (foundAuctions.size() > 0) {
        return foundAuctions;
    } else {
        throw new EntityNotFoundException(
                "Auctions matching criteria can not be found");
    }
}