BAEL-5370
removing compound index related stuff
This commit is contained in:
parent
a142cc0a1a
commit
793ada0100
@ -2,11 +2,9 @@ package com.baeldung.boot.composite.key;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource("classpath:boot.composite.key/app.properties")
|
||||
@EnableMongoRepositories(basePackages = { "com.baeldung.boot.composite.key" })
|
||||
public class SpringBootCompositeKeyApplication {
|
||||
public static void main(String... args) {
|
||||
|
@ -1,12 +0,0 @@
|
||||
package com.baeldung.boot.composite.key.dao;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import com.baeldung.boot.composite.key.data.Customer;
|
||||
|
||||
public interface CustomerRepository extends MongoRepository<Customer, String> {
|
||||
|
||||
Optional<Customer> findByStoreIdAndNumber(Long storeId, Long number);
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package com.baeldung.boot.composite.key.dao;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import com.baeldung.boot.composite.key.data.Sale;
|
||||
import com.baeldung.boot.composite.key.data.TicketId;
|
||||
|
||||
public interface SaleRepository extends MongoRepository<Sale, String> {
|
||||
Optional<Sale> findByTicketId(TicketId ticketId);
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package com.baeldung.boot.composite.key.data;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.index.CompoundIndex;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document
|
||||
@CompoundIndex(name = "customer_idx", def = "{ 'storeId': 1, 'number': 1 }", unique = true)
|
||||
public class Customer {
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private Long storeId;
|
||||
private Long number;
|
||||
|
||||
private String name;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getStoreId() {
|
||||
return storeId;
|
||||
}
|
||||
|
||||
public void setStoreId(Long storeId) {
|
||||
this.storeId = storeId;
|
||||
}
|
||||
|
||||
public Long getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(Long number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package com.baeldung.boot.composite.key.data;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.index.CompoundIndex;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document
|
||||
@CompoundIndex(name = "sale_idx", def = "{ 'ticketId': 1 }", unique = true)
|
||||
public class Sale {
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private TicketId ticketId;
|
||||
private Double value;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public TicketId getTicketId() {
|
||||
return ticketId;
|
||||
}
|
||||
|
||||
public void setTicketId(TicketId ticketId) {
|
||||
this.ticketId = ticketId;
|
||||
}
|
||||
|
||||
public Double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Double value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
@ -5,25 +5,15 @@ import java.util.Optional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.boot.composite.key.dao.CustomerRepository;
|
||||
import com.baeldung.boot.composite.key.dao.SaleRepository;
|
||||
import com.baeldung.boot.composite.key.dao.TicketRepository;
|
||||
import com.baeldung.boot.composite.key.data.Customer;
|
||||
import com.baeldung.boot.composite.key.data.Sale;
|
||||
import com.baeldung.boot.composite.key.data.Ticket;
|
||||
import com.baeldung.boot.composite.key.data.TicketId;
|
||||
|
||||
@Service
|
||||
public class CustomerService {
|
||||
@Autowired
|
||||
private CustomerRepository customerRepository;
|
||||
|
||||
@Autowired
|
||||
private TicketRepository ticketRepository;
|
||||
|
||||
@Autowired
|
||||
private SaleRepository saleRepository;
|
||||
|
||||
public Optional<Ticket> find(TicketId id) {
|
||||
return ticketRepository.findById(id);
|
||||
}
|
||||
@ -35,32 +25,4 @@ public class CustomerService {
|
||||
public Ticket save(Ticket ticket) {
|
||||
return ticketRepository.save(ticket);
|
||||
}
|
||||
|
||||
public Optional<Customer> findCustomerById(String id) {
|
||||
return customerRepository.findById(id);
|
||||
}
|
||||
|
||||
public Optional<Customer> findCustomerByIndex(Long storeId, Long number) {
|
||||
return customerRepository.findByStoreIdAndNumber(storeId, number);
|
||||
}
|
||||
|
||||
public Customer insert(Customer customer) {
|
||||
return customerRepository.insert(customer);
|
||||
}
|
||||
|
||||
public Customer save(Customer customer) {
|
||||
return customerRepository.save(customer);
|
||||
}
|
||||
|
||||
public Sale insert(Sale sale) {
|
||||
return saleRepository.insert(sale);
|
||||
}
|
||||
|
||||
public Optional<Sale> findSaleByTicketId(TicketId ticketId) {
|
||||
return saleRepository.findByTicketId(ticketId);
|
||||
}
|
||||
|
||||
public Optional<Sale> findSaleById(String id) {
|
||||
return saleRepository.findById(id);
|
||||
}
|
||||
}
|
||||
|
@ -4,15 +4,12 @@ import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.boot.composite.key.data.Customer;
|
||||
import com.baeldung.boot.composite.key.data.Sale;
|
||||
import com.baeldung.boot.composite.key.data.Ticket;
|
||||
import com.baeldung.boot.composite.key.data.TicketId;
|
||||
import com.baeldung.boot.composite.key.service.CustomerService;
|
||||
@ -37,39 +34,4 @@ public class CustomerController {
|
||||
public Ticket putTicket(@RequestBody Ticket ticket) {
|
||||
return customerService.save(ticket);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Optional<Customer> getCustomer(@PathVariable String id) {
|
||||
return customerService.findCustomerById(id);
|
||||
}
|
||||
|
||||
@GetMapping("/{storeId}/{number}")
|
||||
public Optional<Customer> getCustomerByIndex(@PathVariable Long storeId, @PathVariable Long number) {
|
||||
return customerService.findCustomerByIndex(storeId, number);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Customer postCustomer(@RequestBody Customer customer) {
|
||||
return customerService.insert(customer);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Customer putCustomer(@RequestBody Customer customer) {
|
||||
return customerService.save(customer);
|
||||
}
|
||||
|
||||
@PostMapping("/sale")
|
||||
public Sale postSale(@RequestBody Sale sale) {
|
||||
return customerService.insert(sale);
|
||||
}
|
||||
|
||||
@GetMapping("/sale/{id}")
|
||||
public Optional<Sale> getSale(@PathVariable String id) {
|
||||
return customerService.findSaleById(id);
|
||||
}
|
||||
|
||||
@GetMapping("/sale")
|
||||
public Optional<Sale> getSale(TicketId ticketId) {
|
||||
return customerService.findSaleByTicketId(ticketId);
|
||||
}
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
spring.data.mongodb.auto-index-creation=true
|
@ -14,7 +14,6 @@ import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.composite.key.data.Customer;
|
||||
import com.baeldung.boot.composite.key.data.Ticket;
|
||||
import com.baeldung.boot.composite.key.data.TicketId;
|
||||
import com.baeldung.boot.composite.key.service.CustomerService;
|
||||
@ -54,20 +53,6 @@ public class CustomerServiceIntegrationTest {
|
||||
assertEquals(savedTicket.getId(), ticketId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompoundUniqueIndex_whenSearchingByGeneratedId_thenFound() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("Name");
|
||||
customer.setNumber(0l);
|
||||
customer.setStoreId(0l);
|
||||
|
||||
Customer savedCustomer = service.insert(customer);
|
||||
|
||||
Optional<Customer> optional = service.findCustomerById(savedCustomer.getId());
|
||||
|
||||
assertThat(optional.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompositeId_whenDupeInsert_thenExceptionIsThrown() {
|
||||
TicketId ticketId = new TicketId();
|
||||
@ -96,35 +81,4 @@ public class CustomerServiceIntegrationTest {
|
||||
|
||||
assertEquals(savedTicket.getEvent(), ticketB.getEvent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompoundUniqueIndex_whenDupeInsert_thenExceptionIsThrown() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("Name");
|
||||
customer.setNumber(1l);
|
||||
customer.setStoreId(2l);
|
||||
|
||||
assertThrows(DuplicateKeyException.class, () -> {
|
||||
service.insert(customer);
|
||||
service.insert(customer);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompoundUniqueIndex_whenDupeSave_thenExceptionIsThrown() {
|
||||
Customer customerA = new Customer();
|
||||
customerA.setName("Name A");
|
||||
customerA.setNumber(1l);
|
||||
customerA.setStoreId(2l);
|
||||
|
||||
Customer customerB = new Customer();
|
||||
customerB.setName("Name B");
|
||||
customerB.setNumber(1l);
|
||||
customerB.setStoreId(2l);
|
||||
|
||||
assertThrows(DuplicateKeyException.class, () -> {
|
||||
service.save(customerA);
|
||||
service.save(customerB);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user