org.hibernate.MappingException:无法找到具有逻辑名称Id的列

时间:2012-09-30 18:50:15

标签: java hibernate jpa many-to-one mappingexception

这只是我在JPA上的第一个演示之一,它很简单,这个错误让我发疯:(

数据库(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
)

模型

@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;
}

中间层

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

例外

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)

1 个答案:

答案 0 :(得分:3)

我认为不应引用create table语句中的表和列的名称。您是否尝试过以下创建表语句?

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
)

**编辑我没有查看映射**

我认为带有转义的列的映射被忽略了。另外,尝试使用以下内容更改映射:

@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;
}