Skip to content

Commit 2be225b

Browse files
j7-juarezmaibin
authored andcommitted
added jpa embeddable example (eugenp#6636)
* [BAEL-12731] - Fixed tests in spring-boot-security module * [BAEL-12731] - Added BcryptPasswordEncoder support * first commit * [BAEL-13322] - Removed noexception module : Code already present in libraries module * added jpa embeddable example * Issue-6604: Upgrade spring cloud to Edgware.SR5 * BAEL-2569 EnvironmentPostProcessor in Spring Boot (eugenp#6608) * BAEL-2569 : EnvironmentPostProcessor in Spring Boot * BAEL-2569 add test * BAEL-2569 update test * BAEL-2569 refactoring the class PriceCalculationEnvironmentPostProcessor * BAEL-2569: changes to class PriceCalculationEnvironmentPostProcessor * BAEL-2477 * BAEL-2829: Moving to spring-data-jpa-2 * BAEL-379 A Guide to jBPM with Java (eugenp#6619) * BAEL-379 A Guide to jBPM with Java * BAEL-379 * BAEL-379 A Guide to jBPM with Java * BAEL-379 A Guide to jBPM with Java * BAEL-379 A Guide to jBPM with Java * BAEL-379 A Guide to jBPM with Java * BAEL-1959 Classpath contains multiple bindings Updated pom.xml to exclude log4j binding * Fixed or added Context Integration Tests for modules named in ticket * extension function in java * BAEL-2803 (eugenp#6640) * added sample code for eval article * BAEL-2803 Sample Code * updated tests, repo method names * moved sources for BAEL-2803 here * removed dependencies introduced by BAEL-2803 * re-added sources * moved code to new module * removed eval article code * BAEL-2736: Update README (eugenp#6651) * BAEL-2246: add link back to article * BAEL-2174: rename core-java-net module to core-java-networking * BAEL-2174: add link back to article * BAEL-2363 BAEL-2337 BAEL-1996 BAEL-2277 add links back to articles * BAEL-2367: add link back to article * BAEL-2335: add link back to article * BAEL-2413: add link back to article * Update README.MD * BAEL-2577: add link back to article * BAEL-2490: add link back to article * BAEL-2471: add link back to article * BAEL-2583: add link back to article * BAEL-2738: add link back to article * BAEL-2711: Add spring-boot-angular module to root pom * BAEL-2544 BAEL-2711 BAEL-2575 BAEL-2657 Add links back to articles * BAEL-2736: Add link back to article * BAEL * BAEL-2844 (eugenp#6641) * Revering tree update (eugenp#6561) * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * corrections * BAEL-2481: Moved to spring-mvc-java * BAEL-2820 Transforming Optional of an Empty String into Empty Optional (eugenp#6634) * [BAEL-13311] - Checked modules that don't use our standard parents * BAEL2489:Avoid check for null statements in Java (eugenp#6647) * BAEL2489 - Avoid check for null statement in Java * BAEL2489 Avoid check for null statement in Java * BAEL2489 Avoid check for null statement in Java * BAEL2489 Avoid check for null statement in Java * BAEL2489 Avoid check for null statement in Java * BAEL2489 Avoid check for null statement in Java - Removing unused pom changes * BAEL2489 Avoid check for null statement in Java - Removing unused pom changes * Delete pom.xml Removing unused changes in core-java * BAEL2489 Avoid check for null statement in Java - Removing unused pom changes * BAEL2489 Avoid check for null statement in Java - adding ofnullable to Optional example * BAEL-2801 (eugenp#6665) * BAEL-2727 Example Code * BAEL-2801 Example code * Change to use parent-boot-2 * fix bean config * [BAEL-13378] - Added modules those were not built * Bael 2826 case insensitive spring data jpa (eugenp#6668) * BAEL-2826: Case Insensitive Comparison Spring data jpa * BAEL-2826: Fixing imports * BAEL-2826: Case Insensitive Comparison Spring data jpa * BAEL-2826: Fixing imports * BAEL-2826: Moving code to another repo * BAEL-2826: Moving code to another repo * BAEL-2826 : Refactor * BAEL-2826 : Refactor * BAEL-2826 : Override equals hashcode * [BAEL-13313] - Extract versions into properties * BAEL * BAEL-2859 : Updating to Spring Boot 2.0.6.RELEASE * BAEL-2859 : Updating to Spring Boot 2.0.6.RELEASE * BAEL-1959: Moved to logging-modules/logback * [BAEL-13313] - Fixed PMD violation for string-test-functions module unit test * [BAEL-13313] - Fixed PMD violation for math-test-functions module * [BAEL-13378] - Added submodules in integration profiles also * BAEL-2789: update README (eugenp#6676) * BAEL-2246: add link back to article * BAEL-2174: rename core-java-net module to core-java-networking * BAEL-2174: add link back to article * BAEL-2363 BAEL-2337 BAEL-1996 BAEL-2277 add links back to articles * BAEL-2367: add link back to article * BAEL-2335: add link back to article * BAEL-2413: add link back to article * Update README.MD * BAEL-2577: add link back to article * BAEL-2490: add link back to article * BAEL-2471: add link back to article * BAEL-2583: add link back to article * BAEL-2738: add link back to article * BAEL-2711: Add spring-boot-angular module to root pom * BAEL-2544 BAEL-2711 BAEL-2575 BAEL-2657 Add links back to articles * BAEL-2736: Add link back to article * BAEL-2789: Add link back to article * BAEL-2569 (eugenp#6679) * BAEL-2569 : EnvironmentPostProcessor in Spring Boot * BAEL-2569 add test * BAEL-2569 update test * BAEL-2569 refactoring the class PriceCalculationEnvironmentPostProcessor * BAEL-2569: changes to class PriceCalculationEnvironmentPostProcessor * BAEL-2569 move code to spring-boot-ops module * [BAEL-13314] - Fix formatting of POMs * BAEL-379 A Guide to jBPM with Java (eugenp#6687) * BAEL-379 A Guide to jBPM with Java * BAEL-379 A Guide to jBPM with Java * BAEL-2811: Added sample code for jpa joins (eugenp#6691) * added jpa embeddable example * moved embeddable example to spring-data-jpa-2 * moved embeddable example to spring-data-jpa-2
1 parent 75ef80e commit 2be225b

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.embeddable.model;
2+
3+
import javax.persistence.AttributeOverride;
4+
import javax.persistence.AttributeOverrides;
5+
import javax.persistence.Column;
6+
import javax.persistence.Embedded;
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.Id;
10+
11+
@Entity
12+
public class Company {
13+
14+
@Id
15+
@GeneratedValue
16+
private Integer id;
17+
18+
private String name;
19+
20+
private String address;
21+
22+
private String phone;
23+
24+
@Embedded
25+
@AttributeOverrides(value = {
26+
@AttributeOverride( name = "firstName", column = @Column(name = "contact_first_name")),
27+
@AttributeOverride( name = "lastName", column = @Column(name = "contact_last_name")),
28+
@AttributeOverride( name = "phone", column = @Column(name = "contact_phone"))
29+
})
30+
private ContactPerson contactPerson;
31+
32+
public Integer getId() {
33+
return id;
34+
}
35+
36+
public void setId(Integer id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
48+
public String getAddress() {
49+
return address;
50+
}
51+
52+
public void setAddress(String address) {
53+
this.address = address;
54+
}
55+
56+
public String getPhone() {
57+
return phone;
58+
}
59+
60+
public void setPhone(String phone) {
61+
this.phone = phone;
62+
}
63+
64+
public ContactPerson getContactPerson() {
65+
return contactPerson;
66+
}
67+
68+
public void setContactPerson(ContactPerson contactPerson) {
69+
this.contactPerson = contactPerson;
70+
}
71+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.embeddable.model;
2+
3+
import javax.persistence.Embeddable;
4+
5+
@Embeddable
6+
public class ContactPerson {
7+
8+
private String firstName;
9+
10+
private String lastName;
11+
12+
private String phone;
13+
14+
public String getFirstName() {
15+
return firstName;
16+
}
17+
18+
public void setFirstName(String firstName) {
19+
this.firstName = firstName;
20+
}
21+
22+
public String getLastName() {
23+
return lastName;
24+
}
25+
26+
public void setLastName(String lastName) {
27+
this.lastName = lastName;
28+
}
29+
30+
public String getPhone() {
31+
return phone;
32+
}
33+
34+
public void setPhone(String phone) {
35+
this.phone = phone;
36+
}
37+
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.embeddable.repositories;
2+
3+
import com.baeldung.embeddable.model.Company;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
7+
import java.util.List;
8+
9+
public interface CompanyRepository extends JpaRepository<Company, Integer> {
10+
11+
List<Company> findByContactPersonFirstName(String firstName);
12+
13+
@Query("SELECT C FROM Company C WHERE C.contactPerson.firstName = ?1")
14+
List<Company> findByContactPersonFirstNameWithJPQL(String firstName);
15+
16+
@Query(value = "SELECT * FROM company WHERE contact_first_name = ?1", nativeQuery = true)
17+
List<Company> findByContactPersonFirstNameWithNativeQuery(String firstName);
18+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.baeldung.embeddable;
2+
3+
import com.baeldung.Application;
4+
import com.baeldung.embeddable.model.Company;
5+
import com.baeldung.embeddable.model.ContactPerson;
6+
import com.baeldung.embeddable.repositories.CompanyRepository;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
import java.util.List;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(classes = {Application.class})
20+
public class EmbeddableIntegrationTest {
21+
22+
@Autowired
23+
private CompanyRepository companyRepository;
24+
25+
@Test
26+
@Transactional
27+
public void whenInsertingCompany_thenEmbeddedContactPersonDetailsAreMapped() {
28+
ContactPerson contactPerson = new ContactPerson();
29+
contactPerson.setFirstName("First");
30+
contactPerson.setLastName("Last");
31+
contactPerson.setPhone("123-456-789");
32+
33+
Company company = new Company();
34+
company.setName("Company");
35+
company.setAddress("1st street");
36+
company.setPhone("987-654-321");
37+
company.setContactPerson(contactPerson);
38+
39+
companyRepository.save(company);
40+
41+
Company result = companyRepository.getOne(company.getId());
42+
43+
assertEquals("Company", result.getName());
44+
assertEquals("1st street", result.getAddress());
45+
assertEquals("987-654-321", result.getPhone());
46+
assertEquals("First", result.getContactPerson().getFirstName());
47+
assertEquals("Last", result.getContactPerson().getLastName());
48+
assertEquals("123-456-789", result.getContactPerson().getPhone());
49+
}
50+
51+
@Test
52+
@Transactional
53+
public void whenFindingCompanyByContactPersonAttribute_thenCompanyIsReturnedProperly() {
54+
ContactPerson contactPerson = new ContactPerson();
55+
contactPerson.setFirstName("Name");
56+
contactPerson.setLastName("Last");
57+
contactPerson.setPhone("123-456-789");
58+
59+
Company company = new Company();
60+
company.setName("Company");
61+
company.setAddress("1st street");
62+
company.setPhone("987-654-321");
63+
company.setContactPerson(contactPerson);
64+
65+
companyRepository.save(company);
66+
67+
List<Company> result = companyRepository.findByContactPersonFirstName("Name");
68+
69+
assertEquals(1, result.size());
70+
71+
result = companyRepository.findByContactPersonFirstName("FirstName");
72+
73+
assertEquals(0, result.size());
74+
}
75+
76+
@Test
77+
@Transactional
78+
public void whenFindingCompanyByContactPersonAttributeWithJPQL_thenCompanyIsReturnedProperly() {
79+
ContactPerson contactPerson = new ContactPerson();
80+
contactPerson.setFirstName("@QueryName");
81+
contactPerson.setLastName("Last");
82+
contactPerson.setPhone("123-456-789");
83+
84+
Company company = new Company();
85+
company.setName("Company");
86+
company.setAddress("1st street");
87+
company.setPhone("987-654-321");
88+
company.setContactPerson(contactPerson);
89+
90+
companyRepository.save(company);
91+
92+
List<Company> result = companyRepository.findByContactPersonFirstNameWithJPQL("@QueryName");
93+
94+
assertEquals(1, result.size());
95+
96+
result = companyRepository.findByContactPersonFirstNameWithJPQL("FirstName");
97+
98+
assertEquals(0, result.size());
99+
}
100+
101+
@Test
102+
@Transactional
103+
public void whenFindingCompanyByContactPersonAttributeWithNativeQuery_thenCompanyIsReturnedProperly() {
104+
ContactPerson contactPerson = new ContactPerson();
105+
contactPerson.setFirstName("NativeQueryName");
106+
contactPerson.setLastName("Last");
107+
contactPerson.setPhone("123-456-789");
108+
109+
Company company = new Company();
110+
company.setName("Company");
111+
company.setAddress("1st street");
112+
company.setPhone("987-654-321");
113+
company.setContactPerson(contactPerson);
114+
115+
companyRepository.save(company);
116+
117+
List<Company> result = companyRepository.findByContactPersonFirstNameWithNativeQuery("NativeQueryName");
118+
119+
assertEquals(1, result.size());
120+
121+
result = companyRepository.findByContactPersonFirstNameWithNativeQuery("FirstName");
122+
123+
assertEquals(0, result.size());
124+
}
125+
}

0 commit comments

Comments
 (0)