ManyToOne ClassCastException

时间:2016-09-02 14:49:39

标签: spring hibernate jpa spring-boot

我正在尝试创建一个“简单”的OneToMany和ManyToOne关系。 我得到以下例外:

  

引起:java.lang.ClassCastException:   org.hibernate.mapping.ManyToOne无法强制转换为   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个常见帧   省略

有人能告诉我我做错了吗?

下面你可以找到我的两个实体。

先谢谢你。

  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;

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 个答案:

答案 0 :(得分:1)

感谢您的回答。 我通过添加这些属性解决了这个问题 spring.jpa.hibernate.ddl-AUTO =更新 spring.jpa.generate-DDL =真 问题是spring-data无法更新架构。

答案 1 :(得分:-1)

请检查您的QuestionGroupEntity实体,private String questionGroupID;配置列名称&#34; question_group_id&#34;,但您仍然添加@UniqueConstraint(columnNames={"questionGroupID","questionID"})}&#34; questionGroupID&#34;列,将解除异常

  

引起:org.hibernate.AnnotationException:无法在表question_group_entity上创建唯一键约束(questionGroupID,questionID):数据库列&#39; questionGroupID&#39;未找到。确保使用正确的列名,这取决于使用的命名策略(它可能与实体中的属性名称不同,特别是对于关系类型)       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)       在org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)

修复此问题后,可以使用spring boot 1.4