我需要支持涉及以下实体(使用JPA)的方案:
一个用户可以有多个帐户,并且一个帐户可以在多个用户之间共享,这是到目前为止的标准@ManyToMany关系。
每个帐户的用户可以拥有一组不同的角色,一个角色可以在多个用户之间共享。
我遵循了this practice,它解释了一种映射带有多列的多对多关联的方法,但我不确定我是否理解。
用户实体:
@Entity
@Table(name = "users")
public class User implements Serializable {
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "full_name", nullable = false)
private String fullName;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<UserAccount> usersAccounts;
public User() {
usersAccounts= Sets.newHashSet();
}
public User(String email, String fullName) {
this();
this.email = email;
this.fullName = fullName;
}
public void addAccont(Account account) {
UserAccount userAccount = new UserAccount(this, account);
accounts.add(userAccount);
account.getUsers().add(userAccount);
this.accounts.add(userAccount);
}
public void removeAccont(Account account) {
for (Iterator<UserAccount> iterator = accounts.iterator(); iterator.hasNext(); ) {
UserAccount userAccount = iterator.next();
if (userAccount.getUser().equals(this) &&
userAccount.getAccount().equals(account)) {
iterator.remove();
userAccount.getAccount().getUsers().remove(userAccount);
userAccount.setUser(null);
userAccount.setAccount(null);
}
}
}
//Getters Setters..
}
帐户实体:
@Entity
@Table(name = "accounts")
public class Account implements Serializable {
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "billing_address", nullable = false)
private String billingAddress;
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<UserAccount> usersAccounts;
public Account() {
usersAccounts= Sets.newHashSet();
}
//Getters Setters..
}
UserAccount实体:
@Entity
@Table(name = "users_accounts")
public class UserAccount implements Serializable {
@EmbeddedId
private UserAccountId id;
@ManyToOne(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
private User user;
@ManyToOne(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
private Account account;
@ManyToMany(
fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(
name = "users_accounts_roles",
joinColumns = {
@JoinColumn(name = "user_id", referencedColumnName = "user_id"),
@JoinColumn(name = "account_id", referencedColumnName = "account_id"),
},
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
private Set<Role> roles;
private UserAccount() {}
public UserAccount(@NotNull User user, @NotNull Account account) {
this.user = user;
this.account = account;
roles = Sets.newHashSet();
this.id = new UserAccountId(user.getId(), account.getId());
}
//Getters Setters..
}
UserAccountId:
@Embeddable
public class UserAccountId implements Serializable {
@Column(name = "user_id")
private String userId;
@Column(name = "account_id")
private String accountId;
private UserAccountId() {
}
public UserAccountId(
String userId,
String accountId) {
this.userId = userId;
this.accountId = accountId;
}
//Getters Setters..
}
我正在创建一个新用户,并尝试将其保存到数据库:
User user = new User("some.email@mail.com", "John Doe");
userRepository.save(savedEntity);
我收到JpaSystemException:
Caused by: java.sql.SQLException: Field 'user_account_account_id' doesn't have a default value
我看过hibernate的create table语句:
create table users
(
id varchar(255) not null
primary key,
email varchar(255) not null,
full_name varchar(255) not null,
user_account_account_id varchar(255) not null,
user_account_user_id varchar(255) not null,
constraint UK_exxyrhm7e34pwn8dvem8wxuxu
unique (user_account_account_id, user_account_user_id),
constraint FKhti2663qxk7qo15f7gfnnaj7r
foreign key (user_account_account_id, user_account_user_id) references users_accounts (account_id, user_id)
)
engine = InnoDB;
我不清楚标记为user_account_account_id
的{{1}}和user_account_user_id
列,据我了解,在我的实体中,无需帐户即可创建用户。< / p>
为什么以这种方式创建表?我该如何运作?
答案 0 :(得分:1)
这是正确的吗?
您的解决方案将起作用,只是听起来很复杂。您确定多对多关联不应该在Role
和Account
之间(而不是UserAccount
之间)吗?换句话说,Account
会根据使用哪个Roles
而具有不同的User
?
此外,我在您建议的映射中发现了一个问题。您可能想要:
@JoinTable(
name = "users_accounts_roles",
joinColumns = {
@JoinColumn(name = "user_id", referencedColumnName = "user_id"),
@JoinColumn(name = "account_id", referencedColumnName = "account_id"),
},
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
(因为id
中没有要引用的UserAccount
列;而是有两个主键列)
答案 1 :(得分:0)
我的感觉是user_account_account_id
和user_account_user_id
列都是您先前尝试的遗留物。您可以安全地删除它们,然后一切正常。
为了安全起见,您可以完全删除“用户”表,然后从头开始重新创建它。