Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tboychuk committed Oct 3, 2018
0 parents commit c4bebf9
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
**/*.iml
**/target
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Spring Data JPA exercises
The list of exercises dedicated to training your *Spring Data JPA* skills

### No pain, No gain :heavy_exclamation_mark:

> Skill is only developed by hours and hours and hours of beating on your craft
Working on real problems, you're focused on finding a solution. Learning new things, you're trying to understand how it works.
It is important to have a different type of activities, which purpose is improving your skill

***An exercise** is a predefined task that you continuously implement to improve a certain skill* :muscle:
##
64 changes: 64 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.bobocode</groupId>
<artifactId>spring-data-jpa-exercises</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>spring-data-jpa-exercises-model</module>
<module>spring-data-jpa-exercises-util</module>
</modules>

<packaging>pom</packaging>

<properties>
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>


<!--Since JAXB APIs is no longer in default classpath in JDK 1.9 -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
</dependencies>

</project>
15 changes: 15 additions & 0 deletions spring-data-jpa-exercises-model/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-data-jpa-exercises</artifactId>
<groupId>com.bobocode</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jpa-exercises-model</artifactId>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.bobocode.model;


import lombok.*;

import javax.persistence.*;
import java.time.LocalDateTime;

@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode(of = "id")
@ToString(exclude = "user")
@Entity
@Table(name = "address")
public class Address {
@Id
@GeneratedValue
private Long id;

@Column(name = "city")
private String city;

@Column(name = "street")
private String street;

@Column(name = "street_number")
private String streetNumber;

@Column(name = "apartment_number")
private String apartmentNumber;

@Column(name = "zip_code")
private String zipCode;

@Column(name = "creation_date")
private LocalDateTime creationDate;

@OneToOne
@JoinColumn(name = "user_id")
private User user;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bobocode.model;

public enum Gender {
MALE,
FEMALE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.bobocode.model;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;

@NoArgsConstructor
@Getter
@Setter
@ToString(exclude = "user")
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue
private Long id;

@Enumerated(EnumType.STRING)
@Column(name = "role_type")
private RoleType roleType;

@Column(name = "creation_date")
private LocalDateTime creationDate = LocalDateTime.now();

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

public static Role valueOf(RoleType roleType) {
return new Role(roleType);
}

private Role(RoleType roleType) {
this.roleType = roleType;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Role)) return false;

Role role = (Role) o;

return Objects.equals(id, role.id);
}

@Override
public int hashCode() {
return 31;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bobocode.model;

public enum RoleType {
USER, ADMIN, OPERATOR, CUSTOMER
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.bobocode.model;

import lombok.*;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@NoArgsConstructor
@Getter
@Setter
@ToString
@EqualsAndHashCode(of = "id")
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue
private Long id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "email")
private String email;

@Column(name = "birthday")
private LocalDate birthday;

@Column(name = "creation_date")
private LocalDate creationDate;


@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private Address address;

@Setter(AccessLevel.PRIVATE)
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Role> roles = new HashSet<>();

public void setAddress(Address address) {
address.setUser(this);
this.address = address;
}

public void addRole(Role role) {
roles.add(role);
role.setUser(this);
}

public void addRoles(List<Role> roles) {
this.roles.addAll(roles);
roles.forEach(role -> role.setUser(this));
}

public void removeRole(Role role) {
this.roles.remove(role);
role.setUser(null);
}
}
27 changes: 27 additions & 0 deletions spring-data-jpa-exercises-util/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-data-jpa-exercises</artifactId>
<groupId>com.bobocode</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jpa-exercises-util</artifactId>

<dependencies>
<dependency>
<groupId>com.bobocode</groupId>
<artifactId>spring-data-jpa-exercises-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.codearte.jfairy</groupId>
<artifactId>jfairy</artifactId>
<version>0.5.7</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.bobocode.util;


import com.bobocode.model.Address;
import com.bobocode.model.Role;
import com.bobocode.model.RoleType;
import com.bobocode.model.User;
import io.codearte.jfairy.Fairy;
import io.codearte.jfairy.producer.person.Person;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class TestDataGenerator {

private List<Role> generateRoleList() {
Random random = new Random();
Predicate<RoleType> randomPredicate = i -> random.nextBoolean();

return Stream.of(RoleType.values())
.filter(randomPredicate)
.map(Role::valueOf)
.collect(toList());
}

public User generateUser(RoleType... roles) {
User user = generateUserWithoutRoles();
Stream.of(roles)
.map(Role::valueOf)
.forEach(user::addRole);

return user;
}

private User generateUserWithoutRoles() {
Fairy fairy = Fairy.create();
Person person = fairy.person();

User user = new User();
user.setFirstName(person.getFirstName());
user.setLastName(person.getLastName());
user.setEmail(person.getEmail());
user.setBirthday(LocalDate.of(
person.getDateOfBirth().getYear(),
person.getDateOfBirth().getMonthOfYear(),
person.getDateOfBirth().getDayOfMonth()));
user.setCreationDate(LocalDate.now());

Address address = generateAddress();
user.setAddress(address);

return user;
}


public User generateUser() {
User user = generateUserWithoutRoles();
user.addRoles(generateRoleList());

return user;
}

private Address generateAddress() {
Fairy fairy = Fairy.create();
Person person = fairy.person();

Address address = new Address();
address.setCity(person.getAddress().getCity());
address.setStreet(person.getAddress().getStreet());
address.setStreetNumber(person.getAddress().getStreetNumber());
address.setApartmentNumber(person.getAddress().getApartmentNumber());
address.setCreationDate(LocalDateTime.now());
address.setZipCode(person.getAddress().getPostalCode());

return address;
}

}

0 comments on commit c4bebf9

Please sign in to comment.