我的实体类中有Set
个Long
个值。我希望此Set存储此类的特定实例的值。这是我的代码:
@Entity
@Table(name = "accounts")
public class DefaultAccount implements Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private Long id;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "password", nullable = false)
private String password;
@ElementCollection
private Set<Long> managerAccounts = new HashSet<Long>();
public DefaultAccount(String email, String password) {
Assert.hasText(email);
Assert.hasText(password);
this.email = email;
this.password = password;
}
public DefaultAccount(String email, String password, Set<Long> accounts) {
this(email, password);
Assert.notEmpty(accounts);
this.managerAccounts.addAll(accounts);
}
// Getters
}
坚持代码:
private EntityManager entityManager;
@PersistenceContext(unitName = "crm-db")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public EntityManager getEntityManager() {
return entityManager;
}
public void createAccount(Account account) {
entityManager.persist(account);
}
使用第二个构造函数来持久化DefaultAccount
对象时,JPA不会将managerAccounts
Set
中的值传递给数据库。这有什么问题?
答案 0 :(得分:1)
这是我试过的
@Entity
@Table(name = "defaultaccounts")
public class DefaultAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private Long id;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "password", nullable = false)
private String password;
@ElementCollection
private Set<Long> managerAccounts = new HashSet<Long>();
public DefaultAccount(String email, String password) {
this.email = email;
this.password = password;
}
public DefaultAccount(String email, String password, Set<Long> accounts) {
this(email, password);
this.managerAccounts.addAll(accounts);
}
我的测试代码
EntityManager em = // get entity manager from entity manager factory
em.getTransaction().begin();
HashSet accounts = new HashSet();
accounts.add(new Long(10000));
accounts.add(new Long(10001));
accounts.add(new Long(10002));
DefaultAccount account = new DefaultAccount("test@test.com", "test", accounts);
em.persist(account);
em.getTransaction().commit();
em.close();
以下是发送到数据库的SQL(预期 4个总插入数)
insert
into
defaultaccounts
(email, password)
values
(?, ?)
insert
into
default_account_manager_acc
(default_account, manager_accounts)
values
(?, ?)
insert
into
default_account_manager_acc
(default_account, manager_accounts)
values
(?, ?)
insert
into
default_account_manager_acc
(default_account, manager_accounts)
values
(?, ?)
值持久存储在数据库
中mysql> select * from defaultaccounts;
+----+---------------+----------+
| id | email | password |
+----+---------------+----------+
| 1 | test@test.com | test |
+----+---------------+----------+
1 row in set (0.00 sec)
mysql> select * from default_account_manager_acc;
+-----------------+------------------+
| default_account | manager_accounts |
+-----------------+------------------+
| 1 | 10001 |
| 1 | 10000 |
| 1 | 10002 |
+-----------------+------------------+
3 rows in set (0.00 sec)
答案 1 :(得分:0)
您需要将帐户设置为实例的managerAccounts:
public DefaultAccount(String email, String password, Set<Long> accounts) {
this(email, password);
Assert.notEmpty(accounts);
this.managerAccounts=accounts;
}
应该足够了。