1

My database is :

TABLE1
"FILE_ID" NUMBER NOT NULL ENABLE, 
"GEO_ZONE" VARCHAR2(2 BYTE) NOT NULL ENABLE,
PRIMARY KEY ("FILE_ID", "GEO_ZONE")

TABLE2
"FILE_ID" NUMBER NOT NULL ENABLE, 
"GEO_ZONE" VARCHAR2(2 BYTE) NOT NULL ENABLE,
"RECORD_NUM" NUMBER, 
PRIMARY KEY ("FILE_ID", "GEO_ZONE", "RECORD_NUM"),
CONSTRAINT "FK_8ULB8IEBEU6A0VK1WTNQA3MCY" FOREIGN KEY ("FILE_ID", "GEO_ZONE")

We can have multiple rows in Table2 for 1 in Table1.

My TABLE1 entity is:

@Entity
@Table(name = "TABLE1")
@IdClass(Table1Id.class)
public class Table1Entity implements Serializable {
private static final long serialVersionUID = 7825109721507305471L;

@Id
@Column(name = "FILE_ID", insertable = false, updatable = false)
private Long fileId;

@Id
@Column(name = "GEO_ZONE", insertable = false, updatable = false)
private String geoZone;

... Others attributes and getter and setter

My Table1Id class is:

public class Table1Id implements Serializable {
    private static final long serialVersionUID = -8618317422024959144L;

    private Long fileId;

    private String geoZone;

... Others attributes and getter and setter

My TABLE2 entity is:

@Entity
@Table(name = "TABLE2")
@IdClass(Table2Id.class)
public class Table2Entity implements Serializable {

    private static final long serialVersionUID = -1344497166638156145L;

    @Id
    @Column(name = "FILE_ID", insertable = false, updatable = false)
    private Long fileId;

    @Id
    @Column(name = "GEO_ZONE", insertable = false, updatable = false)
    private String geoZone;

    @Id
    @Column(name = "RECORD_NUM")
    private Long recordNum;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumns({@JoinColumn(name = "FILE_ID", insertable = false, updatable = false), //
            @JoinColumn(name = "GEO_ZONE", insertable = false, updatable = false)})
    private Table1Entity table1Entity;


... Others attributes and getter and setter

My Table2Id class is:

public class Table2Id implements Serializable {

    private static final long serialVersionUID = -4599660767213338871L;

    private Long fileId;

    private String geoZone;

    private Long recordNum;

... Others attributes and getter and setter

When I try to launch my tomcat, I have the following error:

org.hibernate.MappingException: Foreign key (FK_8ulb8iebeu6a0vk1wtnqa3mcy:TABLE2 [FILE_ID,GEO_ZONE])) must have same number of columns as the referenced primary key (TABLE1 [FILE_ID])

I tried with referenced column, with primary key join column, and many others things but by reading it on internet, it can database modelization problem. I think the problem is that primary key and foreign key have the same name in the 2 tables but I can be wrong... I ask you to confirm it or if you have solution.

Thanks you by advance because 1 week I search to find solution but none of solutions I find work.

EDIT: I changed Table name to toto which doesn't exist in my base and I have the same error with another fk id. It seems hibernate doesn't connect to my database. But if I remove composite key from TABLE1 to have only file_id in join_column in Table2Entity and projects works but it's not what I want. It makes me sick, I don't understand where is the problem at all. And error tells that RDJ_STAT pk is only FILE_ID but no, like if hibernate is drunk

EDIT2 : Is it possible that ojdbc version can be a problem ? And no solution ?

EDIT3 :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa">

    <context:property-placeholder location="classpath:application.properties" />

    <context:annotation-config/>

     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${db.driver}" />
            <property name="url" value="${db.url}" />
            <property name="username" value="${db.username}" />
            <property name="password" value="${db.password}" />
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true"/>
        <property name="generateDdl" value="true"/>
        <property name="database" value="MYSQL"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="persistenceXmlLocation" value="classpath:persistence.xml"></property>
        <!-- spring based scanning for entity classes>-->
    </bean>

     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

    <jpa:repositories base-package="com.sacre.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>

</beans>
5
  • why are PK columns not insertable/updatable? I use these docs to define my compound identity datanucleus.org/products/accessplatform_4_2/jpa/orm/… so using a relation field as (part of) the @Id Commented Feb 23, 2016 at 11:14
  • I have to put insertable = false, updatable = false eather in joincolumn or in column or hibernate says duplicate. Here, I put it in both without real reason And I followed every tutos, every help on stackoverflow and others websites. The only reason I see that it still doesn't work is database problem but maybe not Commented Feb 23, 2016 at 13:11
  • well no you didn't follow the docs I linked ... they use the relation field as the Id rather than dumping in duplicates like you did Commented Feb 23, 2016 at 13:15
  • Indeed, didn't try this but first time I see inner class like that. But I try and tell you ! But it's very like EmbeddedId no ? Commented Feb 23, 2016 at 13:28
  • A friend tried this code on its computer. He added referencedColumnName and equals and hash code method, and changed my id class by an inner class. It works on its PC. The only thing which is kindly different is configuration, or is there a kind of cache on hibernate ? Commented Mar 2, 2016 at 9:28

2 Answers 2

1

The issue of the error is this @ManyToOne try to use @OneToOne:

    @OneToMany(fetch = FetchType.EAGER)
    ...
    private Table1Entity table1Entity;
Sign up to request clarification or add additional context in comments.

3 Comments

Poster has suggested T1 > T2 is one-to-many so how can this be a fix?
What could it changes ? And indeed, I have a ManyToOne relation
I noticed you changer to OneToMany now ? Can I have explanation ? Because OneToMany must have list right ? Or I am missing something hard
0

Yea, could be the fact that the same name is used in both classes. Try putting a table="TABLE1" in your join column definition:

EDIT: Putting table="TABLE1" caused a validation error in eclipse for me. I guess the doc's say that if it is left out, it will point to the target entity table. However, the classes as you have them described above caused this validation error.

Referenced column name must be specified when there are multiple join columns

I addressed it by adding referencedColumnName as it asked:

@JoinColumns({@JoinColumn(name = "FILE_ID", referencedColumnName="FILE_ID", insertable = false, updatable = false), //
        @JoinColumn(name = "GEO_ZONE", referencedColumnName="GEO_ZONE", insertable = false, updatable = false)})

So, it runs under Wildfly 9.0.2.Final, and creates the following SQL:

create table TABLE1 (FILE_ID bigint not null, GEO_ZONE varchar(255) not null, primary key (FILE_ID, GEO_ZONE))

create table TABLE2 (FILE_ID bigint not null, GEO_ZONE varchar(255) not null, RECORD_NUM bigint not null, primary key (FILE_ID, GEO_ZONE, RECORD_NUM))

alter table TABLE2 add constraint FK_4f6hkpolrjfbm222dd74seh6 foreign key (FILE_ID, GEO_ZONE) references TABLE1

So, I didn't try to insert any rows or anything, but it seems like you could have multiple TABLE2 entries referring to a TABLE1 entry, each with a unique recordNum with this schema.

2 Comments

I have org.hibernate.AnnotationException: Cannot find the expected secondary table: no TABLE1 available for com.sacre.entity.Table2Entity I'll search if it's possible
Tried your solution before and nothing. But Eclipse doesn't say anything. Do you know if there is like hibernate cache or something like that ? Because when I read my first error, it seems hibernate doesn't see my composite key on TABLE1 because of referenced primary key (TABLE1 [FILE_ID])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.