java org.hibernate.MappingException:无法找到具有逻辑名称 Id 的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12664005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.MappingException: Unable to find column with logical name Id
提问by Mikhail Ivanov
It's just one of my first demos on JPA and it's so simple that this error drives me crazy:(
这只是我在 JPA 上的第一个演示之一,它非常简单以至于这个错误让我发疯:(
Database (postgresql)
数据库(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)
回答by rbernabe
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 语句中的表和列的名称。您是否尝试过以下创建表语句?
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;
}