Code Junkie
Code Junkie

Reputation: 7788

Hibernate query composite key

I'm trying to perform a composite key query using @embeddable.

Here's what I have thus far.

@Embeddable
public class IfasvVendorPK implements Serializable{

@Column(length = 4, nullable = false)
protected String peId;
@Column(length = 8, nullable = false)
protected String peAddrCd;

Entity

@Entity
public class IfasvVendor implements Serializable {

@EmbeddedId
private IfasvVendorPK ifasvVendorPK;

Query

List contains two pks. Not sure if I should be using a list for this.

            Query query = session.createQuery("from IfasvVendor t0 where     t0.ifasvVendorPK.peId=:id");
            query.setParameter("id", list);
            query.list();

I also wasn't sure how to get the object once I get the query working.

Upvotes: 1

Views: 6147

Answers (1)

buritos
buritos

Reputation: 598

I believe that the following should work:

Query query = session.createQuery("from IfasvVendor t0 where t0.ifasvVendorPK.peId in (:id)");
query.setParameterList("id", list);

query.list();

You must enclose the named parameter in parenthesis in your query and use setParameterList. See the javadoc for setParameterList here.

The query results will be in the list returned by: query.list(). This returns an unchecked list which you may want to cast to List<IfasvVendor>.

btw. this is not a composite key query. see @Perception comment...

Upvotes: 2

Related Questions