简体   繁体   中英

Hibernate JPA Metamodel Generator, missing @ManyToOne Attribute in @Embedabble

We're using Hibernate to generate JPA Metamodel - Classes for our Entities. That's working quite fine for most cases, but if there's a relation ( @ManyToOne ) to an Entity in an @Embeddable , there's no SingularAttribute generated.

Classes are implemented like this (following a "Generation Gap Pattern"):

@Entity
public class EntityA extends EntityABase {

    ....
}

@MappedSuperClass
public abstract class EntityABase {
    @EmbeddedId
    private EntityAPrimaryKey primaryKey;
}

@Embeddable
public class EntityAPrimaryKey extends EntityAPrimaryKeyBase {
    ...
}

@MappedSuperClass
public class EntityAPrimaryKeyBase {
    @ManyToOne
    @NotNull
    private EntityB entityB;

    private String someText;
}

Result is like this

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(EntityAPrimaryKeyBase.class)
public abstract class EntityAPrimaryKeyBase_ {

    public static volatile SingularAttribute<EntityAPrimaryKeyBase, String> someText;
}

So, the "ordinary" field someText is generated fine, but the Attribute for the relationship to EntityB is missing.

The expected output would be

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(EntityAPrimaryKeyBase.class)
public abstract class EntityAPrimaryKeyBase_ {

    public static volatile SingularAttribute<EntityAPrimaryKeyBase, String> someText;
    public static volatile SingularAttribute<EntityAPrimaryKeyBase, EntityB> entityB;
}

All other Metamodel-Classes are generated fine (EntityB, EntityA, EntityABase etc.)

I've tried removing the indirection between EntityAPrimaryKey and EntityAPrimaryKeyBase (and annotating the EntityAPrimaryKeyBase with @ Embeddable ), but that doesn't change the output.

Any ideas why the Attribute entityB isn't generated? Would be very helpful!

JPA Spec (11.1.17) - EmbeddedId Annotation

The EmbeddedId annotation is applied to a persistent field or property of an entity class or mapped superclass to denote a composite primary key that is an embeddable class. The embeddable class must be annotated as Embeddable.[104] Relationship mappings defined within an embedded id class are not supported.

JPA Spec (2.11.2) - Mapped Superclasses

A class designated as a mapped superclass can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself .

You cannot have a relationship mapping within an @Embeddable that is to be used as an @EmbeddedId. Even tho you have the relationship in the @MappedSuperclass, 2.11.2 states that mappings are applied to the subclasses, which in this case is the @Embeddable.

For the record, changing the class structure too (and thus making it according to the spec) solves the Problem:

@Entity
public class EntityA extends EntityABase {

    ....
}

@MappedSuperClass
public abstract class EntityABase {
    @EmbeddedId
    private EntityAPrimaryKey primaryKey;

    @ManyToOne
    @MapsId("entityBID")
    private EntityB entityB;
}

@Embeddable
public class EntityAPrimaryKey extends EntityAPrimaryKeyBase {
    ...
}

@MappedSuperClass
public class EntityAPrimaryKeyBase {

    private Long entityBID;

    private String someText;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM