added jpa embeddable example (#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 (#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 (#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 (#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 (#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 (#6641)

* Revering tree update (#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 (#6634)

* [BAEL-13311] - Checked modules that don't use our standard parents

* BAEL2489:Avoid check for null statements in Java (#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 (#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 (#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 (#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 (#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 (#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 (#6691)

* added jpa embeddable example

* moved embeddable example to spring-data-jpa-2

* moved embeddable example to spring-data-jpa-2
This commit is contained in:
Joel Juarez 2019-04-16 22:57:57 -06:00 committed by maibin
parent 75ef80ed42
commit 2be225b6fc
4 changed files with 252 additions and 0 deletions

View File

@ -0,0 +1,71 @@
package com.baeldung.embeddable.model;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Company {
@Id
@GeneratedValue
private Integer id;
private String name;
private String address;
private String phone;
@Embedded
@AttributeOverrides(value = {
@AttributeOverride( name = "firstName", column = @Column(name = "contact_first_name")),
@AttributeOverride( name = "lastName", column = @Column(name = "contact_last_name")),
@AttributeOverride( name = "phone", column = @Column(name = "contact_phone"))
})
private ContactPerson contactPerson;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public ContactPerson getContactPerson() {
return contactPerson;
}
public void setContactPerson(ContactPerson contactPerson) {
this.contactPerson = contactPerson;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.embeddable.model;
import javax.persistence.Embeddable;
@Embeddable
public class ContactPerson {
private String firstName;
private String lastName;
private String phone;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.embeddable.repositories;
import com.baeldung.embeddable.model.Company;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface CompanyRepository extends JpaRepository<Company, Integer> {
List<Company> findByContactPersonFirstName(String firstName);
@Query("SELECT C FROM Company C WHERE C.contactPerson.firstName = ?1")
List<Company> findByContactPersonFirstNameWithJPQL(String firstName);
@Query(value = "SELECT * FROM company WHERE contact_first_name = ?1", nativeQuery = true)
List<Company> findByContactPersonFirstNameWithNativeQuery(String firstName);
}

View File

@ -0,0 +1,125 @@
package com.baeldung.embeddable;
import com.baeldung.Application;
import com.baeldung.embeddable.model.Company;
import com.baeldung.embeddable.model.ContactPerson;
import com.baeldung.embeddable.repositories.CompanyRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class EmbeddableIntegrationTest {
@Autowired
private CompanyRepository companyRepository;
@Test
@Transactional
public void whenInsertingCompany_thenEmbeddedContactPersonDetailsAreMapped() {
ContactPerson contactPerson = new ContactPerson();
contactPerson.setFirstName("First");
contactPerson.setLastName("Last");
contactPerson.setPhone("123-456-789");
Company company = new Company();
company.setName("Company");
company.setAddress("1st street");
company.setPhone("987-654-321");
company.setContactPerson(contactPerson);
companyRepository.save(company);
Company result = companyRepository.getOne(company.getId());
assertEquals("Company", result.getName());
assertEquals("1st street", result.getAddress());
assertEquals("987-654-321", result.getPhone());
assertEquals("First", result.getContactPerson().getFirstName());
assertEquals("Last", result.getContactPerson().getLastName());
assertEquals("123-456-789", result.getContactPerson().getPhone());
}
@Test
@Transactional
public void whenFindingCompanyByContactPersonAttribute_thenCompanyIsReturnedProperly() {
ContactPerson contactPerson = new ContactPerson();
contactPerson.setFirstName("Name");
contactPerson.setLastName("Last");
contactPerson.setPhone("123-456-789");
Company company = new Company();
company.setName("Company");
company.setAddress("1st street");
company.setPhone("987-654-321");
company.setContactPerson(contactPerson);
companyRepository.save(company);
List<Company> result = companyRepository.findByContactPersonFirstName("Name");
assertEquals(1, result.size());
result = companyRepository.findByContactPersonFirstName("FirstName");
assertEquals(0, result.size());
}
@Test
@Transactional
public void whenFindingCompanyByContactPersonAttributeWithJPQL_thenCompanyIsReturnedProperly() {
ContactPerson contactPerson = new ContactPerson();
contactPerson.setFirstName("@QueryName");
contactPerson.setLastName("Last");
contactPerson.setPhone("123-456-789");
Company company = new Company();
company.setName("Company");
company.setAddress("1st street");
company.setPhone("987-654-321");
company.setContactPerson(contactPerson);
companyRepository.save(company);
List<Company> result = companyRepository.findByContactPersonFirstNameWithJPQL("@QueryName");
assertEquals(1, result.size());
result = companyRepository.findByContactPersonFirstNameWithJPQL("FirstName");
assertEquals(0, result.size());
}
@Test
@Transactional
public void whenFindingCompanyByContactPersonAttributeWithNativeQuery_thenCompanyIsReturnedProperly() {
ContactPerson contactPerson = new ContactPerson();
contactPerson.setFirstName("NativeQueryName");
contactPerson.setLastName("Last");
contactPerson.setPhone("123-456-789");
Company company = new Company();
company.setName("Company");
company.setAddress("1st street");
company.setPhone("987-654-321");
company.setContactPerson(contactPerson);
companyRepository.save(company);
List<Company> result = companyRepository.findByContactPersonFirstNameWithNativeQuery("NativeQueryName");
assertEquals(1, result.size());
result = companyRepository.findByContactPersonFirstNameWithNativeQuery("FirstName");
assertEquals(0, result.size());
}
}