-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA 2.0 metamodel and composite ids
PostPosted: Wed Aug 11, 2010 4:47 pm 
Newbie

Joined: Wed Aug 11, 2010 4:31 pm
Posts: 1
I have a class:

Code:
@Entity
@IdClass(BadPojo.PK.class)
public class BadPojo implements Serializable {
    @Id
    int id1;

    @Id
    int id2;

    String value;

    public class PK implements Serializable {
        int id1;
        int id2;

        @Override
        public boolean equals(Object o) {...}

        @Override
        public int hashCode() {...}
    }
}

For this class I can obtain only 'value' attribute with EntityType.getDeclaredSingularAttribute.
I can get id1 attribute only iterating through EntityType.getIdClassAttributes() and comparing attribute names. The other option to get the attribute is using static schema mapping.

If I remove @IdClass annotation and BadPojo.PK class, I can not get id1 and id2 attributes for using in criteria query at all. Attributes are unavailable and static schema mapping field is not assigned.

If I leave only one @Id annotation, I can obtain all three attributes with EntityType.getDeclaredSingularAttribute.

Iterating through EntityType.getIdClassAttributes() does not seem to be good practice. If such hibernate behavior correct or it is a bug?


Top
 Profile  
 
 Post subject: Re: JPA 2.0 metamodel and composite ids
PostPosted: Wed Jul 13, 2011 9:15 am 
Newbie

Joined: Wed May 04, 2011 2:24 am
Posts: 10
I am running into the same problem, applying Predicates, Orders and other manipulations on composite ID's is very cumbersome because of this.

A simple piece of code, like this:
Code:
Root<ProjectActivity> root = query.from(ProjectActivity.class);
query.multiselect(root.get("projectId"), root.get("projectActivityId"), root.get("name"));

turns into a big chunck of code:
Code:
Root<ProjectActivity> root = query.from(ProjectActivity.class);
EntityType<ProjectActivity> et = e.getMetamodel().entity(ProjectActivity.class);
Path<?>[] attributes = new Path<?>[3];
for(SingularAttribute<? super ProjectActivity, ?> s : et.getIdClassAttributes()){
   if(s.getName().equals("projectId"))
      attributes[0] = root.get(s);
   if(s.getName().equals("projectActivityId"))
      attributes[1] = root.get(s);
}
attributes[2] = root.get("name");
query.multiselect(attributes);

Any ideas on how to deal with this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.