这只是我在 JPA 上的第一个演示之一,它是如此简单,以至于这个错误让我发疯:(
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
)
型
@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)
我认为您的创建表语句中的表和列的名称不应引起引号。您是否尝试过以下创建表语句?
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;
}