【发布时间】:2019-09-19 10:39:18
【问题描述】:
我需要支持涉及以下实体的场景(使用 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..
}
用户帐户 ID:
@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;
我不清楚标记为not null 的user_account_account_id 和user_account_user_id 列,据我所知,可以在没有帐户的情况下创建用户。
为什么要以这种方式创建表?我怎样才能让它发挥作用?
【问题讨论】:
标签: hibernate jpa orm spring-data-jpa