简体   繁体   中英

Hibernate query inner join on unmapped table (unidirectional)

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 xa = yb 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;
    //..
}

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...

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM