Skip to content

Commit a787ea3

Browse files
authored
BAEL-5394 - Unique Field in MongoDB Document in Spring Data (eugenp#12384)
* BAEL-5370 - MongoDB Composite Key First Draft. * removing comments * BAEL-5370 Test could fail if ran in a different order: givenCompositeId_whenSearchingByIdObject_thenFound * BAEL-5370 removing compound index related stuff * removing first insert from assertThrows * first draft * removing Customer2
1 parent f1bbcec commit a787ea3

File tree

13 files changed

+437
-0
lines changed

13 files changed

+437
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.boot.unique.field;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.PropertySource;
6+
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
7+
8+
@SpringBootApplication
9+
@PropertySource("classpath:boot.unique.field/app.properties")
10+
@EnableMongoRepositories(basePackages = { "com.baeldung.boot.unique.field" })
11+
public class SpringBootUniqueFieldApplication {
12+
public static void main(String... args) {
13+
SpringApplication.run(SpringBootUniqueFieldApplication.class, args);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.boot.unique.field.dao;
2+
3+
import org.springframework.data.mongodb.repository.MongoRepository;
4+
5+
import com.baeldung.boot.unique.field.data.Asset;
6+
7+
public interface AssetRepository extends MongoRepository<Asset, String> {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.boot.unique.field.dao;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.data.mongodb.repository.MongoRepository;
6+
7+
import com.baeldung.boot.unique.field.data.Company;
8+
9+
public interface CompanyRepository extends MongoRepository<Company, String> {
10+
Optional<Company> findByEmail(String email);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.boot.unique.field.dao;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.data.mongodb.repository.MongoRepository;
6+
7+
import com.baeldung.boot.unique.field.data.Customer;
8+
9+
public interface CustomerRepository extends MongoRepository<Customer, String> {
10+
Optional<Customer> findByStoreIdAndNumber(Long storeId, Long number);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.boot.unique.field.dao;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.data.mongodb.repository.MongoRepository;
6+
7+
import com.baeldung.boot.unique.field.data.Sale;
8+
import com.baeldung.boot.unique.field.data.SaleId;
9+
10+
public interface SaleRepository extends MongoRepository<Sale, String> {
11+
Optional<Sale> findBySaleId(SaleId id);
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.boot.unique.field.data;
2+
3+
import org.springframework.data.mongodb.core.index.Indexed;
4+
import org.springframework.data.mongodb.core.mapping.Document;
5+
6+
@Document
7+
public class Asset {
8+
@Indexed(unique = true)
9+
private String name;
10+
11+
@Indexed(unique = true)
12+
private Integer number;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public Integer getNumber() {
23+
return number;
24+
}
25+
26+
public void setNumber(Integer number) {
27+
this.number = number;
28+
}
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.boot.unique.field.data;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.index.Indexed;
5+
import org.springframework.data.mongodb.core.mapping.Document;
6+
7+
@Document
8+
public class Company {
9+
@Id
10+
private String id;
11+
12+
private String name;
13+
14+
@Indexed(unique = true)
15+
private String email;
16+
17+
public String getId() {
18+
return id;
19+
}
20+
21+
public void setId(String id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getEmail() {
34+
return email;
35+
}
36+
37+
public void setEmail(String email) {
38+
this.email = email;
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.boot.unique.field.data;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.index.CompoundIndex;
5+
import org.springframework.data.mongodb.core.mapping.Document;
6+
7+
@Document
8+
@CompoundIndex(name = "customer_idx", def = "{ 'storeId': 1, 'number': 1 }", unique = true)
9+
public class Customer {
10+
@Id
11+
private String id;
12+
13+
private Long storeId;
14+
15+
private Long number;
16+
17+
private String name;
18+
19+
public Customer() {
20+
}
21+
22+
public Customer(String name) {
23+
this.name = name;
24+
}
25+
26+
public String getId() {
27+
return id;
28+
}
29+
30+
public void setId(String id) {
31+
this.id = id;
32+
}
33+
34+
public Long getStoreId() {
35+
return storeId;
36+
}
37+
38+
public void setStoreId(Long storeId) {
39+
this.storeId = storeId;
40+
}
41+
42+
public Long getNumber() {
43+
return number;
44+
}
45+
46+
public void setNumber(Long number) {
47+
this.number = number;
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public void setName(String name) {
55+
this.name = name;
56+
}
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.boot.unique.field.data;
2+
3+
import org.springframework.data.mongodb.core.index.Indexed;
4+
import org.springframework.data.mongodb.core.mapping.Document;
5+
6+
@Document
7+
public class Sale {
8+
@Indexed(unique = true)
9+
private SaleId saleId;
10+
11+
private Double value;
12+
13+
public Sale() {
14+
}
15+
16+
public Sale(SaleId saleId) {
17+
super();
18+
this.saleId = saleId;
19+
}
20+
21+
public SaleId getSaleId() {
22+
return saleId;
23+
}
24+
25+
public void setSaleId(SaleId saleId) {
26+
this.saleId = saleId;
27+
}
28+
29+
public Double getValue() {
30+
return value;
31+
}
32+
33+
public void setValue(Double value) {
34+
this.value = value;
35+
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.boot.unique.field.data;
2+
3+
public class SaleId {
4+
private Long item;
5+
private String date;
6+
7+
public Long getItem() {
8+
return item;
9+
}
10+
11+
public void setItem(Long item) {
12+
this.item = item;
13+
}
14+
15+
public String getDate() {
16+
return date;
17+
}
18+
19+
public void setDate(String date) {
20+
this.date = date;
21+
}
22+
}

0 commit comments

Comments
 (0)