Hibernate查询未映射表上的内连接(单向)

发布于 2024-12-11 17:48:38 字数 1144 浏览 0 评论 0原文

我确实遇到了 spring 和 hibernate 的查询问题。

我有一个名为 Car 的类,它将 ManyToMany 映射到我的类 Inventory。顺便说一句,库存不包含对 Car 类的引用。 这会导致 spring 和 hibernate 创建映射表 car_loading,其中 fk 指向 car,fk 指向 inventory 表。

我现在想查询一辆特殊汽车的库存:

String squery = "SELECT i from Inventory i, car_loading loads WHERE i.id = loads.loading AND car = ?";

但我得到了异常

org.hibernate.hql.ast.QuerySyntaxException: car_loading is not mapped

仅供参考:Hibernate 不支持 JOIN ON xa = yb 导致我这样做......

提前感谢旅馆的任何帮助!

编辑 - 我的映射

public class Car {

    @OneToOne
    private Driver driver;

    @ManyToMany(cascade=CascadeType.ALL)
    private List<Inventory> loading = new ArrayList<Inventory>();   

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd:MM:yyy HH:mm")
    private Date lastModified;
    //...
}

public class Inventory {

    private Integer shouldAmount;

    private Integer minAmount;

    private Integer isAmount;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd:MM:yyy HH:mm")
    private Date lastModified;
    //..
}

I do have a problem query problem with spring and hibernate.

I've got a class called Car which maps ManyToMany to my class Inventory. Inventory btw holds no references to the Car class.
This causes spring and hibernate to create the mapping table car_loading with a fk to the car and a fk to the inventory table.

I now want to query the inventory for a special car:

String squery = "SELECT i from Inventory i, car_loading loads WHERE i.id = loads.loading AND car = ?";

But I am getting the exception

org.hibernate.hql.ast.QuerySyntaxException: car_loading is not mapped

FYI: Hibernate doesn't support the JOIN ON x.a = y.b leading me to do it that way...

Thanks inn advance for any help!

EDIT - My Mapping

public class Car {

    @OneToOne
    private Driver driver;

    @ManyToMany(cascade=CascadeType.ALL)
    private List<Inventory> loading = new ArrayList<Inventory>();   

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd:MM:yyy HH:mm")
    private Date lastModified;
    //...
}

public class Inventory {

    private Integer shouldAmount;

    private Integer minAmount;

    private Integer isAmount;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd:MM:yyy HH:mm")
    private Date lastModified;
    //..
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2024-12-18 17:48:38

你不应该显式地使用映射表,当你使用实体的属性时,hibernate 会自动添加它。
对于您的情况,查询应如下所示:

"select c.loading from Car c where c = ?"

或者只是获取一个汽车对象 Car car = session.get(Car.class, id),然后像平常一样使用 getter Collection;加载 = car.getLoading();

You should never use the mapping table explicitly, hibernate adds it automatically when you use entity's properties.
For your situation the query should look like:

"select c.loading from Car c where c = ?"

or just get a car object Car car = session.get(Car.class, id), then use getter as ususal Collection<Inventory> loading = car.getLoading();

泛泛之交 2024-12-18 17:48:38

我看到这个问题并想更新它。我做错了。我可以简单地查询汽车并返回这辆车内的所有库存。因为汽车和库存之间存在关系,但反之则不然。因此,查询特定的汽车并简单地返回库存列表属性对我来说是这样的......

I saw this question and wanted to updated it. I did it the wrong way around. I could simply query the car and return all inventorys within this car. Because there is a relation from car to inventory, but not the other way around. So query a specific car and simply return the inventory list attribute did it for me...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文