Incorrect return value for SingularAttribute.getJavaType() / getMember for @IdClass entities


#1

Any particular reason why metamodel.entity(Entity.class). getSingularAttribute("key1").getMember() returns Entity.EntityId#key1 rather than Entity#key1 if Entity.EntityId is the @IdClass of Entity? And equivalently getJavaType() returns i.e. Long rather than the hyptothetical SomeAssociationType? Is this a bug?

@Entity @IdClass(Entity.EntityId.class) public class Entity {
    @Id private Long key2;
    @Id @ManyToOne private SomeAssociationType key1;

    public static class EntityId implements Serializable {
        private Long key1, key2;
    }
}

@Entity public class SomeAssociationType {
    @Id private Long id;
}


// This is the problem
assertEquals(
   metamodel.entity(Entity.class). getSingularAttribute("key1").getJavaType(), // Long
   metamodel.entity(Entity.class). getSingularAttribute("key1").getType().getJavaType() // SomeAssociationType

#2

I don’t understand your question. Please add the actual entity mapping and the output you get from that method call. Thanks.


#4

I don’t think the mapping is ok. You should have an @Id next to the `@ManyToOne and the IdClass should match the association as well.


#5

Of course the @Id should be there. In my situation there is. The issue still occurs. The metamodel keeps returning the property of the Idclass as member for the attribute, rather than the property on the entity class.


#6

But the associated @IdClass does not reflect the @Id annotated fields. Instead of:

public static class EntityId implements Serializable {
        private Long key1, key2;
}

you should have:

public static class EntityId implements Serializable {
        private Long key2;

        private SomeAssociationType key1;
}

Check out the User Guide for an example of such mapping.


#7

The Id class should consist of the id values of the associations. See the example at: https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Example_JPA_2.0_ManyToOne_id_annotation

Ive filed an issue HHH-12969
See also the test provided in https://github.com/hibernate/hibernate-orm/pull/2530