Search in sources :

Example 1 with CustomerAddress

use of com.querydsl.example.dto.CustomerAddress in project querydsl by querydsl.

the class CustomerDaoImpl method save.

@Override
public Customer save(Customer c) {
    Long id = c.getId();
    if (id == null) {
        id = queryFactory.insert(customer).set(customer.name, c.getName()).set(customer.contactPersonId, c.getContactPerson().getId()).executeWithKey(customer.id);
        c.setId(id);
    } else {
        queryFactory.update(customer).set(customer.name, c.getName()).set(customer.contactPersonId, c.getContactPerson().getId()).where(customer.id.eq(c.getId())).execute();
        // delete address rows
        queryFactory.delete(customerAddress).where(customerAddress.customerId.eq(id)).execute();
    }
    SQLInsertClause insert = queryFactory.insert(customerAddress);
    for (CustomerAddress ca : c.getAddresses()) {
        if (ca.getAddress().getId() == null) {
            ca.getAddress().setId(queryFactory.insert(address).populate(ca.getAddress()).executeWithKey(address.id));
        }
        insert.set(customerAddress.customerId, id).set(customerAddress.addressId, ca.getAddress().getId()).set(customerAddress.addressTypeCode, ca.getAddressTypeCode()).set(customerAddress.fromDate, ca.getFromDate()).set(customerAddress.toDate, ca.getToDate()).addBatch();
    }
    insert.execute();
    c.setId(id);
    return c;
}
Also used : SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) CustomerAddress(com.querydsl.example.dto.CustomerAddress)

Aggregations

CustomerAddress (com.querydsl.example.dto.CustomerAddress)1 SQLInsertClause (com.querydsl.sql.dml.SQLInsertClause)1