@Test(expected = DuplicateKeyException.class) public void saveNewCustomerWithDuplicateEmail() { Customer c = new Customer(); c.setFirstName("Bob"); c.setLastName("Doe"); c.setEmailAddress(new EmailAddress("bob@doe.com")); Address a = new Address("66 Main St", "Middletown", "USA"); c.addAddress(a); repository.save(c); }
@Override public void save(final Customer customer) { if (customer.getId() == null) { Long generatedKey = qdslTemplate.insertWithKey(qCustomer, new SqlInsertWithKeyCallback<Long>() { @Override public Long doInSqlInsertWithKeyClause(SQLInsertClause insert) throws SQLException { return insert .columns(qCustomer.firstName, qCustomer.lastName, qCustomer.emailAddress) .values(customer.getFirstName(), customer.getLastName(), customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()) .executeWithKey(qCustomer.id); } }); customer.setId(generatedKey); } else { qdslTemplate.update(qCustomer, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update .where(qCustomer.id.eq(customer.getId())) .set(qCustomer.firstName, customer.getFirstName()) .set(qCustomer.lastName, customer.getLastName()) .set(qCustomer.emailAddress, customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()).execute(); } }); } final List<Long> ids = new ArrayList<Long>(); for (Address a : customer.getAddresses()) { if (a.getId() != null) { ids.add(a.getId()); } } if (ids.size() > 0) { qdslTemplate.delete(qAddress, new SqlDeleteCallback() { @Override public long doInSqlDeleteClause(SQLDeleteClause delete) { return delete.where(qAddress.customerId.eq(customer.getId()).and(qAddress.id.notIn(ids))).execute(); } }); } for (final Address a : customer.getAddresses()) { if (a.getId() != null) { qdslTemplate.update(qAddress, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update.where(qAddress.id.eq(a.getId())).set(qAddress.customerId, customer.getId()) .set(qAddress.street, a.getStreet()).set(qAddress.city, a.getCity()) .set(qAddress.country, a.getCountry()).execute(); } }); } else { qdslTemplate.insert(qAddress, new SqlInsertCallback() { @Override public long doInSqlInsertClause(SQLInsertClause insert) { return insert.columns(qAddress.customerId, qAddress.street, qAddress.city, qAddress.country) .values(customer.getId(), a.getStreet(), a.getCity(), a.getCountry()).execute(); } }); } } }
QueryDslCustomerRepository implements CustomerRepository { @Override public void save(final Customer customer) { if (customer.getId() == null) { Long generatedKey = qdslTemplate.insertWithKey(qCustomer, new SqlInsertWithKeyCallback<Long>() { @Override public Long doInSqlInsertWithKeyClause(SQLInsertClause insert) throws SQLException { return insert .columns(qCustomer.firstName, qCustomer.lastName, qCustomer.emailAddress) .values(customer.getFirstName(), customer.getLastName(), customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()) .executeWithKey(qCustomer.id); } }); customer.setId(generatedKey); } else { qdslTemplate.update(qCustomer, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update .where(qCustomer.id.eq(customer.getId())) .set(qCustomer.firstName, customer.getFirstName()) .set(qCustomer.lastName, customer.getLastName()) .set(qCustomer.emailAddress, customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()).execute(); } }); } final List<Long> ids = new ArrayList<Long>(); for (Address a : customer.getAddresses()) { if (a.getId() != null) { ids.add(a.getId()); } } if (ids.size() > 0) { qdslTemplate.delete(qAddress, new SqlDeleteCallback() { @Override public long doInSqlDeleteClause(SQLDeleteClause delete) { return delete.where(qAddress.customerId.eq(customer.getId()).and(qAddress.id.notIn(ids))).execute(); } }); } for (final Address a : customer.getAddresses()) { if (a.getId() != null) { qdslTemplate.update(qAddress, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update.where(qAddress.id.eq(a.getId())).set(qAddress.customerId, customer.getId()) .set(qAddress.street, a.getStreet()).set(qAddress.city, a.getCity()) .set(qAddress.country, a.getCountry()).execute(); } }); } else { qdslTemplate.insert(qAddress, new SqlInsertCallback() { @Override public long doInSqlInsertClause(SQLInsertClause insert) { return insert.columns(qAddress.customerId, qAddress.street, qAddress.city, qAddress.country) .values(customer.getId(), a.getStreet(), a.getCity(), a.getCountry()).execute(); } }); } } } }
QueryDslCustomerRepository implements CustomerRepository { @Override public void save(final Customer customer) { if (customer.getId() == null) { Long generatedKey = qdslTemplate.insertWithKey(qCustomer, new SqlInsertWithKeyCallback<Long>() { @Override public Long doInSqlInsertWithKeyClause(SQLInsertClause insert) throws SQLException { return insert .columns(qCustomer.firstName, qCustomer.lastName, qCustomer.emailAddress) .values(customer.getFirstName(), customer.getLastName(), customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()) .executeWithKey(qCustomer.id); } }); customer.setId(generatedKey); } else { qdslTemplate.update(qCustomer, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update .where(qCustomer.id.eq(customer.getId())) .set(qCustomer.firstName, customer.getFirstName()) .set(qCustomer.lastName, customer.getLastName()) .set(qCustomer.emailAddress, customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()).execute(); } }); } final List<Long> ids = new ArrayList<Long>(); for (Address a : customer.getAddresses()) { if (a.getId() != null) { ids.add(a.getId()); } } if (ids.size() > 0) { qdslTemplate.delete(qAddress, new SqlDeleteCallback() { @Override public long doInSqlDeleteClause(SQLDeleteClause delete) { return delete.where(qAddress.customerId.eq(customer.getId()).and(qAddress.id.notIn(ids))).execute(); } }); } for (final Address a : customer.getAddresses()) { if (a.getId() != null) { qdslTemplate.update(qAddress, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update.where(qAddress.id.eq(a.getId())).set(qAddress.customerId, customer.getId()) .set(qAddress.street, a.getStreet()).set(qAddress.city, a.getCity()) .set(qAddress.country, a.getCountry()).execute(); } }); } else { qdslTemplate.insert(qAddress, new SqlInsertCallback() { @Override public long doInSqlInsertClause(SQLInsertClause insert) { return insert.columns(qAddress.customerId, qAddress.street, qAddress.city, qAddress.country) .values(customer.getId(), a.getStreet(), a.getCity(), a.getCountry()).execute(); } }); } } }  }
QueryDslCustomerRepository implements CustomerRepository { @Override public void save(final Customer customer) { if (customer.getId() == null) { Long generatedKey = qdslTemplate.insertWithKey(qCustomer, new SqlInsertWithKeyCallback<Long>() { @Override public Long doInSqlInsertWithKeyClause(SQLInsertClause insert) throws SQLException { return insert .columns(qCustomer.firstName, qCustomer.lastName, qCustomer.emailAddress) .values(customer.getFirstName(), customer.getLastName(), customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()) .executeWithKey(qCustomer.id); } }); customer.setId(generatedKey); } else { qdslTemplate.update(qCustomer, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update .where(qCustomer.id.eq(customer.getId())) .set(qCustomer.firstName, customer.getFirstName()) .set(qCustomer.lastName, customer.getLastName()) .set(qCustomer.emailAddress, customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()).execute(); } }); } final List<Long> ids = new ArrayList<Long>(); for (Address a : customer.getAddresses()) { if (a.getId() != null) { ids.add(a.getId()); } } if (ids.size() > 0) { qdslTemplate.delete(qAddress, new SqlDeleteCallback() { @Override public long doInSqlDeleteClause(SQLDeleteClause delete) { return delete.where(qAddress.customerId.eq(customer.getId()).and(qAddress.id.notIn(ids))).execute(); } }); } for (final Address a : customer.getAddresses()) { if (a.getId() != null) { qdslTemplate.update(qAddress, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update.where(qAddress.id.eq(a.getId())).set(qAddress.customerId, customer.getId()) .set(qAddress.street, a.getStreet()).set(qAddress.city, a.getCity()) .set(qAddress.country, a.getCountry()).execute(); } }); } else { qdslTemplate.insert(qAddress, new SqlInsertCallback() { @Override public long doInSqlInsertClause(SQLInsertClause insert) { return insert.columns(qAddress.customerId, qAddress.street, qAddress.city, qAddress.country) .values(customer.getId(), a.getStreet(), a.getCity(), a.getCountry()).execute(); } }); } } }  @Autowired void setDataSource(DataSource dataSource); @Override @Transactional(readOnly = true) Customer findById(Long id); @Override @Transactional(readOnly = true) List<Customer> findAll(); @Override @Transactional(readOnly = true) Customer findByEmailAddress(EmailAddress emailAddress); @Override void save(final Customer customer); @Override void delete(final Customer customer); }
QueryDslCustomerRepository implements CustomerRepository { @Override public void save(final Customer customer) { if (customer.getId() == null) { Long generatedKey = qdslTemplate.insertWithKey(qCustomer, new SqlInsertWithKeyCallback<Long>() { @Override public Long doInSqlInsertWithKeyClause(SQLInsertClause insert) throws SQLException { return insert .columns(qCustomer.firstName, qCustomer.lastName, qCustomer.emailAddress) .values(customer.getFirstName(), customer.getLastName(), customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()) .executeWithKey(qCustomer.id); } }); customer.setId(generatedKey); } else { qdslTemplate.update(qCustomer, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update .where(qCustomer.id.eq(customer.getId())) .set(qCustomer.firstName, customer.getFirstName()) .set(qCustomer.lastName, customer.getLastName()) .set(qCustomer.emailAddress, customer.getEmailAddress() == null ? null : customer.getEmailAddress().toString()).execute(); } }); } final List<Long> ids = new ArrayList<Long>(); for (Address a : customer.getAddresses()) { if (a.getId() != null) { ids.add(a.getId()); } } if (ids.size() > 0) { qdslTemplate.delete(qAddress, new SqlDeleteCallback() { @Override public long doInSqlDeleteClause(SQLDeleteClause delete) { return delete.where(qAddress.customerId.eq(customer.getId()).and(qAddress.id.notIn(ids))).execute(); } }); } for (final Address a : customer.getAddresses()) { if (a.getId() != null) { qdslTemplate.update(qAddress, new SqlUpdateCallback() { @Override public long doInSqlUpdateClause(SQLUpdateClause update) { return update.where(qAddress.id.eq(a.getId())).set(qAddress.customerId, customer.getId()) .set(qAddress.street, a.getStreet()).set(qAddress.city, a.getCity()) .set(qAddress.country, a.getCountry()).execute(); } }); } else { qdslTemplate.insert(qAddress, new SqlInsertCallback() { @Override public long doInSqlInsertClause(SQLInsertClause insert) { return insert.columns(qAddress.customerId, qAddress.street, qAddress.city, qAddress.country) .values(customer.getId(), a.getStreet(), a.getCity(), a.getCountry()).execute(); } }); } } }  @Autowired void setDataSource(DataSource dataSource); @Override @Transactional(readOnly = true) Customer findById(Long id); @Override @Transactional(readOnly = true) List<Customer> findAll(); @Override @Transactional(readOnly = true) Customer findByEmailAddress(EmailAddress emailAddress); @Override void save(final Customer customer); @Override void delete(final Customer customer);  }