我正在尝试使用JPA将记录插入表中。我从postgres得到一个错误,列名不正确。有问题的列是另一个表的外键。
Internal Exception: org.postgresql.util.PSQLException: ERROR: column "location_id" of relation "device" does not exist
Position: 125
Error Code: 0
Call: INSERT INTO public.device (ASSETMGR, CIIM, CREATEDDATE, ISDISPOSED, MODIFIEDDATE, NDV, PARTNUMBER, SERIALNUMBER, TAGNUMBER, LOCATION_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
我期望插入语句使用'location'而不是'location_id',实际上我仔细检查了项目中是否指定了'location_id'列名,但找不到。我对数据库中的所有列名使用小写。
有人能指出为什么JPA基础设施试图插入代码中不存在的字段吗?谢谢!
这是Device类:
@MappedSuperclass
public abstract class AbstractDevice implements java.io.Serializable
{
// Fields
/**
*
*/
private static final long serialVersionUID = 7363521382735742845L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private Integer id;
private Integer ciim;
private Integer assetmgr;
private String serialnumber;
private String partnumber;
private String tagnumber;
private Timestamp createddate;
private Timestamp modifieddate;
private Integer ndv;
private Location location;
private Boolean isdisposed;
private Set<LocationReport> locationReports = new HashSet<LocationReport>(0);
// Constructors
/** default constructor */
public AbstractDevice()
{
}
/** minimal constructor */
public AbstractDevice(Integer id, String serialnumber,
Timestamp createddate, Timestamp modifieddate, Boolean isdisposed)
{
this.id = id;
this.serialnumber = serialnumber;
this.createddate = createddate;
this.modifieddate = modifieddate;
this.isdisposed = isdisposed;
}
/** full constructor */
public AbstractDevice(Integer id, Location location, Integer ciim,
Integer assetmgr, String serialnumber, String partnumber,
String tagnumber, Timestamp createddate, Timestamp modifieddate,
Integer ndv, Boolean isdisposed, Set<LocationReport> locationReports)
{
this.id = id;
this.location = location;
this.ciim = ciim;
this.assetmgr = assetmgr;
this.serialnumber = serialnumber;
this.partnumber = partnumber;
this.tagnumber = tagnumber;
this.createddate = createddate;
this.modifieddate = modifieddate;
this.ndv = ndv;
this.isdisposed = isdisposed;
this.locationReports = locationReports;
}
// Property accessors
@Column(name = "id", unique = true, nullable = false)
public Integer getId()
{
return this.id;
}
public void setId(Integer id)
{
this.id = id;
}
@Column(name = "ciim")
public Integer getCiim()
{
return this.ciim;
}
public void setCiim(Integer ciim)
{
this.ciim = ciim;
}
@Column(name = "assetmgr")
public Integer getAssetmgr()
{
return this.assetmgr;
}
public void setAssetmgr(Integer assetmgr)
{
this.assetmgr = assetmgr;
}
@Column(name = "serialnumber", unique = true, nullable = false, length = 32)
public String getSerialnumber()
{
return this.serialnumber;
}
public void setSerialnumber(String serialnumber)
{
this.serialnumber = serialnumber;
}
@Column(name = "partnumber", length = 128)
public String getPartnumber()
{
return this.partnumber;
}
public void setPartnumber(String partnumber)
{
this.partnumber = partnumber;
}
@Column(name = "tagnumber", length = 16)
public String getTagnumber()
{
return this.tagnumber;
}
public void setTagnumber(String tagnumber)
{
this.tagnumber = tagnumber;
}
@Column(name = "createddate", nullable = false, length = 29)
public Timestamp getCreateddate()
{
return this.createddate;
}
public void setCreateddate(Timestamp createddate)
{
this.createddate = createddate;
}
@Column(name = "modifieddate", nullable = false, length = 29)
public Timestamp getModifieddate()
{
return this.modifieddate;
}
public void setModifieddate(Timestamp modifieddate)
{
this.modifieddate = modifieddate;
}
@Column(name = "ndv")
public Integer getNdv()
{
return this.ndv;
}
public void setNdv(Integer ndv)
{
this.ndv = ndv;
}
@Column(name = "isdisposed", nullable = false)
public Boolean getIsdisposed()
{
return this.isdisposed;
}
public void setIsdisposed(Boolean isdisposed)
{
this.isdisposed = isdisposed;
}
@Column(name = "location")
@ManyToOne( cascade = CascadeType.ALL )
public Location getLocation()
{
return this.location;
}
public void setLocation( Location location )
{
this.location = location;
}
public Set<LocationReport> getLocationReports()
{
return this.locationReports;
}
public void setLocationReports(Set<LocationReport> locationReports)
{
this.locationReports = locationReports;
}
@Override
public String toString()
{
return "AbstractDevice [id=" + id + ", ciim=" + ciim + ", assetmgr="
+ assetmgr + ", serialnumber=" + serialnumber + ", partnumber="
+ partnumber + ", tagnumber=" + tagnumber + ", createddate="
+ createddate + ", modifieddate=" + modifieddate + ", ndv="
+ ndv + ", locationReports=" + locationReports + "]";
}
这是设备表的DDL:
CREATE TABLE device
(
ciim integer, -- Corresponding CIIM table id.
assetmgr integer, -- Corresponding asset manager record identifier.
serialnumber character varying(32) NOT NULL, -- A unique hardware identifier identifying this particular piece of equipment among all others from the same vendor.
partnumber character varying(128), -- Vendor supplied part number defining this particular type of unit.
tagnumber character varying(16), -- Tag identifier that identifies the asset for capital management purposes. Can be null and can be duplicated among different serial numbers.
createddate timestamp without time zone NOT NULL DEFAULT now(),
modifieddate timestamp without time zone NOT NULL DEFAULT now(),
ndv integer DEFAULT nextval('device_seq'::regclass), -- foreign key to the device table in the NDV db
id serial NOT NULL,
location integer, -- fkey to location table
isdisposed boolean NOT NULL DEFAULT false,
CONSTRAINT device_pkey PRIMARY KEY (id),
CONSTRAINT device_location_fkey FOREIGN KEY (location)
REFERENCES location (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT "DEVICE_SERIAL_U" UNIQUE (serialnumber)
)
WITH (
OIDS=FALSE
);
ALTER TABLE device
OWNER TO postgres;
COMMENT ON COLUMN device.ciim IS 'Corresponding CIIM table id.';
COMMENT ON COLUMN device.assetmgr IS 'Corresponding asset manager record identifier.
';
COMMENT ON COLUMN device.serialnumber IS 'A unique hardware identifier identifying this particular piece of equipment among all others from the same vendor. ';
COMMENT ON COLUMN device.partnumber IS 'Vendor supplied part number defining this particular type of unit.';
COMMENT ON COLUMN device.tagnumber IS 'Tag identifier that identifies the asset for capital management purposes. Can be null and can be duplicated among different serial numbers.';
COMMENT ON COLUMN device.ndv IS 'foreign key to the device table in the NDV db';
COMMENT ON COLUMN device.location IS 'fkey to location table';
-- Index: device_serialnumber_idx
-- DROP INDEX device_serialnumber_idx;
CREATE INDEX device_serialnumber_idx
ON device
USING btree
(serialnumber COLLATE pg_catalog."default");
-- Index: fki_device_location_fkey
-- DROP INDEX fki_device_location_fkey;
CREATE INDEX fki_device_location_fkey
ON device
USING btree
(location);
这是AbstractLocation.java的主键列,它是device.location的外键。
@MappedSuperclass
public abstract class AbstractLocation implements java.io.Serializable
{
// Fields
/**
*
*/
private static final long serialVersionUID = -8336411995952512794L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private Integer id;
@Column( name="building
答案 0 :(得分:0)
您似乎获得了OneToOne
字段的默认AbstractDevice.location
映射。 JPA规范定义的默认JoinColumn.name
值为
以下内容的串联:引用的名称 关系属性或参考实体或领域 可嵌入的类; “_”;引用的主键列的名称。
在这种情况下,默认值为location_id
。
要更改此设置,您必须指定JoinColumn
:
@JoinColummn(name="location")
private Location location;