如何在另一个表中创建具有属性的hibernate订单?

时间:2011-12-29 09:09:43

标签: hibernate

我正在尝试在桌子上创建一个订单,其中一个要“排序”的项目恰好是属于该关系的一端的属性。 我可以通过键的名称属性的名称(在外表中)还是表的名称来指定属性? 目前有: order = new Order[]{Order.asc("lastControlDate"), Order.asc("idManufacturer"), Order.asc("model")}; idManufacturer 中的外键是外键 感谢

实体制造商 1..M 汽车其中:

制造商:

public class Manufacturer{
    private Long idManufacturer;
    private String name;

//<editor-fold defaultstate="collapsed" desc="Constructors">
public Manufacturer(){
//getters and setters....

}

汽车:

public class Car {
private long idCar;
private String model;
private Manufacturer manufacturer;
//Constructor, getters and setters...
}

order = new Order [] {Order.desc(“manufacturer”),Order.desc(“model”)};

public static List<Car> listCarsByManufacturer(Manufacturer name, Order...orders)throws Exception{
        LinkedList<Criterion> criterions = new LinkedList<Criterion>();
        criterions.add(Restrictions.like("Car.manufacturer", name));

        List<Car> cars =carDAO.findAllCars(criterions, orders);
        return cars;
}

1 个答案:

答案 0 :(得分:2)

您仍未在CarDAO.findAllCars()中向我们展示查询。

无论如何,这里是你如何获得一个汽车列表,其中包含制造商名称的限制和制造商ID的订单:

Criteria c = session.createCriteria(Car.class, "car");
// to add a restriction on a property of manufacturer, you need
// an inner join on manufacturer
c.createAlias("car.manufacturer", "manufacturer"); 
c.add(Restrictions.like("manufacturer.name", name));
// you want to get the car with their manufacturer, so you need a fetch
c.setFetchMode(car.manufacturer", FetchMode.JOIN);
// you want to order by id of manufacturer
c.addOrder(Order.asc("manufacturer.idManufacturer"));

所有这些都在Hibernate reference manual中得到了很好的解释。