Reputation: 642
I have a Set
of Long
values inside my entity class. I want this Set to store values for particular instances of this class. Here's my code:
@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
}
Persist code:
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);
}
When using the second constructor to persist a DefaultAccount
object, JPA does not pass values from managerAccounts
Set
to the database. What is the issue for this?
Upvotes: 2
Views: 2186
Reputation: 9102
Here is what I tried
@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);
}
My test code
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("[email protected]", "test", accounts);
em.persist(account);
em.getTransaction().commit();
em.close();
Here is the SQL sent to the database ( as expected 4 total inserts)
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
(?, ?)
Values persisted in database
mysql> select * from defaultaccounts;
+----+---------------+----------+
| id | email | password |
+----+---------------+----------+
| 1 | [email protected] | 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)
Upvotes: 1
Reputation: 3682
you need to set accounts to the instance's managerAccounts:
public DefaultAccount(String email, String password, Set<Long> accounts) {
this(email, password);
Assert.notEmpty(accounts);
this.managerAccounts=accounts;
}
should suffice.
Upvotes: 0