Reputation: 33
I have a composite primary key
public class MainPk{
private int keyOne;
private int keyTwo;
// getters and setters
}
And i set only KeyOne however in the query the both values are searched for
select * from ..... where keyOne = ? and KeyTwo = ?
Is there anyway to search only for one? I know i can use criteria or query, i am asking if it is possible with this way
Upvotes: 0
Views: 753
Reputation: 21113
The problem is you're probably doing something like the following:
MainPk id = new MainPk();
id.setKeyOne( someValue );
List<YourEntity> results = session
.createQuery( "FROM YourEntity e WHERE e.id = :id" )
.setParameter( "id", id )
.getResultList();
You can reconstruct your query and accomplish what you're seeking by explicitly only querying against the single value you have of the composite id. Hibernate doesn't know whether to translate your null value in the composite as a literal null
or if it should ignore it.
List<YourEntity> results = session
.createQuery( "FROM YourEntity e WHERE e.id.keyOne = :keyOne" )
.setParameter( "keyOne", someValue )
.getResultList();
Upvotes: 1
Reputation: 268
You can search for any property. Why don't you use Criteria and Restriction classes? They allow you to make easy queries based on any property. Review the documentation of Hibernate:
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html
Upvotes: 0