+10
-17
@@ -1,8 +1,8 @@
|
||||
<?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">
|
||||
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>
|
||||
<artifactId>cdi</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-4</artifactId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-4</relativePath>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -26,28 +26,22 @@
|
||||
<artifactId>weld-se-core</artifactId>
|
||||
<version>${weld-se-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectjweaver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
@@ -61,7 +55,6 @@
|
||||
<weld-se-core.version>3.0.5.Final</weld-se-core.version>
|
||||
<aspectjweaver.version>1.9.2</aspectjweaver.version>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<spring.version>5.1.2.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+1
@@ -10,5 +10,6 @@ public class CopyListServiceUnitTest {
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenModifyCopyOfList_thenThrowsException() {
|
||||
List<Integer> copyList = List.copyOf(Arrays.asList(1, 2, 3, 4));
|
||||
copyList.add(4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,5 +30,4 @@
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.nullmethodparameter;
|
||||
|
||||
public class NullParameterExample {
|
||||
public void processSomethingNotNull(Object myParameter) {
|
||||
if (myParameter == null) {
|
||||
throw new IllegalArgumentException("Parameter 'myParameter' cannot be null");
|
||||
}
|
||||
//Do something with the parameter
|
||||
}
|
||||
|
||||
public void processSomethingElseNotNull(Object myParameter) {
|
||||
if (myParameter == null) {
|
||||
throw new NullPointerException("Parameter 'myParameter' cannot be null");
|
||||
}
|
||||
//Do something with the parameter
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.nullmethodparameter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NullParameterExampleUnitTest {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenNullParameter_whenProcessSomethingNotNull_thenIllegalArgumentException() {
|
||||
NullParameterExample example = new NullParameterExample();
|
||||
example.processSomethingNotNull(null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullParameter_whenProcessSomethingElseNotNull_thenNullPointerException() {
|
||||
NullParameterExample example = new NullParameterExample();
|
||||
example.processSomethingElseNotNull(null);
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.algorithms.largestpowerof2;
|
||||
|
||||
import org.nd4j.linalg.io.Assert;
|
||||
|
||||
public class LargestPowerOf2 {
|
||||
public long findLargestPowerOf2LessThanTheGivenNumber(long input) {
|
||||
Assert.isTrue(input > 1, "Invalid input");
|
||||
|
||||
long firstPowerOf2 = 1;
|
||||
long nextPowerOf2 = 2;
|
||||
|
||||
while (nextPowerOf2 < input) {
|
||||
firstPowerOf2 = nextPowerOf2;
|
||||
nextPowerOf2 = nextPowerOf2 * 2;
|
||||
}
|
||||
return firstPowerOf2;
|
||||
}
|
||||
|
||||
public long findLargestPowerOf2LessThanTheGivenNumberUsingLogBase2(long input) {
|
||||
Assert.isTrue(input > 1, "Invalid input");
|
||||
|
||||
long temp = input;
|
||||
if (input % 2 == 0) {
|
||||
temp = input - 1;
|
||||
}
|
||||
|
||||
// Find log base 2 of a given number
|
||||
long power = (long) (Math.log(temp) / Math.log(2));
|
||||
long result = (long) Math.pow(2, power);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public long findLargestPowerOf2LessThanTheGivenNumberUsingBitwiseAnd(long input) {
|
||||
Assert.isTrue(input > 1, "Invalid input");
|
||||
long result = 1;
|
||||
for (long i = input - 1; i > 1; i--) {
|
||||
if ((i & (i - 1)) == 0) {
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public long findLargestPowerOf2LessThanTheGivenNumberUsingBitShiftApproach(long input) {
|
||||
Assert.isTrue(input > 1, "Invalid input");
|
||||
long result = 1;
|
||||
long powerOf2;
|
||||
|
||||
for (long i = 0; i < Long.BYTES * 8; i++) {
|
||||
powerOf2 = 1 << i;
|
||||
if (powerOf2 >= input) {
|
||||
break;
|
||||
}
|
||||
result = powerOf2;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.algorithms.largestpowerof2;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class LargestPowerOf2UnitTest {
|
||||
private long input;
|
||||
private long expectedResult;
|
||||
|
||||
public LargestPowerOf2UnitTest(long input, long expectedResult) {
|
||||
this.input = input;
|
||||
this.expectedResult = expectedResult;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters(name = "{index}: verifyLargestPowerOf2LessThanTheGivenNumber({0}) = {1}")
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][] { { 2, 1 }, { 4, 2 }, { 500, 256 }, { 512, 256 }, { 1050, 1024 } });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumber() {
|
||||
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
|
||||
|
||||
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumber(input);
|
||||
|
||||
Assert.assertEquals(expectedResult, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumberUsingLogBase2() {
|
||||
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
|
||||
|
||||
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumberUsingLogBase2(input);
|
||||
|
||||
Assert.assertEquals(expectedResult, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumberBitwiseAnd() {
|
||||
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
|
||||
|
||||
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumberUsingBitwiseAnd(input);
|
||||
|
||||
Assert.assertEquals(expectedResult, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_verifyLargestPowerOf2LessThanTheGivenNumberBitShiftApproach() {
|
||||
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
|
||||
|
||||
long result = largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumberUsingBitShiftApproach(input);
|
||||
|
||||
Assert.assertEquals(expectedResult, result);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenInvalidInput_ShouldThrowException() {
|
||||
LargestPowerOf2 largestPowerOf2 = new LargestPowerOf2();
|
||||
largestPowerOf2.findLargestPowerOf2LessThanTheGivenNumber(1);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
### Relevant Article:
|
||||
|
||||
- [Creating Docker Images with Spring Boot](https://www.baeldung.com/spring-boot-docker-images)
|
||||
@@ -1,3 +1,4 @@
|
||||
rootProject.name='gradle-5-articles'
|
||||
include 'java-exec'
|
||||
include 'unused-dependencies'
|
||||
include 'unused-dependencies'
|
||||
include 'source-sets'
|
||||
@@ -0,0 +1 @@
|
||||
/build/
|
||||
@@ -0,0 +1,67 @@
|
||||
|
||||
apply plugin: "eclipse"
|
||||
apply plugin: "java"
|
||||
|
||||
description = "Source Sets example"
|
||||
|
||||
task printSourceSetInformation(){
|
||||
description = "Print source set information"
|
||||
|
||||
doLast{
|
||||
sourceSets.each { srcSet ->
|
||||
println "["+srcSet.name+"]"
|
||||
print "-->Source directories: "+srcSet.allJava.srcDirs+"\n"
|
||||
print "-->Output directories: "+srcSet.output.classesDirs.files+"\n"
|
||||
print "-->Compile classpath:\n"
|
||||
srcSet.compileClasspath.files.each {
|
||||
print " "+it.path+"\n"
|
||||
}
|
||||
println ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets{
|
||||
itest {
|
||||
compileClasspath += sourceSets.main.output
|
||||
runtimeClasspath += sourceSets.main.output
|
||||
java {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test {
|
||||
testLogging {
|
||||
events "passed","skipped", "failed"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation('org.apache.httpcomponents:httpclient:4.5.12')
|
||||
testImplementation('junit:junit:4.12')
|
||||
itestImplementation('com.google.guava:guava:29.0-jre')
|
||||
}
|
||||
|
||||
task itest(type: Test) {
|
||||
description = "Run integration tests"
|
||||
group = "verification"
|
||||
testClassesDirs = sourceSets.itest.output.classesDirs
|
||||
classpath = sourceSets.itest.runtimeClasspath
|
||||
}
|
||||
|
||||
itest {
|
||||
testLogging {
|
||||
events "passed","skipped", "failed"
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
itestImplementation.extendsFrom(testImplementation)
|
||||
itestRuntimeOnly.extendsFrom(testRuntimeOnly)
|
||||
}
|
||||
|
||||
eclipse {
|
||||
classpath {
|
||||
plusConfigurations+=[configurations.itestCompileClasspath]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.itest;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.main.SourceSetsObject;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class SourceSetsItest {
|
||||
|
||||
@Test
|
||||
public void givenImmutableList_whenRun_ThenSuccess() {
|
||||
|
||||
SourceSetsObject underTest = new SourceSetsObject("lorem", "ipsum");
|
||||
List<String> someStrings = ImmutableList.of("Baeldung", "is", "cool");
|
||||
|
||||
assertThat(underTest.getUser(), is("lorem"));
|
||||
assertThat(underTest.getPassword(), is("ipsum"));
|
||||
assertThat(someStrings.size(), is(3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.main;
|
||||
|
||||
public class SourceSetsMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hell..oh...world!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.main;
|
||||
|
||||
public class SourceSetsObject {
|
||||
|
||||
private final String user;
|
||||
private final String password;
|
||||
|
||||
public SourceSetsObject(String user, String password) {
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.main.SourceSetsObject;
|
||||
|
||||
public class SourceSetsTest {
|
||||
|
||||
@Test
|
||||
public void whenRun_ThenSuccess() {
|
||||
|
||||
SourceSetsObject underTest = new SourceSetsObject("lorem", "ipsum");
|
||||
|
||||
assertThat(underTest.getUser(), is("lorem"));
|
||||
assertThat(underTest.getPassword(), is("ipsum"));
|
||||
}
|
||||
}
|
||||
-1
@@ -24,7 +24,6 @@ public class SmurfsRepository {
|
||||
smurfs.put("Architect", new Smurf("Architect", true, true));
|
||||
smurfs.put("Baby", new Smurf("Baby", true, true));
|
||||
smurfs.put("Baker", new Smurf("Baker", true, true));
|
||||
smurfs.put("Baker", new Smurf("Baker", true, true));
|
||||
}
|
||||
|
||||
public List<Smurf> findAll() {
|
||||
|
||||
-2
@@ -5,8 +5,6 @@ public class Smurf {
|
||||
private boolean comic;
|
||||
private boolean cartoon;
|
||||
|
||||
public Smurf() {}
|
||||
|
||||
public Smurf(String name, boolean comic, boolean cartoon) {
|
||||
this.name = name;
|
||||
this.comic = comic;
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-4</artifactId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-4</relativePath>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
+12
-23
@@ -27,6 +27,7 @@
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
<version>${version.derby}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- For tests -->
|
||||
@@ -70,27 +71,16 @@
|
||||
<groupId>io.openliberty.tools</groupId>
|
||||
<artifactId>liberty-maven-plugin</artifactId>
|
||||
<version>${version.liberty-maven-plugin}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>${version.maven-dependency-plugin}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-derby-dependency</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includeArtifactIds>derby</includeArtifactIds>
|
||||
<outputDirectory>${project.build.directory}/liberty/wlp/usr/shared/resources/</outputDirectory>
|
||||
<systemPropertyVariables>
|
||||
<liberty.test.port>${testServerHttpPort}</liberty.test.port>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<copyDependencies>
|
||||
<location>${project.build.directory}/liberty/wlp/usr/shared/resources/</location>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
<version>${version.derby}</version>
|
||||
</dependency>
|
||||
</copyDependencies>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
@@ -112,8 +102,7 @@
|
||||
<version.jakarta.jakartaee-web-api>8.0.0</version.jakarta.jakartaee-web-api>
|
||||
<version.microprofile>3.2</version.microprofile>
|
||||
<version.derby>10.14.2.0</version.derby>
|
||||
<version.liberty-maven-plugin>3.1</version.liberty-maven-plugin>
|
||||
<version.maven-dependency-plugin>2.10</version.maven-dependency-plugin>
|
||||
<version.liberty-maven-plugin>3.3-M3</version.liberty-maven-plugin>
|
||||
<version.maven-war-plugin>3.2.3</version.maven-war-plugin>
|
||||
<version.junit>4.12</version.junit>
|
||||
<version.yasson>1.0.5</version.yasson>
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ public class EqualByBusinessKey {
|
||||
return false;
|
||||
}
|
||||
if (obj instanceof EqualByBusinessKey) {
|
||||
if (((EqualByBusinessKey) obj).getEmail() == getEmail()) {
|
||||
if (((EqualByBusinessKey) obj).getEmail().equals(getEmail())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
+29
-10
@@ -1,14 +1,9 @@
|
||||
package com.baeldung.manytomany.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "course")
|
||||
public class Course {
|
||||
@@ -18,31 +13,55 @@ public class Course {
|
||||
private Long id;
|
||||
|
||||
@ManyToMany(mappedBy = "likedCourses")
|
||||
private Set<Student> likes;
|
||||
private Set<Student> likes = new HashSet<>();
|
||||
|
||||
@OneToMany(mappedBy = "course")
|
||||
private Set<CourseRating> ratings;
|
||||
private Set<CourseRating> ratings = new HashSet<>();
|
||||
|
||||
@OneToMany(mappedBy = "course")
|
||||
private Set<CourseRegistration> registrations;
|
||||
private Set<CourseRegistration> registrations = new HashSet<>();
|
||||
|
||||
// additional properties
|
||||
|
||||
public Course() {
|
||||
}
|
||||
|
||||
public Course(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Set<Student> getLikes() {
|
||||
return likes;
|
||||
}
|
||||
|
||||
public void setLikes(Set<Student> likes) {
|
||||
this.likes = likes;
|
||||
}
|
||||
|
||||
public Set<CourseRating> getRatings() {
|
||||
return ratings;
|
||||
}
|
||||
|
||||
public void setRatings(Set<CourseRating> ratings) {
|
||||
this.ratings = ratings;
|
||||
}
|
||||
|
||||
public Set<CourseRegistration> getRegistrations() {
|
||||
return registrations;
|
||||
}
|
||||
|
||||
public void setRegistrations(Set<CourseRegistration> registrations) {
|
||||
this.registrations = registrations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
||||
+23
-17
@@ -1,12 +1,6 @@
|
||||
package com.baeldung.manytomany.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.MapsId;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "course_rating")
|
||||
@@ -16,12 +10,12 @@ public class CourseRating {
|
||||
private CourseRatingKey id;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("student_id")
|
||||
@MapsId("studentId")
|
||||
@JoinColumn(name = "student_id")
|
||||
private Student student;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("course_id")
|
||||
@MapsId("courseId")
|
||||
@JoinColumn(name = "course_id")
|
||||
private Course course;
|
||||
|
||||
@@ -31,26 +25,38 @@ public class CourseRating {
|
||||
public CourseRating() {
|
||||
}
|
||||
|
||||
public int getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(int rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public CourseRatingKey getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(CourseRatingKey id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Student getStudent() {
|
||||
return student;
|
||||
}
|
||||
|
||||
public void setStudent(Student student) {
|
||||
this.student = student;
|
||||
}
|
||||
|
||||
public Course getCourse() {
|
||||
return course;
|
||||
}
|
||||
|
||||
public void setCourse(Course course) {
|
||||
this.course = course;
|
||||
}
|
||||
|
||||
public int getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(int rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
||||
+14
-2
@@ -1,9 +1,8 @@
|
||||
package com.baeldung.manytomany.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Embeddable
|
||||
public class CourseRatingKey implements Serializable {
|
||||
@@ -17,14 +16,27 @@ public class CourseRatingKey implements Serializable {
|
||||
public CourseRatingKey() {
|
||||
}
|
||||
|
||||
public CourseRatingKey(Long studentId, Long courseId) {
|
||||
this.studentId = studentId;
|
||||
this.courseId = courseId;
|
||||
}
|
||||
|
||||
public Long getStudentId() {
|
||||
return studentId;
|
||||
}
|
||||
|
||||
public void setStudentId(Long studentId) {
|
||||
this.studentId = studentId;
|
||||
}
|
||||
|
||||
public Long getCourseId() {
|
||||
return courseId;
|
||||
}
|
||||
|
||||
public void setCourseId(Long courseId) {
|
||||
this.courseId = courseId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
||||
+9
-11
@@ -1,14 +1,8 @@
|
||||
package com.baeldung.manytomany.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "course_registration")
|
||||
public class CourseRegistration {
|
||||
@@ -36,6 +30,14 @@ public class CourseRegistration {
|
||||
public CourseRegistration() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Student getStudent() {
|
||||
return student;
|
||||
}
|
||||
@@ -68,10 +70,6 @@ public class CourseRegistration {
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
||||
+25
-12
@@ -1,16 +1,9 @@
|
||||
package com.baeldung.manytomany.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "student")
|
||||
public class Student {
|
||||
@@ -21,35 +14,55 @@ public class Student {
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(name = "course_like", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
|
||||
private Set<Course> likedCourses;
|
||||
private Set<Course> likedCourses = new HashSet<>();
|
||||
|
||||
@OneToMany(mappedBy = "student")
|
||||
private Set<CourseRating> ratings;
|
||||
private Set<CourseRating> ratings = new HashSet<>();
|
||||
|
||||
@OneToMany(mappedBy = "student")
|
||||
private Set<CourseRegistration> registrations;
|
||||
private Set<CourseRegistration> registrations = new HashSet<>();
|
||||
|
||||
// additional properties
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Set<Course> getLikedCourses() {
|
||||
return likedCourses;
|
||||
}
|
||||
|
||||
public void setLikedCourses(Set<Course> likedCourses) {
|
||||
this.likedCourses = likedCourses;
|
||||
}
|
||||
|
||||
public Set<CourseRating> getRatings() {
|
||||
return ratings;
|
||||
}
|
||||
|
||||
public void setRatings(Set<CourseRating> ratings) {
|
||||
this.ratings = ratings;
|
||||
}
|
||||
|
||||
public Set<CourseRegistration> getRegistrations() {
|
||||
return registrations;
|
||||
}
|
||||
|
||||
public void setRegistrations(Set<CourseRegistration> registrations) {
|
||||
this.registrations = registrations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
||||
+34
-3
@@ -1,17 +1,27 @@
|
||||
package com.baeldung.manytomany;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import com.baeldung.manytomany.model.Course;
|
||||
import com.baeldung.manytomany.model.CourseRating;
|
||||
import com.baeldung.manytomany.model.CourseRatingKey;
|
||||
import com.baeldung.manytomany.model.Student;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ManyToManyTestConfiguration.class)
|
||||
@DirtiesContext
|
||||
@Transactional
|
||||
public class ManyToManyIntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
@@ -21,4 +31,25 @@ public class ManyToManyIntegrationTest {
|
||||
public void contextStarted() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCourseRatingPersisted_thenCorrect() {
|
||||
Student student = new Student(101L);
|
||||
entityManager.persist(student);
|
||||
|
||||
Course course = new Course(201L);
|
||||
entityManager.persist(course);
|
||||
|
||||
CourseRating courseRating = new CourseRating();
|
||||
courseRating.setId(new CourseRatingKey());
|
||||
courseRating.setStudent(student);
|
||||
courseRating.setCourse(course);
|
||||
courseRating.setRating(100);
|
||||
entityManager.persist(courseRating);
|
||||
|
||||
CourseRating persistedCourseRating = entityManager.find(CourseRating.class, new CourseRatingKey(101L, 201L));
|
||||
|
||||
assertThat(persistedCourseRating, notNullValue());
|
||||
assertThat(persistedCourseRating.getStudent().getId(), is(101L));
|
||||
assertThat(persistedCourseRating.getCourse().getId(), is(201L));
|
||||
}
|
||||
}
|
||||
|
||||
+15
-7
@@ -1,20 +1,21 @@
|
||||
package com.baeldung.manytomany;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("manytomany/test.properties")
|
||||
public class ManyToManyTestConfiguration {
|
||||
@@ -23,8 +24,8 @@ public class ManyToManyTestConfiguration {
|
||||
public DataSource dataSource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.addScript("classpath:/manytomany/db.sql")
|
||||
.build();
|
||||
.addScript("classpath:/manytomany/db.sql")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -44,6 +45,13 @@ public class ManyToManyTestConfiguration {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Bean
|
||||
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
|
||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
public JpaVendorAdapter jpaVendorAdapter() {
|
||||
return new HibernateJpaVendorAdapter();
|
||||
}
|
||||
|
||||
@@ -701,6 +701,7 @@
|
||||
<module>spring-rest-compress</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-http-2</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
|
||||
+41
-1
@@ -30,9 +30,48 @@
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy-jackson</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-hibernate-orm-panache</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-jdbc-h2</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.6</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5-mockito</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-test-h2</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -117,7 +156,8 @@
|
||||
|
||||
<properties>
|
||||
<surefire-plugin.version>2.22.0</surefire-plugin.version>
|
||||
<quarkus.version>0.15.0</quarkus.version>
|
||||
<quarkus.version>1.7.0.Final</quarkus.version>
|
||||
<junit-jupiter.version>5.6.0</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user