Hibernate 多对多连接 table 未填充
Hibernate many-to-many join table not populating
我想使用联接 table 通过多对多关联关联两个实体 类 用户和单位。但是,这没有按预期工作。首先,我将简要地向您展示到目前为止我得到了什么。
数据库:
| User | | UserToUnit | | Unit |
|Id|login|...| |User_Id|Unit_Id| |Id|name|...|
实体用户:
@Entity
@Table(name = "\"User\"")
public class User implements Identifiable {
public User() {
units = new ArrayList<Unit>();
}
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "guid")
@Column(name = "Id", columnDefinition = "uniqueidentifier")
private String id;
@Transient
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "UserToUnit",
joinColumns = { @JoinColumn(name = "User_Id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "Unit_Id", nullable = false, updatable = false) }
)
private Collection<Unit> units;
[...]
public Collection<Unit> getUnits() {
return units;
}
public void setUnits(Collection<Unit> ous) {
units= ous;
}
}
实体单位:
@Entity
@Table(name = "Unit")
@XmlRootElement
public class Unit implements Identifiable {
public Unit() {
users = new ArrayList<User>();
}
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "guid")
@Column(name = "Id", columnDefinition = "uniqueidentifier")
private String id;
@Transient
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "units")
private Collection<User> users;
[...]
public Collection<User> getUsers() {
return users;
}
public void setUsers(Collection<User> us) {
users = us;
}
用法:
user.getUnits().add(unit);
unit.getUsers().add(user);
unitDAO.saveOrUpdate(unit);
userDAO.saveOrUpdate(user);
但这不会向 UserToUnit 添加任何内容 table。我在这里错过了什么?
干杯,
克里斯
我认为您应该在 User
class 和 Unit
class 中指定您的 ID,以匹配将在 [=13= 中加入的属性] 注释。
你能同时添加到 User
和 Unit
class 吗?
@Id
@Column(name = "Id")
private long id;
您使用 @Transient
注释的原因是什么?如果您想使用 属性 访问权限并且 getter 不在字段名称之后,您应该使用它。
您正在混合字段和 属性 访问,因此您还应该将 @Access(AccessType.PROPERTY)
添加到 getter 方法或将注释移动到该字段。
我想使用联接 table 通过多对多关联关联两个实体 类 用户和单位。但是,这没有按预期工作。首先,我将简要地向您展示到目前为止我得到了什么。
数据库:
| User | | UserToUnit | | Unit |
|Id|login|...| |User_Id|Unit_Id| |Id|name|...|
实体用户:
@Entity
@Table(name = "\"User\"")
public class User implements Identifiable {
public User() {
units = new ArrayList<Unit>();
}
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "guid")
@Column(name = "Id", columnDefinition = "uniqueidentifier")
private String id;
@Transient
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "UserToUnit",
joinColumns = { @JoinColumn(name = "User_Id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "Unit_Id", nullable = false, updatable = false) }
)
private Collection<Unit> units;
[...]
public Collection<Unit> getUnits() {
return units;
}
public void setUnits(Collection<Unit> ous) {
units= ous;
}
}
实体单位:
@Entity
@Table(name = "Unit")
@XmlRootElement
public class Unit implements Identifiable {
public Unit() {
users = new ArrayList<User>();
}
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "guid")
@Column(name = "Id", columnDefinition = "uniqueidentifier")
private String id;
@Transient
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "units")
private Collection<User> users;
[...]
public Collection<User> getUsers() {
return users;
}
public void setUsers(Collection<User> us) {
users = us;
}
用法:
user.getUnits().add(unit);
unit.getUsers().add(user);
unitDAO.saveOrUpdate(unit);
userDAO.saveOrUpdate(user);
但这不会向 UserToUnit 添加任何内容 table。我在这里错过了什么? 干杯, 克里斯
我认为您应该在 User
class 和 Unit
class 中指定您的 ID,以匹配将在 [=13= 中加入的属性] 注释。
你能同时添加到 User
和 Unit
class 吗?
@Id
@Column(name = "Id")
private long id;
您使用 @Transient
注释的原因是什么?如果您想使用 属性 访问权限并且 getter 不在字段名称之后,您应该使用它。
您正在混合字段和 属性 访问,因此您还应该将 @Access(AccessType.PROPERTY)
添加到 getter 方法或将注释移动到该字段。