-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Hibernate error references a non-existant primary key
PostPosted: Mon Jan 09, 2017 11:39 am 
Newbie

Joined: Mon Jan 09, 2017 11:30 am
Posts: 4
Link to same question on stack overflow

I'm getting the following error when attempting to deploy an application that uses hibernate:

Quote:
Foreign key (FK2e496501ee5lgk3viylwiicsa:MANAGED_APP_TO_CON_TO_SRUCT [MANAGED_APP_TO_CONTACT_ID])) must have same number of columns as the referenced primary key (MANAGED_APPLICATION_TO_CONTACT [CONTACT_ID,MANAGED_APPLICATION_ID])


But I'm not sure what key hibernate is referencing here. As you can see from the model below produced by Oracle SQL Developer, there is no composite primary key on `CONTACT_ID, MANAGED_APPLICATION_ID`. The primary key of `MANAGED_APPLICATION_TO_CONTACT` is on the sequenced column `MANAGED_APP_TO_CONTACT_ID`, which is the column referenced in the foreign key.

Image

So, I am led to believe that the issue lies somewhere in the JPA mappings in my entities. Below are the relevant entities (note that I have opted to post the full entities instead of a minimal example, in case the issue lies somewhere that I am not expecting in my entities):

ManagedApplicationToContact:

Code:
    @Entity
    @Table(name = "MANAGED_APPLICATION_TO_CONTACT", schema = "UAM")
    public class ManagedApplicationToContact implements java.io.Serializable {
   
   
        private Long managedAppToContactId;
        private ManagedApplication managedApplication;
        private Contact contact;
        private Long lastUpdatedBy;
        private Date lastUpdated;
   
        public ManagedApplicationToContact() {
        }
   
        public ManagedApplicationToContact(final ManagedApplication managedApplication, final Contact contact, final Long lastUpdatedBy, final Date lastUpdated) {
            this.managedApplication = managedApplication;
            this.contact = contact;
            this.lastUpdatedBy = lastUpdatedBy;
            this.lastUpdated = lastUpdated;
        }
   
        @SequenceGenerator(name = "generator", sequenceName = "UAM.SEQ_MANAGED_APP_TO_CONTACT", initialValue = 0, allocationSize = 1)
        @Id
        @GeneratedValue(strategy = SEQUENCE, generator = "generator")
        @Column(name = "MANAGED_APP_TO_CONTACT_ID", unique = true, nullable = false, precision = 15, scale = 0)
        public Long getManagedAppToContactId() {
            return this.managedAppToContactId;
        }
   
        public void setManagedAppToContactId(final Long managedAppToContactId) {
            this.managedAppToContactId = managedAppToContactId;
        }
   
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "MANAGED_APPLICATION_ID", nullable = false)
        public ManagedApplication getManagedApplication() {
            return this.managedApplication;
        }
   
        public void setManagedApplication(final ManagedApplication managedApplication) {
            this.managedApplication = managedApplication;
        }
   
        @Column(name = "LAST_UPDATED_BY", nullable = false, precision = 15, scale = 0)
        public Long getLastUpdatedBy() {
            return this.lastUpdatedBy;
        }
   
        public void setLastUpdatedBy(final Long lastUpdatedBy) {
            this.lastUpdatedBy = lastUpdatedBy;
        }
   
        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "LAST_UPDATED", length = 7)
        public Date getLastUpdated() {
            return this.lastUpdated;
        }
   
        public void setLastUpdated(final Date lastUpdated) {
            this.lastUpdated = lastUpdated;
        }
   
        @ManyToOne
        @JoinColumn(name= "CONTACT_ID", referencedColumnName = "CONTACT_ID", nullable = false)
        public Contact getContact() {
            return contact;
        }
   
        public void setContact(final Contact contact) {
            this.contact = contact;
        }
    }


ManagedAppToConToStruct:

Code:
@Entity
    @Table(name = "MANAGED_APP_TO_CON_TO_STRUCT", schema = "UAM")
    public class ManagedAppToConToStruct {
        @SequenceGenerator(name = "generator", sequenceName = "UAM.MAN_APP_TO_CON_TO_STRUCT_SEQ", allocationSize = 1)
   
        @Id
        @GeneratedValue(strategy = SEQUENCE, generator = "generator")
        @Column(name = "ID", unique = true, nullable = false)
        private Long id;
   
        @ManyToOne
        @JoinColumn(name= "MANAGED_APP_TO_CONTACT_ID", referencedColumnName = "MANAGED_APP_TO_CONTACT_ID", nullable = false)
        private ManagedApplicationToContact managedApplicationToContact;
   
        @ManyToOne
        @JoinColumn(name="STRUCTURE_ID", nullable = false)
        private Structure structure;
   
        @Column(name = "LAST_UPDATED")
        private LocalDateTime lastUpdated;
   
        @Column(name = "LAST_UPDATED_BY")
        @Basic
        private Long lastUpdatedBy;
   
        public Long getId() {
            return id;
        }
   
        public void setId(final Long id) {
            this.id = id;
        }
   
        public ManagedApplicationToContact getManagedApplicationToContact() {
            return managedApplicationToContact;
        }
   
        public void setManagedApplicationToContact(final ManagedApplicationToContact managedApplicationToContact) {
            this.managedApplicationToContact = managedApplicationToContact;
        }
   
        public Structure getStructure() {
            return structure;
        }
   
        public void setStructure(final Structure structure) {
            this.structure = structure;
        }
   
        public LocalDateTime getLastUpdated() {
            return lastUpdated;
        }
   
        public void setLastUpdated(final LocalDateTime lastUpdated) {
            this.lastUpdated = lastUpdated;
        }
   
        public Long getLastUpdatedBy() {
            return lastUpdatedBy;
        }
   
        public void setLastUpdatedBy(final Long lastUpdatedBy) {
            this.lastUpdatedBy = lastUpdatedBy;
        }
    }


I even used IntelliJ IDE's built in entity generation feature to build these entities from the database schema, and it produced the same mappings. I added a Hibernate JPA facet to my project, and IntelliJ indicates that all of the mapping is done correctly - so I'm seriously stumped here.

__________________________________________________________________________________

Create statements for the tables:

MANAGED_APPLICATION_TO_CONTACT: (this one was generated from Oracle SQL Developer, as I don't have the original script that was run)

Code:
CREATE TABLE "UAM"."MANAGED_APPLICATION_TO_CONTACT"
       (   "MANAGED_APP_TO_CONTACT_ID" NUMBER(15,0) NOT NULL ENABLE,
       "CONTACT_ID" NUMBER(15,0) NOT NULL ENABLE,
       "MANAGED_APPLICATION_ID" NUMBER(6,0) NOT NULL ENABLE,
       "LAST_UPDATED_BY" NUMBER,
       "LAST_UPDATED" DATE,
        CONSTRAINT "MANAGE_APPLICATION_CONTACT_PK" PRIMARY KEY ("MANAGED_APP_TO_CONTACT_ID")
      USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
      STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
      PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
      BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
      TABLESPACE "UAM"  ENABLE,
        CONSTRAINT "MANAGED_APPLICATION_TO_CO_FK1" FOREIGN KEY ("MANAGED_APPLICATION_ID")
         REFERENCES "UAM"."MANAGED_APPLICATION" ("MANAGED_APPLICATION_ID") ENABLE
       ) SEGMENT CREATION IMMEDIATE
      PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
     NOCOMPRESS LOGGING
      STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
      PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
      BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
      TABLESPACE "UAM" ;


MANAGED_APP_TO_CON_TO_STRUCT: (this is the actual script that was used to create the table)

Code:
CREATE TABLE UAM.MANAGED_APP_TO_CON_TO_STRUCT (
      ID NUMBER NOT NULL,
      MANAGED_APP_TO_CONTACT_ID NUMBER(15) NOT NULL,
      STRUCTURE_ID NUMBER NOT NULL,
      LAST_UPDATED TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
      LAST_UPDATED_BY NUMBER NOT NULL,
      CONSTRAINT MAN_APP_TO_CON_TO_STRUCT_PK PRIMARY KEY (ID),
      CONSTRAINT MAN_APP_TO_CON_TO_STRUCT_FK01 FOREIGN KEY (MANAGED_APP_TO_CONTACT_ID) REFERENCES UAM.MANAGED_APPLICATION_TO_CONTACT (MANAGED_APP_TO_CONTACT_ID),
      CONSTRAINT MAN_APP_TO_CON_TO_STRUCT_FK02 FOREIGN KEY (STRUCTURE_ID) REFERENCES HIERARCHY.STRUCTURE (STRUCTURE_ID),
      CONSTRAINT MAN_APP_TO_CON_TO_STRUCT_FK03 FOREIGN KEY (LAST_UPDATED_BY) REFERENCES MCT.CONTACT (CONTACT_ID)
    );
    CREATE UNIQUE INDEX UAM.MAN_APP_TO_CON_TO_STRUCT_IDX01 ON UAM.MANAGED_APP_TO_CON_TO_STRUCT (MANAGED_APP_TO_CONTACT_ID, STRUCTURE_ID);


_____________________________________________________________________________________________________________________

Full stack trace:

Code:
2017-01-06 15:34:42.834 ERROR [RMI TCP Connection(5)-127.0.0.1] o.s.web.context.ContextLoader:353 - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MCT' defined in com.myproject.JpaConfig: Invocation of init method failed; nested exception is org.hibernate.MappingException: Foreign key (FK2e496501ee5lgk3viylwiicsa:MANAGED_APP_TO_CON_TO_STRUCT [MANAGED_APP_TO_CONTACT_ID])) must have same number of columns as the referenced primary key (MANAGED_APPLICATION_TO_CONTACT [CONTACT_ID,MANAGED_APPLICATION_ID])
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
       at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
       at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
       at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
       at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
       at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054)
       at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:829)
       at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
       at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
       at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
       at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
       at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4990)
       at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5490)
       at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
       at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
       at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
       at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
       at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1763)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:483)
       at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
       at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
       at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
       at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
       at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:483)
       at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
       at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
       at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
       at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
       at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
       at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
       at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399)
       at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:828)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:483)
       at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
       at sun.rmi.transport.Transport$1.run(Transport.java:200)
       at sun.rmi.transport.Transport$1.run(Transport.java:197)
       at java.security.AccessController.doPrivileged(Native Method)
       at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
       at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
       at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
       at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$240(TCPTransport.java:683)
       at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/1638143133.run(Unknown Source)
       at java.security.AccessController.doPrivileged(Native Method)
       at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
       at java.lang.Thread.run(Thread.java:745)
    Caused by: org.hibernate.MappingException: Foreign key (FK2e496501ee5lgk3viylwiicsa:MANAGED_APP_TO_CON_TO_STRUCT [MANAGED_APP_TO_CONTACT_ID])) must have same number of columns as the referenced primary key (MANAGED_APPLICATION_TO_CONTACT [CONTACT_ID,MANAGED_APPLICATION_ID])
       at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:137)
       at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:119)
       at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.secondPassCompileForeignKeys(InFlightMetadataCollectorImpl.java:1888)
       at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.secondPassCompileForeignKeys(InFlightMetadataCollectorImpl.java:1808)
       at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1627)
       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)
       at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
       at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
       at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
       ... 58 common frames omitted


Last edited by ajmair777 on Tue Jan 10, 2017 10:58 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Hibernate error references a non-existant primary key
PostPosted: Tue Jan 10, 2017 7:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There must be a MANAGED_APP_TO_CON_TO_STORE table in your schema that you haven't shown.


Top
 Profile  
 
 Post subject: Re: Hibernate error references a non-existant primary key
PostPosted: Tue Jan 10, 2017 10:55 am 
Newbie

Joined: Mon Jan 09, 2017 11:30 am
Posts: 4
vlad wrote:
There must be a MANAGED_APP_TO_CON_TO_STORE table in your schema that you haven't shown.


Sorry, that's another entity/table that is essentially the same as MANAGED_APP_TO_CON_TO_STRUCT. Only difference is one has a STORE_ID, and one has a STRUCTURE_ID. I get this same exception for both of them. Only showed one of them for brevity, but posted the stack trace for the other. I updated the stack trace to match.


Top
 Profile  
 
 Post subject: Re: Hibernate error references a non-existant primary key
PostPosted: Tue Jan 10, 2017 4:27 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Are you sing something like:

Code:
<property name="hibernate.hbm2ddl.auto" value="update" />


Or with a value of "validate", or something like that?


Top
 Profile  
 
 Post subject: Re: Hibernate error references a non-existant primary key
PostPosted: Tue Jan 10, 2017 4:42 pm 
Newbie

Joined: Mon Jan 09, 2017 11:30 am
Posts: 4
vlad wrote:
Are you sing something like:

Code:
<property name="hibernate.hbm2ddl.auto" value="update" />


Or with a value of "validate", or something like that?


Yes, we use "validate".

Code:
jpaProperties.setProperty("hibernate.hbm2ddl.auto", "validate");


Top
 Profile  
 
 Post subject: Re: Hibernate error references a non-existant primary key
PostPosted: Wed Jan 11, 2017 2:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You don't need to use validate if you generate your schema externally. Remove the hbm2ddl configuration and see if it works.


Top
 Profile  
 
 Post subject: Re: Hibernate error references a non-existant primary key
PostPosted: Thu Jan 12, 2017 1:37 pm 
Newbie

Joined: Mon Jan 09, 2017 11:30 am
Posts: 4
So it turns out that I had this mapping in the `Contact` entity that was using `MANAGED_APPLICATION_TO_CONTACT` as a join table to retrieve the `ManagedApplication` records tied to the `Contact`, which was causing hibernate to assume the primary key on `CONTACT_ID,MANAGED_APPLICATION_ID`:

Code:
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "MANAGED_APPLICATION_TO_CONTACT", schema = "UAM",
            joinColumns = {@JoinColumn(name = "CONTACT_ID",
                    referencedColumnName = "CONTACT_ID",
                    nullable = false)},
            inverseJoinColumns = {@JoinColumn(name = "MANAGED_APPLICATION_ID",
                    referencedColumnName = "MANAGED_APPLICATION_ID",
                    nullable = false)})
    @OrderBy("name asc")
    public Set<ManagedApplication> getManagedApplications() {
        return this.managedApplications;
    }

    public void setManagedApplications(final Set<ManagedApplication> managedApplications) {
        this.managedApplications = managedApplications;
    }


To correct this, I got rid of this mapping and made it into a transient method, which just pulls the `ManagedApplication` records out of the already properly mapped `managedApplicationToContacts` records.

Code:
    @Transient
    public Set<ManagedApplication> getManagedApplications() {
        if (this.managedApplications.isEmpty()) {
            this.managedApplications = this.managedApplicationToContacts.stream()
                    .map(ManagedApplicationToContact::getManagedApplication)
                    .collect(Collectors.toSet());
        }
        return this.managedApplications;
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.