0
votes

I am trying to create a "simple" OneToMany and ManyToOne relation. And I get following Exception:

Caused by: java.lang.ClassCastException: org.hibernate.mapping.ManyToOne cannot be cast to org.hibernate.mapping.Component at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:464) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:420) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:758) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:719) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final] at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] ... 39 common frames omitted

Is someone able to tell me what I am doing wrong?

Below you can find my two entities.

Thank you in advanced.

  import javax.persistence.OneToMany;
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"questionGroupID","questionID"})})
public class QuestionGroupEntity implements Serializable{

    private static final long serialVersionUID = 1796640204447018439L;

    @Id
    @NotEmpty
    @Column(name="question_group_id")
    private String questionGroupID;

    @NotEmpty
    private String label;


    @NotEmpty
    private String questionID;


    @OneToMany(fetch=FetchType.EAGER,targetEntity=QuestionGroupMappingEntity.class,mappedBy="questionGroup")
    List<QuestionGroupMappingEntity> questionGroupMappings;

AND

import javax.persistence.ManyToOne;
    @Entity
    @Table(uniqueConstraints={@UniqueConstraint(columnNames={"questionGroupID","questionID","answerID"})})
    public class QuestionGroupMappingEntity implements Serializable {

        private static final long serialVersionUID = 8437546139229082305L;

        @Id
        @SequenceGenerator(name="groupMappingIDGenerator")
        @GeneratedValue(generator="groupMappingIDGenerator",strategy=GenerationType.AUTO)
        private String groupMappingID;

        @NotEmpty
        private String questionID;

        @NotEmpty
        private String questionGroupID;

        @NotEmpty
        private String answerID;

        @ManyToOne(fetch=FetchType.EAGER)
        @JoinColumn(name="question_group_id")
        private QuestionGroupEntity questionGroup;
2
have you defined the getters and setters? On what call are you getting the error?george

2 Answers

1
votes

Thank you for your answers. I solved this issue by adding those properties spring.jpa.hibernate.ddl-auto=update spring.jpa.generate-ddl=true The problem was that spring-data was not able to update the schema.

-1
votes

Please check your QuestionGroupEntity entity, the config the private String questionGroupID; with the column name "question_group_id", but you still add a @UniqueConstraint(columnNames={"questionGroupID","questionID"})} with the "questionGroupID" column, which will occue the exception

Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (questionGroupID, questionID) on table question_group_entity: database column 'questionGroupID' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.buildUniqueKeyFromColumnNames(InFlightMetadataCollectorImpl.java:2090) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.buildUniqueKeyFromColumnNames(InFlightMetadataCollectorImpl.java:1965) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processUniqueConstraintHolders(InFlightMetadataCollectorImpl.java:1953) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1629) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)

After fix this issue, it ok with spring boot 1.4