org.hibernate.MappingException: Unable to find column with logical name Id

Mikhail Ivanov picture Mikhail Ivanov · Sep 30, 2012 · Viewed 12.9k times · Source

It's just one of my first demos on JPA and it's so simple that this error drives me crazy:(

Database (postgresql)

CREATE TABLE "Order"
(
  "Id" integer NOT NULL,
  "DateOf" timestamp with time zone,
  "Description" text,
  CONSTRAINT "PK_Id" PRIMARY KEY ("Id" )
)

CREATE TABLE "LineItem"
(
  "Id" oid NOT NULL,
  "OrderId" integer,
  "Cost" money,
  CONSTRAINT "LineItem_Id" PRIMARY KEY ("Id" ),
  CONSTRAINT "LineItem_OrderId_fkey" FOREIGN KEY ("OrderId")
      REFERENCES "Order" ("Id") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

Model

@Entity
@Table(name="\"Order\"")
@XmlRootElement(name="Order")
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue
    @Column(name="\"Id\"")
    public int Id;

    @Column(name="\"DateOf\"")
    public java.util.Date DateOf;

    @Column(name="\"Description\"")
    public String Description;

    @OneToMany(cascade=CascadeType.REMOVE, mappedBy="order")
    public List<LineItem> Items;
}

@Entity
@Table(name="\"LineItem\"")
public class LineItem implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue
    @Column(name="\"Id\"")
    public int Id;

    @ManyToOne(cascade=CascadeType.REMOVE)
    @JoinColumn(name="\"OrderId\"", referencedColumnName="Id")
    public Order order;
}

middle-tier

EntityManagerFactory entityManagerFactory = Persistence
                .createEntityManagerFactory("store");
        EntityManager entityManager = entityManagerFactory
                .createEntityManager();
        Query select = entityManager.createQuery("select t from Order t");

exception

org.hibernate.MappingException: Unable to find column with logical name: Id in org.hibernate.mapping.Table(Order) and its related supertables and secondary tables
    org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:552)
    org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:257)
    org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
    org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1525)
    org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1446)
    org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1351)
    org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
    org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
    org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    rest.Orders.List(Orders.java:37)

Answer

rbernabe picture rbernabe · Sep 30, 2012

I think that the name of the tables and columns in your create Table statements should not be quoted. Have you tried with the following create Table Statements?

CREATE TABLE Order
(
  Id integer NOT NULL,
  DateOf timestamp with time zone,
  Description text,
  CONSTRAINT PK_Id PRIMARY KEY (Id )
)

CREATE TABLE LineItem
(
  Id oid NOT NULL,
  OrderId integer,
  Cost money,
  CONSTRAINT LineItem_Id PRIMARY KEY (Id ),
  CONSTRAINT LineItem_OrderId_fkey FOREIGN KEY (OrderId)
      REFERENCES Order (Id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

** EDIT I Didn't review the mappings **

I think that the mappings of columns with escapes are being ignored. Also, try to change the mappings with the following:

@Entity
@Table(name="Order")
@XmlRootElement(name="Order")
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue
    @Column(name="Id")
    public int Id;

    @Column(name="DateOf")
    public java.util.Date DateOf;

    @Column(name="Description")
    public String Description;

    @OneToMany(cascade=CascadeType.REMOVE, mappedBy="order")
    public List<LineItem> Items;
}

@Entity
@Table(name="LineItem")
public class LineItem implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue
    @Column(name="Id")
    public int Id;

    @ManyToOne(cascade=CascadeType.REMOVE)
    @JoinColumn(name="OrderId", referencedColumnName="Id")
    public Order order;
}