gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:30:49 25 4
gpt4 key购买 nike

这只是我在 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");

异常(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)

最佳答案

我认为不应该引用 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;
}

关于java - org.hibernate.MappingException : Unable to find column with logical name Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12664005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com