简体   繁体   中英

Hibernate - Get a row with only one value of a composite primary key?

I have a table (Oracle database) that looks like:

CREATE TABLE example
(
    idEx INTEGER,
    idAdh INTEGER,
    date DATE,
    PRIMARY KEY (idEx, idAdh)
)

I have generated the corresponding classes in Netbeans and I have two classes created for this table: Example.java and ExampleId.java , this last one containing the two values of my primary key.

Now let's say I have some records here and I would like to delete one using only one value of the primary key ( idEx for example, which is unique too). So first I need to get that row, but I can't find a way to do this. Would it be possible to do something like this?

Example ex = (Example) session.get(Example.class, new ExampleId(?, idEx));

I'd need something to replace that ? that would act as a wildcard.

Or maybe this is absolutely not the way to go and in this case I'd really appreciate some advices.

您应该能够使用HQL查询获取ex

There is no way to get this without an HQL/JPQL/Criteria query, for good reason.

The get method returns a single object and automatically generates a query like select * from table where key = :key .

The only way you can guarantee that this query returns exactly one row is when you specify the entire key. If you don't have the full PK object available, the query will return a list of objects, at which point get is not appropriate anymore.

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