简体   繁体   中英

Embeddable Not Working When column is expected to be a FK and PK at the same time

I am trying to learn JPA, I'm currently encountering issues with the embeddable keywords.

My Goal is on Medical History , I would want to make firstName and lastName as Primary Key and Foreign Key however I am encountering an error message.

This is the error message that I am encountering.


Caused by: org.hibernate.AnnotationException: Unable to find column reference in the @MapsId mapping: first_name
    at org.hibernate.cfg.CopyIdentifierComponentSecondPass.createSimpleProperty(CopyIdentifierComponentSecondPass.java:203) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    at org.hibernate.cfg.CopyIdentifierComponentSecondPass.doSecondPass(CopyIdentifierComponentSecondPass.java:112) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1691) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.15.jar:5.3.15]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.15.jar:5.3.15]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.15.jar:5.3.15]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.15.jar:5.3.15]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.15.jar:5.3.15]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
    ... 16 common frames omitted

This is the sample code that i am trying to make work.

    @Embeddable
    public class PersonId implements Serializable {
        String firstName;
        String lastName;
    }
    
    @Entity
    public class Person implements Serializable {
        @EmbeddedId
        PersonId personId;
    }
    
    @Entity
    public class MedicalHistory implements Serializable {
        @EmbeddedId PersonId id;
    
        @MapsId
        @JoinColumns({
                @JoinColumn(name = "firstName", referencedColumnName = "firstName"),
                @JoinColumn(name = "lastName", referencedColumnName = "lastName")
        })
        @OneToOne
        Person patient;
    }

not sure what I am doing wrong?

  • note: I am using this learning material "JSR 338: JavaTM Persistence API, Version 2.2"

@chris is absolutley right, Hibernate is turning the column name fron 'firstName' to 'first_name' and i guess it will be the same for 'lastName'.

You can also change the column names to match the naming like

  @Column(name = "first_name")
  private String firstName;

On the @Embeddable and adapt the name on the @JoinColumn that should do the trick.

If you dont generate your database schema automaticly, you will have to rename also the columns in the database.

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