Hibernate查询未映射表上的内连接(单向)
我确实遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不应该显式地使用映射表,当你使用实体的属性时,hibernate 会自动添加它。
对于您的情况,查询应如下所示:
或者只是获取一个汽车对象
Car car = session.get(Car.class, id)
,然后像平常一样使用 getterCollection;加载 = 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:
or just get a car object
Car car = session.get(Car.class, id)
, then use getter as ususalCollection<Inventory> loading = car.getLoading();
我看到这个问题并想更新它。我做错了。我可以简单地查询汽车并返回这辆车内的所有库存。因为汽车和库存之间存在关系,但反之则不然。因此,查询特定的汽车并简单地返回库存列表属性对我来说是这样的......
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...