Search code examples
hibernatespring-roo

Spring Roo, Hibernate generates unnecessary foreign key for the primary key column


I'm new to spring mvc, roo and hibernate. I'm using Oracle XE 10g database.

I've created new entity using Roo.

entity --class Opcina
field string --fieldName Naziv
field reference --fieldName Entitet --type ~.domain.Entitet

Generated java class is bellow:

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import randb.domain.Entitet;
import javax.persistence.ManyToOne;

@RooJavaBean
@RooToString
@RooEntity
public class Opcina {

    private String Naziv;

    @ManyToOne
    private Entitet Entitet;
}

Generated table looks like this:

CREATE TABLE "TEST"."OPCINA"
  (
    "ID"      NUMBER(19,0) NOT NULL ENABLE,
    "NAZIV"   VARCHAR2(255 BYTE),
    "VERSION" NUMBER(10,0),
    "ENTITET" NUMBER(19,0),
    PRIMARY KEY ("ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "USERS" ENABLE,
    CONSTRAINT "FKC3C2CDFACAB9A04E" FOREIGN KEY ("ID") REFERENCES "TEST"."OPCINA" ("ID") ENABLE,
    CONSTRAINT "FKC3C2CDFA1A1F0EF1" FOREIGN KEY ("ENTITET") REFERENCES "TEST"."ENTITET" ("ID") ENABLE
  );

What is the purpose of

CONSTRAINT "FKC3C2CDFACAB9A04E" FOREIGN KEY ("ID") REFERENCES "TEST"."OPCINA" ("ID") ENABLE,

constraint, and why is it generated? (How can I avoid it?)


Solution

  • You could have switched over to Entitet by accident during your Roo session. Be explicit with the target class:

    field reference --fieldName Entitet --type ~.domain.Entitet --class ~.domain.Opcina
    

    Or switch focus first:

    focus --class ~.domain.Opcina
    field reference --fieldName Entitet --type ~.domain.Entitet