1

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.

1
  • This is not a composite key query, you are searching by an attribute in your composite key. Please confirm this is what you are trying to accomplish. In any case your current query is incorrect assuming you want to pass a list of attributes. Use this instead - from IfasvVendor t0 where t0.ifasvVendorPK.peId in :idList and query.setParameter("idList", list). Commented Feb 28, 2012 at 16:55

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I believe you and perception are correct. I haven't had a chance to get back to it to try out the fix, but it looks very promising.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.