我有2个表,devices包含设备列表,dev_tags包含这些设备的资产标签列表。这些表在dev_serial_num上联接,这是两个表的主键。这些设备在其ip_address字段上是唯一的,并且具有由dev_id标识的主键。设备在2周后“老化”。因此,同一件硬件可以在设备中出现多次。
我提到这是为了解释为什么dev_tags与设备之间存在一个OneToMany关系,而这似乎应该是一个OneToOne关系。
所以我有两个实体
@Entity
@Table(name = "dev_tags")
public class DevTags implements Serializable {
private Integer tagId;
private String devTagId;
private String devSerialNum;
private List<Devices> devices;
@Id
@GeneratedValue
@Column(name = "tag_id")
public Integer getTagId() {
return tagId;
}
public void setTagId(Integer tagId) {
this.tagId = tagId;
}
@Column(name="dev_tag_id")
public String getDevTagId() {
return devTagId;
}
public void setDevTagId(String devTagId) {
this.devTagId = devTagId;
}
@Column(name="dev_serial_num")
public String getDevSerialNum() {
return devSerialNum;
}
public void setDevSerialNum(String devSerialNum) {
this.devSerialNum = devSerialNum;
}
@OneToMany(mappedBy="devSerialNum")
public List<Devices> getDevices() {
return devices;
}
public void setDevices(List<Devices> devices) {
this.devices = devices;
}
}
和这个
public class Devices implements java.io.Serializable {
private Integer devId;
private Integer officeId;
private String devSerialNum;
private String devPlatform;
private String devName;
private OfficeView officeView;
private DevTags devTag;
public Devices() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "dev_id", unique = true, nullable = false)
public Integer getDevId() {
return this.devId;
}
public void setDevId(Integer devId) {
this.devId = devId;
}
@Column(name = "office_id", nullable = false, insertable=false, updatable=false)
public Integer getOfficeId() {
return this.officeId;
}
public void setOfficeId(Integer officeId) {
this.officeId = officeId;
}
@Column(name = "dev_serial_num", nullable = false, length = 64, insertable=false, updatable=false)
@NotNull
@Length(max = 64)
public String getDevSerialNum() {
return this.devSerialNum;
}
public void setDevSerialNum(String devSerialNum) {
this.devSerialNum = devSerialNum;
}
@Column(name = "dev_platform", nullable = false, length = 64)
@NotNull
@Length(max = 64)
public String getDevPlatform() {
return this.devPlatform;
}
public void setDevPlatform(String devPlatform) {
this.devPlatform = devPlatform;
}
@Column(name = "dev_name")
public String getDevName() {
return devName;
}
public void setDevName(String devName) {
this.devName = devName;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "office_id")
public OfficeView getOfficeView() {
return officeView;
}
public void setOfficeView(OfficeView officeView) {
this.officeView = officeView;
}
@ManyToOne()
@JoinColumn(name="dev_serial_num")
public DevTags getDevTag() {
return devTag;
}
public void setDevTag(DevTags devTag) {
this.devTag = devTag;
}
}
我把@JoinColumn(name =)和@OneToMany的mapledBy属性弄得一团糟,我无法正确地做到这一点。我终于可以编译该死的东西了,但是查询仍在尝试将devices.dev_serial_num与dev_tags.tag_id(该实体的@Id)连接起来。这是控制台的成绩单:
13:12:16,970 INFO [STDOUT] Hibernate:
select
devices0_.office_id as office5_2_,
devices0_.dev_id as dev1_2_,
devices0_.dev_id as dev1_156_1_,
devices0_.dev_name as dev2_156_1_,
devices0_.dev_platform as dev3_156_1_,
devices0_.dev_serial_num as dev4_156_1_,
devices0_.office_id as office5_156_1_,
devtags1_.tag_id as tag1_157_0_,
devtags1_.comment as comment157_0_,
devtags1_.dev_serial_num as dev3_157_0_,
devtags1_.dev_tag_id as dev4_157_0_
from
ond.devices devices0_
left outer join
ond.dev_tags devtags1_
on devices0_.dev_serial_num=devtags1_.tag_id
where
devices0_.office_id=?
13:12:16,970 INFO [IntegerType] could not read column value from result set: dev4_156_1_; Invalid value for getInt() - 'FDO1129Y2U4'
13:12:16,970 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: S1009
13:12:16,970 ERROR [JDBCExceptionReporter] Invalid value for getInt() - 'FDO1129Y2U4'
getInt()'FD01129Y2U4'的值是一个序列号,绝对不是Int!我在这里想念/误解了什么?我可以在我想要的任何字段上加入2个表,还是至少有一个必须是主键?
最佳答案
简短的回答是“不,您不能在任何字段上连接2个表”;关联总是在一侧引用主键。
@OneToMany的“ mappedBy”属性用于bi-directional assocations,并指定集合元素上属性的名称,该属性映射回所有者实体为@ManyToOne。就你而言
@OneToMany(mappedBy="devSerialNum")
声明无效;它应该更改为
@OneToMany(mappedBy="devTag")
相反,如果您想保持双向关系。 @JoinColumn可以与@ManyToOne一起使用,以指定指向另一个表的(外键)列的名称。就你而言
@ManyToOne()
@JoinColumn(name="dev_serial_num")
public DevTags getDevTag() {
声明说您在
dev_serial_num
表中有一列称为devices
的列,该列将是指向dev_tags.tag_id
的外键,这也是错误的。对于“设备老化”的含义,我还不太清楚,但是在我看来,您正在尝试将两个单独的概念合并到一个表中,所有这些问题均来自此表。考虑改为将“设备”表(和实体)分为两个:
“核心”设备(由于缺少更好的名称)应包含真正唯一的属性,例如序列号。您的DevTags实体将与该实体一对多链接。
设备“版本”将包含适用于各个“版本”的属性。每个“核心”设备将有多个“版本”; “版本”每2周更新一次。
关于java - @OneToMany协会加入了错误的领域,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1574026/