@OneToMany协会加入了错误的领域

时间:2009-10-15 17:54:57

标签: java hibernate

我有2个表,设备包含设备列表和dev_tags,其中包含这些设备的资产标签列表。表连接dev_serial_num,这是两个表的主键。这些设备在其ip_address字段中是唯一的,并且它们具有由dev_id标识的主键。这些设备在2周后“老化”。因此,同一件硬件可以在设备中出现不止一次。

我提到这个解释为什么dev_tags和设备之间存在OneToMany关系,而这似乎应该是OneToOne关系。

所以我有我的2个实体

@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的mappedBy属性搞砸了很多,我无法做到这一点。我终于得到了编译的麻烦,但查询仍在尝试将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个表,或者至少有一个必须是主键吗?

1 个答案:

答案 0 :(得分:1)

简短的回答是“不,你不能在任何领域加入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的外键,这也是错误的。

我对“设备老化”的含义并不十分清楚,但在我看来,你试图将两个独立的概念合并到一个表中,这就是所有这些问题的起源所在。请考虑将“设备”表(和实体)分成两部分:

  1. “核心”设备(缺少更好的名称)应包含真正独特的属性,如序列号。您的DevTags实体将作为一对多链接到此实体。
  2. 设备“版本”将包含适用于个别“版本”的属性。每个“核心”设备将有多个“版本”; “版本”将每两周更新一次。