updated example codes for Apache shiro and Vavr Collections API
This commit is contained in:
commit
e05af1a987
@ -0,0 +1,26 @@
|
|||||||
|
public class BinarySearch {
|
||||||
|
|
||||||
|
public int runBinarySearch() {
|
||||||
|
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||||
|
int key = 6;
|
||||||
|
|
||||||
|
int low = 0;
|
||||||
|
int high = sortedArray.length - 1;
|
||||||
|
int index = Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
while (low <= high) {
|
||||||
|
|
||||||
|
int mid = (low + high) / 2;
|
||||||
|
|
||||||
|
if (sortedArray[mid] < key) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else if (sortedArray[mid] > key) {
|
||||||
|
high = mid - 1;
|
||||||
|
} else if (sortedArray[mid] == key) {
|
||||||
|
index = mid;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
algorithms/src/test/java/algorithms/BinarySearchTest.java
Normal file
14
algorithms/src/test/java/algorithms/BinarySearchTest.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
public class BinarySearchTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedArrayOfIntegers_whenBinarySearchRunForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
int expectedIndexForSearchKey = 7;
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearch());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -2,11 +2,12 @@ package com.baeldung;
|
|||||||
|
|
||||||
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.SecurityUtils;
|
||||||
import org.apache.shiro.authc.*;
|
import org.apache.shiro.authc.*;
|
||||||
import org.apache.shiro.config.IniSecurityManagerFactory;
|
import org.apache.shiro.mgt.DefaultSecurityManager;
|
||||||
import org.apache.shiro.mgt.SecurityManager;
|
import org.apache.shiro.mgt.SecurityManager;
|
||||||
|
import org.apache.shiro.realm.Realm;
|
||||||
|
import org.apache.shiro.realm.text.IniRealm;
|
||||||
import org.apache.shiro.session.Session;
|
import org.apache.shiro.session.Session;
|
||||||
import org.apache.shiro.subject.Subject;
|
import org.apache.shiro.subject.Subject;
|
||||||
import org.apache.shiro.util.Factory;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -16,9 +17,8 @@ public class Main {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Factory<SecurityManager> factory
|
Realm realm = new MyCustomRealm();
|
||||||
= new IniSecurityManagerFactory("classpath:shiro.ini");
|
SecurityManager securityManager = new DefaultSecurityManager(realm);
|
||||||
SecurityManager securityManager = factory.getInstance();
|
|
||||||
|
|
||||||
SecurityUtils.setSecurityManager(securityManager);
|
SecurityUtils.setSecurityManager(securityManager);
|
||||||
Subject currentUser = SecurityUtils.getSubject();
|
Subject currentUser = SecurityUtils.getSubject();
|
||||||
|
|||||||
@ -1,3 +1,9 @@
|
|||||||
jdbcRealm = com.baeldung.MyCustomRealm
|
[users]
|
||||||
|
user = password,admin
|
||||||
|
user2 = password2,editor
|
||||||
|
user3 = password3,author
|
||||||
|
|
||||||
securityManager.realms = $jdbcRealm
|
[roles]
|
||||||
|
admin = *
|
||||||
|
editor = articles:*
|
||||||
|
author = articles:compose,articles:save
|
||||||
@ -4,7 +4,7 @@ package com.baeldung.concurrent.volatilekeyword;
|
|||||||
public class SharedObject {
|
public class SharedObject {
|
||||||
private volatile int count=0;
|
private volatile int count=0;
|
||||||
|
|
||||||
public void increamentCount(){
|
void increamentCount(){
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
public int getCount(){
|
public int getCount(){
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.cookies;
|
package com.baeldung.networking.cookies;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.cookies;
|
package com.baeldung.networking.cookies;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp;
|
package com.baeldung.networking.udp;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp;
|
package com.baeldung.networking.udp;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp.broadcast;
|
package com.baeldung.networking.udp.broadcast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp.broadcast;
|
package com.baeldung.networking.udp.broadcast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp.multicast;
|
package com.baeldung.networking.udp.multicast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp.multicast;
|
package com.baeldung.networking.udp.multicast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
@ -8,9 +8,9 @@ import static junit.framework.Assert.assertEquals;
|
|||||||
|
|
||||||
public class SharedObjectManualTest {
|
public class SharedObjectManualTest {
|
||||||
|
|
||||||
SharedObject sharedObject;
|
private SharedObject sharedObject;
|
||||||
int valueReadByThread2;
|
private int valueReadByThread2;
|
||||||
int valueReadByThread3;
|
private int valueReadByThread3;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@ -19,36 +19,28 @@ public class SharedObjectManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||||
Thread writer = new Thread(){
|
Thread writer = new Thread(() -> sharedObject.increamentCount());
|
||||||
public void run(){
|
|
||||||
sharedObject.increamentCount();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
writer.start();
|
writer.start();
|
||||||
|
|
||||||
|
|
||||||
Thread readerOne = new Thread(){
|
Thread readerOne = new Thread(() -> {
|
||||||
public void run(){
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
valueReadByThread2 = sharedObject.getCount();
|
valueReadByThread2 = sharedObject.getCount();
|
||||||
}
|
});
|
||||||
};
|
|
||||||
readerOne.start();
|
readerOne.start();
|
||||||
|
|
||||||
Thread readerTwo = new Thread(){
|
Thread readerTwo = new Thread(() -> {
|
||||||
public void run(){
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
valueReadByThread3 = sharedObject.getCount();
|
valueReadByThread3 = sharedObject.getCount();
|
||||||
}
|
});
|
||||||
};
|
|
||||||
readerTwo.start();
|
readerTwo.start();
|
||||||
|
|
||||||
assertEquals(1, valueReadByThread2);
|
assertEquals(1, valueReadByThread2);
|
||||||
@ -58,42 +50,22 @@ public class SharedObjectManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||||
Thread writerOne = new Thread(){
|
Thread writerOne = new Thread(() -> sharedObject.increamentCount());
|
||||||
public void run(){
|
|
||||||
sharedObject.increamentCount();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
writerOne.start();
|
writerOne.start();
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
Thread writerTwo = new Thread(){
|
Thread writerTwo = new Thread(() -> sharedObject.increamentCount());
|
||||||
public void run(){
|
|
||||||
sharedObject.increamentCount();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
writerTwo.start();
|
writerTwo.start();
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
Thread readerOne = new Thread(){
|
Thread readerOne = new Thread(() -> valueReadByThread2 = sharedObject.getCount());
|
||||||
public void run(){
|
|
||||||
valueReadByThread2= sharedObject.getCount();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
readerOne.start();
|
readerOne.start();
|
||||||
|
|
||||||
Thread readerTwo = new Thread(){
|
Thread readerTwo = new Thread(() -> valueReadByThread3 = sharedObject.getCount());
|
||||||
public void run(){
|
|
||||||
valueReadByThread3=sharedObject.getCount();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
readerTwo.start();
|
readerTwo.start();
|
||||||
|
|
||||||
assertEquals(2, valueReadByThread2);
|
assertEquals(2, valueReadByThread2);
|
||||||
assertEquals(2, valueReadByThread3);
|
assertEquals(2, valueReadByThread3);
|
||||||
|
|
||||||
}
|
}
|
||||||
@After
|
|
||||||
public void cleanup(){
|
|
||||||
sharedObject = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp;
|
package com.baeldung.networking.udp;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp.broadcast;
|
package com.baeldung.networking.udp.broadcast;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.java.networking.udp.multicast;
|
package com.baeldung.networking.udp.multicast;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -212,6 +212,11 @@
|
|||||||
<artifactId>commons-chain</artifactId>
|
<artifactId>commons-chain</artifactId>
|
||||||
<version>${commons-chain.version}</version>
|
<version>${commons-chain.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-csv</artifactId>
|
||||||
|
<version>${commons-csv.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-dbutils</groupId>
|
<groupId>commons-dbutils</groupId>
|
||||||
<artifactId>commons-dbutils</artifactId>
|
<artifactId>commons-dbutils</artifactId>
|
||||||
@ -480,6 +485,7 @@
|
|||||||
<commons-text.version>1.1</commons-text.version>
|
<commons-text.version>1.1</commons-text.version>
|
||||||
<commons-beanutils.version>1.9.3</commons-beanutils.version>
|
<commons-beanutils.version>1.9.3</commons-beanutils.version>
|
||||||
<commons-chain.version>1.2</commons-chain.version>
|
<commons-chain.version>1.2</commons-chain.version>
|
||||||
|
<commons-csv.version>1.4</commons-csv.version>
|
||||||
<jasypt.version>1.9.2</jasypt.version>
|
<jasypt.version>1.9.2</jasypt.version>
|
||||||
<javatuples.version>1.2</javatuples.version>
|
<javatuples.version>1.2</javatuples.version>
|
||||||
<javaassist.version>3.21.0-GA</javaassist.version>
|
<javaassist.version>3.21.0-GA</javaassist.version>
|
||||||
|
|||||||
@ -0,0 +1,60 @@
|
|||||||
|
package com.baeldung.commons.csv;
|
||||||
|
|
||||||
|
import org.apache.commons.csv.CSVFormat;
|
||||||
|
import org.apache.commons.csv.CSVPrinter;
|
||||||
|
import org.apache.commons.csv.CSVRecord;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class CSVReaderWriterTest {
|
||||||
|
|
||||||
|
public static final Map<String, String> AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap<String, String>() {
|
||||||
|
{
|
||||||
|
put("Dan Simmons", "Hyperion");
|
||||||
|
put("Douglas Adams", "The Hitchhiker's Guide to the Galaxy");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
public static final String[] HEADERS = { "author", "title" };
|
||||||
|
public static final String EXPECTED_FILESTREAM = "author,title\r\n" + "Dan Simmons,Hyperion\r\n" + "Douglas Adams,The Hitchhiker's Guide to the Galaxy";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException {
|
||||||
|
Reader in = new FileReader("src/test/resources/book.csv");
|
||||||
|
Iterable<CSVRecord> records = CSVFormat.DEFAULT
|
||||||
|
.withHeader(HEADERS)
|
||||||
|
.withFirstRecordAsHeader()
|
||||||
|
.parse(in);
|
||||||
|
for (CSVRecord record : records) {
|
||||||
|
String author = record.get("author");
|
||||||
|
String title = record.get("title");
|
||||||
|
assertEquals(AUTHOR_BOOK_MAP.get(author), title);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException {
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) {
|
||||||
|
AUTHOR_BOOK_MAP.forEach((author, title) -> {
|
||||||
|
try {
|
||||||
|
printer.printRecord(author, title);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
assertEquals(EXPECTED_FILESTREAM, sw
|
||||||
|
.toString()
|
||||||
|
.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
3
libraries/src/test/resources/book.csv
Normal file
3
libraries/src/test/resources/book.csv
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
author,title
|
||||||
|
Dan Simmons,Hyperion
|
||||||
|
Douglas Adams,The Hitchhiker's Guide to the Galaxy
|
||||||
|
@ -121,6 +121,12 @@
|
|||||||
<version>${hsqldb.version}</version>
|
<version>${hsqldb.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>${mysql-connector-java.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
@ -167,7 +173,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
<org.springframework.version>4.3.5.RELEASE</org.springframework.version>
|
<org.springframework.version>4.3.10.RELEASE</org.springframework.version>
|
||||||
|
|
||||||
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
||||||
|
|
||||||
@ -177,7 +183,8 @@
|
|||||||
|
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
<hibernate.version>5.2.10.Final</hibernate.version>
|
<hibernate.version>5.2.10.Final</hibernate.version>
|
||||||
<tomcat-dbcp.version>8.5.15</tomcat-dbcp.version>
|
<mysql-connector-java.version>8.0.7-dmr</mysql-connector-java.version>
|
||||||
|
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||||
<jta.version>1.1</jta.version>
|
<jta.version>1.1</jta.version>
|
||||||
<hsqldb.version>2.3.4</hsqldb.version>
|
<hsqldb.version>2.3.4</hsqldb.version>
|
||||||
<h2.version>1.4.195</h2.version>
|
<h2.version>1.4.195</h2.version>
|
||||||
|
|||||||
@ -0,0 +1,86 @@
|
|||||||
|
package com.baeldung.hibernate.manytomany.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinTable;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "Employee")
|
||||||
|
public class Employee implements Serializable {
|
||||||
|
@Id
|
||||||
|
@Column(name = "employee_id")
|
||||||
|
@GeneratedValue
|
||||||
|
private Long employeeId;
|
||||||
|
|
||||||
|
@Column(name = "first_name")
|
||||||
|
private String firstName;
|
||||||
|
|
||||||
|
@Column(name = "last_name")
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
@ManyToMany(cascade = { CascadeType.ALL })
|
||||||
|
@JoinTable(
|
||||||
|
name = "Employee_Project",
|
||||||
|
joinColumns = { @JoinColumn(name = "employee_id") },
|
||||||
|
inverseJoinColumns = { @JoinColumn(name = "project_id") }
|
||||||
|
)
|
||||||
|
Set<Project> projects = new HashSet<Project>();
|
||||||
|
|
||||||
|
|
||||||
|
public Employee() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(String firstName, String lastName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(String firstName, String lastName, Set<Project> projects) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.projects = projects;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Long getEmployeeId() {
|
||||||
|
return employeeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployeeId(Long employeeId) {
|
||||||
|
this.employeeId = employeeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Set<Project> getProjects() {
|
||||||
|
return projects;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProjects(Set<Project> projects) {
|
||||||
|
this.projects = projects;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.hibernate.manytomany.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "Project")
|
||||||
|
public class Project implements Serializable {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@Column(name = "project_id")
|
||||||
|
@GeneratedValue
|
||||||
|
private Long projectId;
|
||||||
|
|
||||||
|
@Column(name = "title")
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@ManyToMany(mappedBy = "projects")
|
||||||
|
private Set<Employee> employees = new HashSet<Employee>();
|
||||||
|
|
||||||
|
public Project() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Project(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getProjectId() {
|
||||||
|
return projectId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProjectId(Long projectId) {
|
||||||
|
this.projectId = projectId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Employee> getEmployees() {
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployees(Set<Employee> employees) {
|
||||||
|
this.employees = employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.hibernate.manytomany.util;
|
||||||
|
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
|
|
||||||
|
public class HibernateUtil {
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
private static SessionFactory buildSessionFactory() {
|
||||||
|
try {
|
||||||
|
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
||||||
|
Configuration configuration = new Configuration();
|
||||||
|
configuration.addAnnotatedClass(Employee.class);
|
||||||
|
configuration.addAnnotatedClass(Project.class);
|
||||||
|
configuration.configure("manytomany.cfg.xml");
|
||||||
|
System.out.println("Hibernate Annotation Configuration loaded");
|
||||||
|
|
||||||
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||||
|
.build();
|
||||||
|
System.out.println("Hibernate Annotation serviceRegistry created");
|
||||||
|
|
||||||
|
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
|
|
||||||
|
return sessionFactory;
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
System.err.println("Initial SessionFactory creation failed." + ex);
|
||||||
|
ex.printStackTrace();
|
||||||
|
throw new ExceptionInInitializerError(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SessionFactory getSessionFactory() {
|
||||||
|
if (sessionFactory == null)
|
||||||
|
sessionFactory = buildSessionFactory();
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
package com.baeldung.manytomany.spring;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
|
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||||
|
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||||
|
@ComponentScan({ "com.baeldung.hibernate.manytomany" })
|
||||||
|
public class PersistenceConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalSessionFactoryBean sessionFactory() {
|
||||||
|
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||||
|
sessionFactory.setDataSource(restDataSource());
|
||||||
|
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.hibernate.manytomany" });
|
||||||
|
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||||
|
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource restDataSource() {
|
||||||
|
final BasicDataSource dataSource = new BasicDataSource();
|
||||||
|
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
||||||
|
dataSource.setUrl(env.getProperty("jdbc.url"));
|
||||||
|
dataSource.setUsername(env.getProperty("jdbc.user"));
|
||||||
|
dataSource.setPassword(env.getProperty("jdbc.pass"));
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PlatformTransactionManager hibernateTransactionManager() {
|
||||||
|
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
|
||||||
|
transactionManager.setSessionFactory(sessionFactory().getObject());
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||||
|
return new PersistenceExceptionTranslationPostProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Properties hibernateProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
//hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
hibernateProperties.setProperty("hibernate.show_sql", "true");
|
||||||
|
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.persistence.manytomany.dao;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
|
import com.baeldung.persistence.dao.common.IOperations;
|
||||||
|
|
||||||
|
public interface IEmployeeDao extends IOperations<Employee>{
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.persistence.manytomany.dao;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
|
import com.baeldung.persistence.dao.common.IOperations;
|
||||||
|
|
||||||
|
public interface IProjectDao extends IOperations<Project>{
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.persistence.manytomany.dao.impl;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
|
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||||
|
import com.baeldung.persistence.manytomany.dao.IEmployeeDao;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class EmployeeDao extends AbstractHibernateDao<Employee> implements IEmployeeDao {
|
||||||
|
|
||||||
|
public EmployeeDao() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
setClazz(Employee.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.persistence.manytomany.dao.impl;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
|
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||||
|
import com.baeldung.persistence.manytomany.dao.IProjectDao;
|
||||||
|
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class ProjectDao extends AbstractHibernateDao<Project> implements IProjectDao {
|
||||||
|
|
||||||
|
public ProjectDao() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
setClazz(Project.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
27
spring-hibernate5/src/main/resources/manytomany.cfg.xml
Normal file
27
spring-hibernate5/src/main/resources/manytomany.cfg.xml
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE hibernate-configuration PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||||
|
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
|
<hibernate-configuration>
|
||||||
|
<session-factory>
|
||||||
|
<property name="hibernate.connection.driver_class">
|
||||||
|
com.mysql.jdbc.Driver
|
||||||
|
</property>
|
||||||
|
<property name="hibernate.connection.password">
|
||||||
|
buddhinisam123
|
||||||
|
</property>
|
||||||
|
<property name="hibernate.connection.url">
|
||||||
|
jdbc:mysql://localhost:3306/spring_hibernate_many_to_many
|
||||||
|
</property>
|
||||||
|
<property name="hibernate.connection.username">
|
||||||
|
root
|
||||||
|
</property>
|
||||||
|
<property name="hibernate.dialect">
|
||||||
|
org.hibernate.dialect.MySQLDialect
|
||||||
|
</property>
|
||||||
|
<property name="hibernate.current_session_context_class">
|
||||||
|
thread
|
||||||
|
</property>
|
||||||
|
<property name="hibernate.show_sql">true</property>
|
||||||
|
</session-factory>
|
||||||
|
</hibernate-configuration>
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.hibernate.manytomany;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
|
import com.baeldung.manytomany.spring.PersistenceConfig;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = {PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public final void before() {
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public final void after() {
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenEntitiesAreCreated_thenNoExceptions() {
|
||||||
|
Set<Project> projects = new HashSet<Project>();
|
||||||
|
projects.add(new Project("IT Project"));
|
||||||
|
projects.add(new Project("Networking Project"));
|
||||||
|
session.persist(new Employee("Peter", "Oven", projects));
|
||||||
|
session.persist(new Employee("Allan", "Norman", projects));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
package com.baeldung.hibernate.manytomany;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import com.baeldung.hibernate.manytomany.util.HibernateUtil;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Employee;
|
||||||
|
import com.baeldung.hibernate.manytomany.model.Project;
|
||||||
|
|
||||||
|
|
||||||
|
public class HibernateManyToManyAnnotationXMLConfigMainIntegrationTest {
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeTests() {
|
||||||
|
sessionFactory = HibernateUtil.getSessionFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSession_checkIfDatabaseIsPopulated() {
|
||||||
|
Employee employee1 = new Employee("Peter", "Oven");
|
||||||
|
Set<Project> projects = new HashSet<Project>();
|
||||||
|
projects = employee1.getProjects();
|
||||||
|
int noProjects = projects.size();
|
||||||
|
assertEquals(0,noProjects);
|
||||||
|
Project project1 = new Project("IT Project");
|
||||||
|
assertNotNull(project1);
|
||||||
|
projects.add(project1);
|
||||||
|
Project project2 = new Project("Networking Project");
|
||||||
|
assertNotNull(project2);
|
||||||
|
projects.add(project2);
|
||||||
|
employee1.setProjects(projects);
|
||||||
|
assertNotNull(employee1);
|
||||||
|
Employee employee2 = new Employee("Allan", "Norman");
|
||||||
|
employee2.setProjects(projects);
|
||||||
|
assertNotNull(employee2);
|
||||||
|
|
||||||
|
session.persist(employee1);
|
||||||
|
session.persist(employee2);
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Project> projectList = session.createQuery("FROM Project").list();
|
||||||
|
assertNotNull(projectList);
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Employee> employeeList = session.createQuery("FROM Employee").list();
|
||||||
|
assertNotNull(employeeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterTests() {
|
||||||
|
sessionFactory.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user