我有两个实体类Activity
和User
。他们之间的关系是:
Activity
可以有一个或多个User
User
可以属于一个或多个Activity
为了实现这一点,我在它们之间定义了@ManyToMany
映射。以下是我的课程:
Activity
:
@javax.persistence.Entity
@Table(name = "ACTIVITY")
public class Activity extends Entity {
@Transient
private static final long serialVersionUID = 4741665931936809028L;
private Set<User> users;
public Activity() {
super();
}
@ManyToMany(targetEntity = User.class, cascade = { CascadeType.ALL })
@JoinTable(name = "ACTIVITY_USER", joinColumns = @JoinColumn(name = "ID"), inverseJoinColumns = @JoinColumn(name = "ID"))
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
User
:
@javax.persistence.Entity
@Table(name = "USER")
public class User extends Entity {
@Transient
private static final long serialVersionUID = -112950002831333869L;
private Set<Activity> activities;
public User() {
super();
}
@ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "users", targetEntity = Activity.class)
public Set<Activity> getActivities() {
return activities;
}
public void setActivities(Set<Activity> activities) {
this.activities = activities;
}
}
上述两个类都扩展了Entity
:
@MappedSuperclass
public class Entity implements Serializable {
@Transient
private static final long serialVersionUID = 7470288121057059283L;
private Long id;
public Entity() {
super();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", updatable = false, nullable = false, unique = true)
public Long getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(Long id) {
this.id = id;
}
public void setLastUpdateTimestamp(Date lastUpdateTimestamp) {
this.lastUpdateTimestamp = lastUpdateTimestamp;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
return prime * result + ((getId() == null) ? super.hashCode() : getId().hashCode());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!getClass().equals(HibernateProxyHelper.getClassWithoutInitializingProxy(obj))) {
return false;
}
final Entity other = (Entity) obj;
if (getId() != other.getId()) {
if (getId() == null) {
return false;
}
if (!getId().equals(other.getId())) {
return false;
}
}
return true;
}
}
现在来自:
@JoinTable(name = "ACTIVITY_USER", joinColumns = @JoinColumn(name = "ID"), inverseJoinColumns = @JoinColumn(name = "ID"))
据我所知,@JoinColumn
joinColumns
的名称取一个实体的PK列名,而@JoinColumn
的{{1}}名称取名为另一个实体的PK的列名。但在我的情况下,两个列名都相同,即来自inverseJoinColumns
类的ID
。
我的问题是如何避免列名冲突?任何建议都对我很有帮助。
答案 0 :(得分:4)
在同一个表中有两个具有相同名称的列是没有意义的。所以只需为列选择一个不同的名称。
更改
@JoinTable(name = "ACTIVITY_USER",
joinColumns = @JoinColumn(name = "ID"),
inverseJoinColumns = @JoinColumn(name = "ID"))
到
@JoinTable(name = "ACTIVITY_USER",
joinColumns = @JoinColumn(name = "ACTIVITY_ID"),
inverseJoinColumns = @JoinColumn(name = "USER_ID"))
如该属性的名称所示,您提供的名称是连接列的名称,连接表的一部分,即列的名称将外键保存到活动或用户表中,在连接表ACTIVITY_USER中。 Hibernate已经知道用户和活动PK的名称:你在各自的@Id
注释中告诉它。