Merge remote-tracking branch 'upstream/master' into BAEL-1754
This commit is contained in:
commit
ca78752618
28
core-java-8/src/main/java/com/baeldung/hashtable/Word.java
Normal file
28
core-java-8/src/main/java/com/baeldung/hashtable/Word.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.baeldung.hashtable;
|
||||
|
||||
public class Word {
|
||||
private String name;
|
||||
|
||||
public Word(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof Word))
|
||||
return false;
|
||||
|
||||
Word word = (Word) o;
|
||||
return word.getName().equals(this.name) ? true : false;
|
||||
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
@ -0,0 +1,274 @@
|
||||
package com.baeldung.hashtable;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HashtableUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenPutAndGet_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
|
||||
String definition = table.get(word);
|
||||
|
||||
assertEquals("an animal", definition);
|
||||
|
||||
definition = table.remove(word);
|
||||
|
||||
assertEquals("an animal", definition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenThesameInstanceOfKey_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
String extracted = table.get(word);
|
||||
assertEquals("an animal", extracted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEqualsOverridden_thenReturnsValue() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
Word word = new Word("cat");
|
||||
table.put(word, "an animal");
|
||||
String extracted = table.get(new Word("cat"));
|
||||
assertEquals("an animal", extracted);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenNullKey_thenException() {
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(null, "an animal");
|
||||
}
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenIterate_thenFailFast() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "an animal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
|
||||
Iterator<Word> it = table.keySet().iterator();
|
||||
System.out.println("iterator created");
|
||||
|
||||
table.remove(new Word("dog"));
|
||||
System.out.println("element removed");
|
||||
|
||||
while (it.hasNext()) {
|
||||
Word key = it.next();
|
||||
System.out.println(table.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEnumerate_thenNotFailFast() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("1"), "one");
|
||||
table.put(new Word("2"), "two");
|
||||
table.put(new Word("3"), "three");
|
||||
table.put(new Word("4"), "four");
|
||||
table.put(new Word("5"), "five");
|
||||
table.put(new Word("6"), "six");
|
||||
table.put(new Word("7"), "seven");
|
||||
table.put(new Word("8"), "eight");
|
||||
|
||||
Enumeration<Word> enumKey = table.keys();
|
||||
System.out.println("Enumeration created");
|
||||
table.remove(new Word("1"));
|
||||
System.out.println("element removed");
|
||||
while (enumKey.hasMoreElements()) {
|
||||
Word key = enumKey.nextElement();
|
||||
System.out.println(table.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddElements_thenIterationOrderUnpredicable() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("1"), "one");
|
||||
table.put(new Word("2"), "two");
|
||||
table.put(new Word("3"), "three");
|
||||
table.put(new Word("4"), "four");
|
||||
table.put(new Word("5"), "five");
|
||||
table.put(new Word("6"), "six");
|
||||
table.put(new Word("7"), "seven");
|
||||
table.put(new Word("8"), "eight");
|
||||
|
||||
Iterator<Map.Entry<Word, String>> it = table.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Word, String> entry = it.next();
|
||||
System.out.println(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetOrDefault_thenDefaultGot() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
Word key = new Word("dog");
|
||||
String definition;
|
||||
|
||||
// old way
|
||||
/* if (table.containsKey(key)) {
|
||||
definition = table.get(key);
|
||||
} else {
|
||||
definition = "not found";
|
||||
}*/
|
||||
|
||||
// new way
|
||||
definition = table.getOrDefault(key, "not found");
|
||||
|
||||
assertThat(definition, is("not found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPutifAbsent_thenNotRewritten() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
String definition = "an animal";
|
||||
// old way
|
||||
/* if (!table.containsKey(new Word("cat"))) {
|
||||
table.put(new Word("cat"), definition);
|
||||
}*/
|
||||
// new way
|
||||
table.putIfAbsent(new Word("cat"), definition);
|
||||
|
||||
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemovePair_thenCheckKeyAndValue() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
// old way
|
||||
/* if (table.get(new Word("cat")).equals("an animal")) {
|
||||
table.remove(new Word("cat"));
|
||||
}*/
|
||||
|
||||
// new way
|
||||
boolean result = table.remove(new Word("cat"), "an animal");
|
||||
|
||||
assertThat(result, is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplacePair_thenValueChecked() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
String definition = "an animal";
|
||||
|
||||
// old way
|
||||
/* if (table.containsKey(new Word("cat")) && table.get(new Word("cat")).equals("a small domesticated carnivorous mammal")) {
|
||||
table.put(new Word("cat"), definition);
|
||||
}*/
|
||||
// new way
|
||||
table.replace(new Word("cat"), "a small domesticated carnivorous mammal", definition);
|
||||
|
||||
assertThat(table.get(new Word("cat")), is("an animal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyIsAbsent_thenNotRewritten() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
// old way
|
||||
/* if (!table.containsKey(cat)) {
|
||||
String definition = "an animal";// calculate
|
||||
table.put(new Word("cat"), definition);
|
||||
}
|
||||
*/
|
||||
// new way
|
||||
|
||||
table.computeIfAbsent(new Word("cat"), key -> "an animal");
|
||||
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyIsPresent_thenComputeIfPresent() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
|
||||
Word cat = new Word("cat");
|
||||
// old way
|
||||
/* if (table.containsKey(cat)) {
|
||||
String concatination = cat.getName() + " - " + table.get(cat);
|
||||
table.put(cat, concatination);
|
||||
}*/
|
||||
|
||||
// new way
|
||||
table.computeIfPresent(cat, (key, value) -> key.getName() + " - " + value);
|
||||
|
||||
assertThat(table.get(cat), is("cat - a small domesticated carnivorous mammal"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompute_thenForAllKeys() {
|
||||
|
||||
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
|
||||
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
|
||||
for (String animal : animals) {
|
||||
table.compute(animal, (key, value) -> (value == null ? Integer.valueOf(1) : Integer.valueOf(value) + 1));
|
||||
}
|
||||
assertThat(table.values(), hasItems(2, 2, 2, 1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsteadOfCompute_thenMerge() {
|
||||
|
||||
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
|
||||
String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" };
|
||||
for (String animal : animals) {
|
||||
table.merge(animal, 1, (oldValue, value) -> (oldValue + value));
|
||||
}
|
||||
assertThat(table.values(), hasItems(2, 2, 2, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenForeach_thenIterate() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
table.forEach((k, v) -> System.out.println(k.getName() + " - " + v)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceall_thenNoIterationNeeded() {
|
||||
|
||||
Hashtable<Word, String> table = new Hashtable<Word, String>();
|
||||
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
|
||||
table.put(new Word("dog"), "another animal");
|
||||
table.replaceAll((k, v) -> k.getName() + " - " + v);
|
||||
|
||||
assertThat(table.values(), hasItems("cat - a small domesticated carnivorous mammal", "dog - another animal"));
|
||||
}
|
||||
}
|
@ -74,17 +74,14 @@
|
||||
- [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string)
|
||||
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||
- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator)
|
||||
- [Singletons in Java](http://www.baeldung.com/java-singleton)
|
||||
- [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws)
|
||||
- [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded)
|
||||
- [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer)
|
||||
- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int)
|
||||
- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns)
|
||||
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
|
||||
- [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static)
|
||||
- [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array)
|
||||
- [Guide to Java String Pool](http://www.baeldung.com/java-string-pool)
|
||||
- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns)
|
||||
- [Quick Example - Comparator vs Comparable in Java](http://www.baeldung.com/java-comparator-comparable)
|
||||
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
|
||||
- [The Java continue and break Keywords](http://www.baeldung.com/java-continue-and-break)
|
||||
@ -116,8 +113,6 @@
|
||||
- [Comparing Strings in Java](http://www.baeldung.com/java-compare-strings)
|
||||
- [Guide to Inheritance in Java](http://www.baeldung.com/java-inheritance)
|
||||
- [Guide to Externalizable Interface in Java](http://www.baeldung.com/java-externalizable)
|
||||
- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern)
|
||||
- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight)
|
||||
- [Object Type Casting in Java](http://www.baeldung.com/java-type-casting)
|
||||
- [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat)
|
||||
- [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os)
|
||||
@ -136,7 +131,6 @@
|
||||
- [Class Loaders in Java](http://www.baeldung.com/java-classloaders)
|
||||
- [Find Sum and Average in a Java Array](http://www.baeldung.com/java-array-sum-average)
|
||||
- [Java List UnsupportedOperationException](http://www.baeldung.com/java-list-unsupported-operation-exception)
|
||||
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
|
||||
- [Type Erasure in Java Explained](http://www.baeldung.com/java-type-erasure)
|
||||
- [BigDecimal and BigInteger in Java](http://www.baeldung.com/java-bigdecimal-biginteger)
|
||||
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
|
||||
@ -145,5 +139,5 @@
|
||||
- [Sending Emails with Java](http://www.baeldung.com/java-email)
|
||||
- [Introduction to SSL in Java](http://www.baeldung.com/java-ssl)
|
||||
- [Java KeyStore API](http://www.baeldung.com/java-keystore)
|
||||
- [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking)
|
||||
- [Using Java Assertions](http://www.baeldung.com/java-assert)
|
||||
- [Guide to Java Clock Class](http://www.baeldung.com/java-clock)
|
||||
|
@ -1,30 +1,12 @@
|
||||
package com.baeldung.hibernate;
|
||||
|
||||
import com.baeldung.hibernate.pojo.Employee;
|
||||
import com.baeldung.hibernate.pojo.EntityDescription;
|
||||
import com.baeldung.hibernate.pojo.OrderEntry;
|
||||
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
|
||||
import com.baeldung.hibernate.pojo.OrderEntryPK;
|
||||
import com.baeldung.hibernate.pojo.PointEntity;
|
||||
import com.baeldung.hibernate.pojo.PolygonEntity;
|
||||
import com.baeldung.hibernate.pojo.Product;
|
||||
import com.baeldung.hibernate.pojo.Phone;
|
||||
import com.baeldung.hibernate.pojo.TemporalValues;
|
||||
import com.baeldung.hibernate.pojo.Course;
|
||||
import com.baeldung.hibernate.pojo.Student;
|
||||
import com.baeldung.hibernate.pojo.User;
|
||||
import com.baeldung.hibernate.pojo.UserProfile;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Animal;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Bag;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Book;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Car;
|
||||
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
|
||||
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pen;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Person;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pet;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
|
||||
|
||||
import com.baeldung.hibernate.pessimisticlocking.Individual;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
|
||||
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
|
||||
import com.baeldung.hibernate.pojo.*;
|
||||
import com.baeldung.hibernate.pojo.Person;
|
||||
import com.baeldung.hibernate.pojo.inheritance.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
@ -82,6 +64,12 @@ public class HibernateUtil {
|
||||
metadataSources.addAnnotatedClass(PointEntity.class);
|
||||
metadataSources.addAnnotatedClass(PolygonEntity.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
|
||||
metadataSources.addAnnotatedClass(Individual.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingEmployee.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingStudent.class);
|
||||
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
|
||||
|
||||
Metadata metadata = metadataSources.buildMetadata();
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
|
||||
private String country;
|
||||
private String city;
|
||||
|
||||
public Address(String country, String city) {
|
||||
this.country = country;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
private Long customerId;
|
||||
private String name;
|
||||
private String lastName;
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "customer_address")
|
||||
private List<Address> addressList;
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
public Customer(Long customerId, String name, String lastName, List<Address> addressList) {
|
||||
this.customerId = customerId;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
this.addressList = addressList;
|
||||
}
|
||||
|
||||
public Long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(Long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List<Address> getAddressList() {
|
||||
return addressList;
|
||||
}
|
||||
|
||||
public void setAddressList(List<Address> addressList) {
|
||||
this.addressList = addressList;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
public class Individual {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
private String lastName;
|
||||
|
||||
public Individual(Long id, String name, String lastName) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Individual() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingCourse {
|
||||
|
||||
@Id
|
||||
private Long courseId;
|
||||
private String name;
|
||||
@ManyToOne
|
||||
@JoinTable(name = "student_course")
|
||||
private PessimisticLockingStudent student;
|
||||
|
||||
public PessimisticLockingCourse(Long courseId, String name, PessimisticLockingStudent student) {
|
||||
this.courseId = courseId;
|
||||
this.name = name;
|
||||
this.student = student;
|
||||
}
|
||||
|
||||
public PessimisticLockingCourse() {
|
||||
}
|
||||
|
||||
public Long getCourseId() {
|
||||
return courseId;
|
||||
}
|
||||
|
||||
public void setCourseId(Long courseId) {
|
||||
this.courseId = courseId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public PessimisticLockingStudent getStudent() {
|
||||
return student;
|
||||
}
|
||||
|
||||
public void setStudent(PessimisticLockingStudent students) {
|
||||
this.student = students;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingEmployee extends Individual {
|
||||
|
||||
private BigDecimal salary;
|
||||
|
||||
public PessimisticLockingEmployee(Long id, String name, String lastName, BigDecimal salary) {
|
||||
super(id, name, lastName);
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public PessimisticLockingEmployee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BigDecimal getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(BigDecimal average) {
|
||||
this.salary = average;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class PessimisticLockingStudent {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "student")
|
||||
private List<PessimisticLockingCourse> courses;
|
||||
|
||||
public PessimisticLockingStudent(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public PessimisticLockingStudent() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<PessimisticLockingCourse> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<PessimisticLockingCourse> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
@ -0,0 +1,151 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.vividsolutions.jts.util.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BasicPessimisticLockingIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOHN");
|
||||
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "MATH", student);
|
||||
student.setCourses(Arrays.asList(course));
|
||||
entityManager.persist(course);
|
||||
entityManager.persist(student);
|
||||
entityManager.getTransaction()
|
||||
.commit();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFoundRecordWithPessimisticRead_whenFindingNewOne_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
entityManager.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticReadQuery_whenQueryingNewOne_PessimisticLockExceptionThrown() throws IOException {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
Query query = entityManager.createQuery("from Student where studentId = :studentId");
|
||||
query.setParameter("studentId", 1L);
|
||||
query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
|
||||
query.getResultList();
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
Query query2 = entityManager2.createQuery("from Student where studentId = :studentId");
|
||||
query2.setParameter("studentId", 1L);
|
||||
query2.setLockMode(LockModeType.PESSIMISTIC_READ);
|
||||
query2.getResultList();
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticReadLock_whenFindingNewOne_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.lock(resultStudent, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordAndRefreshWithPessimisticRead_whenFindingWithPessimisticWrite_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticRead_whenUpdatingRecord_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_READ);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent2 = entityManager2.find(PessimisticLockingStudent.class, 1L);
|
||||
resultStudent2.setName("Change");
|
||||
entityManager2.persist(resultStudent2);
|
||||
entityManager2.getTransaction()
|
||||
.commit();
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordWithPessimisticWrite_whenUpdatingRecord_PessimisticLockExceptionThrown() {
|
||||
try {
|
||||
EntityManager entityManager = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L);
|
||||
entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_WRITE);
|
||||
|
||||
EntityManager entityManager2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent resultStudent2 = entityManager2.find(PessimisticLockingStudent.class, 1L);
|
||||
resultStudent2.setName("Change");
|
||||
entityManager2.persist(resultStudent2);
|
||||
entityManager2.getTransaction()
|
||||
.commit();
|
||||
|
||||
entityManager.close();
|
||||
entityManager2.close();
|
||||
} catch (Exception e) {
|
||||
Assert.isTrue(e instanceof PessimisticLockException);
|
||||
}
|
||||
}
|
||||
|
||||
protected static EntityManager getEntityManagerWithOpenTransaction() throws IOException {
|
||||
String propertyFileName = "hibernate-pessimistic-locking.properties";
|
||||
EntityManager entityManager = HibernateUtil.getSessionFactory(propertyFileName)
|
||||
.openSession();
|
||||
entityManager.getTransaction()
|
||||
.begin();
|
||||
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
package com.baeldung.hibernate.pessimisticlocking;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.LockModeType;
|
||||
import javax.persistence.PessimisticLockScope;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PessimisticLockScopesIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "JOHN", "SMITH", new BigDecimal(4.5));
|
||||
em.persist(employee);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingEmployee foundEmployee = em2.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundEmployee = em3.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em3.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEntityWithElementCollection_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
Address address = new Address("Poland", "Warsaw");
|
||||
Customer customer = new Customer(1L, "JOE", "DOE", Arrays.asList(address));
|
||||
em.persist(customer);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEntityWithOneToMany_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE");
|
||||
PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student);
|
||||
student.setCourses(Arrays.asList(course));
|
||||
em.persist(course);
|
||||
em.persist(student);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
PessimisticLockingCourse foundCourse = em2.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundCourse = em3.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em3.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
protected EntityManager getEntityManagerWithOpenTransaction() throws IOException {
|
||||
String propertyFileName = "hibernate-pessimistic-locking.properties";
|
||||
EntityManager entityManager = HibernateUtil.getSessionFactory(propertyFileName)
|
||||
.openSession();
|
||||
entityManager.getTransaction()
|
||||
.begin();
|
||||
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
hibernate.connection.driver_class=org.h2.Driver
|
||||
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=100;MVCC=FALSE
|
||||
hibernate.connection.username=sa
|
||||
hibernate.connection.autocommit=true
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.eclipselink.springdata.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
|
||||
private String country;
|
||||
private String city;
|
||||
|
||||
public Address(String country, String city) {
|
||||
this.country = country;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.eclipselink.springdata.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Course {
|
||||
|
||||
@Id
|
||||
private Long courseId;
|
||||
private String name;
|
||||
@ManyToOne
|
||||
@JoinTable(name = "student_course")
|
||||
private Student student;
|
||||
|
||||
public Course(Long courseId, String name, Student student) {
|
||||
this.courseId = courseId;
|
||||
this.name = name;
|
||||
this.student = student;
|
||||
}
|
||||
|
||||
public Course() {
|
||||
}
|
||||
|
||||
public Long getCourseId() {
|
||||
return courseId;
|
||||
}
|
||||
|
||||
public void setCourseId(Long courseId) {
|
||||
this.courseId = courseId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Student getStudent() {
|
||||
return student;
|
||||
}
|
||||
|
||||
public void setStudent(Student students) {
|
||||
this.student = students;
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.baeldung.eclipselink.springdata.pessimisticlocking;
|
||||
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
private Long customerId;
|
||||
private String name;
|
||||
private String lastName;
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "customer_address")
|
||||
private List<Address> addressList;
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
public Customer(Long customerId, String name, String lastName, List<Address> addressList) {
|
||||
this.customerId = customerId;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
this.addressList = addressList;
|
||||
}
|
||||
|
||||
public Long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(Long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List<Address> getAddressList() {
|
||||
return addressList;
|
||||
}
|
||||
|
||||
public void setAddressList(List<Address> addressList) {
|
||||
this.addressList = addressList;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.eclipselink.springdata.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
public class Employee extends Individual {
|
||||
|
||||
private BigDecimal salary;
|
||||
|
||||
public Employee(Long id, String name, String lastName, BigDecimal salary) {
|
||||
super(id, name, lastName);
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public Employee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BigDecimal getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(BigDecimal average) {
|
||||
this.salary = average;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.eclipselink.springdata.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
public class Individual {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
private String lastName;
|
||||
|
||||
public Individual(Long id, String name, String lastName) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Individual() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.eclipselink.springdata.pessimisticlocking;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Student {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "student")
|
||||
private List<Course> courses;
|
||||
|
||||
public Student(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Course> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<Course> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
spring.datasource.url=jdbc:h2:mem:test
|
||||
spring.datasource.url=jdbc:h2:mem:test;MVCC=FALSE;LOCK_TIMEOUT=100;
|
||||
spring.jpa.show-sql=true
|
@ -0,0 +1,118 @@
|
||||
package com.baeldung.eclipselink.springdata.pessimisticlocking;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
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 javax.persistence.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class PessimisticLockScopesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EntityManagerFactory entityManagerFactory;
|
||||
|
||||
@Test
|
||||
public void givenEntityWithJoinInheritance_whenLock_thenNormalAndExtendScopesLockParentAndChildEntity() {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
Employee employee = new Employee(1L, "JOE", "DOE", new BigDecimal(4.5));
|
||||
em.persist(employee);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
Employee foundEmployee = em2.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundEmployee = em3.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em3.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEntityWithElementCollection_whenLock_thenExtendScopeLocksAlsoCollectionTable() {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
Address address = new Address("Poland", "Warsaw");
|
||||
Customer customer = new Customer(1L, "JOHN", "SMITH", Arrays.asList(address));
|
||||
em.persist(customer);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em3.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEclipseEntityWithOneToMany_whenLock_thenExtendedLockAlsoJoinTable() {
|
||||
EntityManager em = getEntityManagerWithOpenTransaction();
|
||||
Student student = new Student(1L, "JOE");
|
||||
Course course = new Course(1L, "COURSE", student);
|
||||
student.setCourses(Arrays.asList(course));
|
||||
em.persist(course);
|
||||
em.persist(student);
|
||||
em.getTransaction()
|
||||
.commit();
|
||||
em.close();
|
||||
|
||||
// NORMAL SCOPE
|
||||
EntityManager em2 = getEntityManagerWithOpenTransaction();
|
||||
Course foundCourse = em2.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE);
|
||||
em2.getTransaction()
|
||||
.rollback();
|
||||
|
||||
// EXTENDED SCOPE
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
|
||||
|
||||
EntityManager em3 = getEntityManagerWithOpenTransaction();
|
||||
foundCourse = em3.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map);
|
||||
em3.getTransaction()
|
||||
.rollback();
|
||||
|
||||
em2.close();
|
||||
em3.close();
|
||||
}
|
||||
|
||||
protected EntityManager getEntityManagerWithOpenTransaction() {
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction()
|
||||
.begin();
|
||||
return entityManager;
|
||||
}
|
||||
}
|
1
pom.xml
1
pom.xml
@ -152,6 +152,7 @@
|
||||
<module>spring-data-couchbase-2</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>spring-data-elasticsearch</module>
|
||||
<module>spring-data-keyvalue</module>
|
||||
<module>spring-data-mongodb</module>
|
||||
<module>persistence-modules/spring-data-neo4j</module>
|
||||
<module>persistence-modules/spring-data-redis</module>
|
||||
|
37
spring-data-keyvalue/pom.xml
Normal file
37
spring-data-keyvalue/pom.xml
Normal file
@ -0,0 +1,37 @@
|
||||
<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.baeldung</groupId>
|
||||
<artifactId>spring-data-keyvalue</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
<relativePath />
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spring-data-keyvalue.version>2.0.3.RELEASE</spring-data-keyvalue.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-keyvalue</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.spring.data.keyvalue;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.keyvalue.core.KeyValueAdapter;
|
||||
import org.springframework.data.keyvalue.core.KeyValueOperations;
|
||||
import org.springframework.data.keyvalue.core.KeyValueTemplate;
|
||||
import org.springframework.data.map.MapKeyValueAdapter;
|
||||
|
||||
@Configuration
|
||||
public class Configurations {
|
||||
|
||||
//To be used only if @EnableMapRepositories is not used.
|
||||
//Else @EnableMapRepositories gives us a template as well.
|
||||
@Bean("keyValueTemplate")
|
||||
public KeyValueOperations keyValueTemplate() {
|
||||
return new KeyValueTemplate(keyValueAdapter());
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KeyValueAdapter keyValueAdapter() {
|
||||
return new MapKeyValueAdapter(ConcurrentHashMap.class);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.data.keyvalue;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.map.repository.config.EnableMapRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableMapRepositories
|
||||
public class SpringDataKeyValueApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDataKeyValueApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.spring.data.keyvalue.repositories;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.spring.data.keyvalue.vo.Employee;
|
||||
|
||||
@Repository("employeeRepository")
|
||||
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring.data.keyvalue.services;
|
||||
|
||||
import com.baeldung.spring.data.keyvalue.vo.Employee;
|
||||
|
||||
public interface EmployeeService {
|
||||
|
||||
void save(Employee employee);
|
||||
|
||||
Employee get(Integer id);
|
||||
|
||||
Iterable<Employee> fetchAll();
|
||||
|
||||
void update(Employee employee);
|
||||
|
||||
void delete(Integer id);
|
||||
|
||||
Iterable<Employee> getSortedListOfEmployeesBySalary();
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.baeldung.spring.data.keyvalue.services.impl;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.keyvalue.core.KeyValueTemplate;
|
||||
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.spring.data.keyvalue.services.EmployeeService;
|
||||
import com.baeldung.spring.data.keyvalue.vo.Employee;
|
||||
|
||||
@Service("employeeServicesWithKeyValueTemplate")
|
||||
@DependsOn("keyValueTemplate")
|
||||
public class EmployeeServicesWithKeyValueTemplate implements EmployeeService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("keyValueTemplate")
|
||||
KeyValueTemplate keyValueTemplate;
|
||||
|
||||
@Override
|
||||
public void save(Employee employee) {
|
||||
keyValueTemplate.insert(employee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee get(Integer id) {
|
||||
Optional<Employee> employee = keyValueTemplate.findById(id, Employee.class);
|
||||
return employee.isPresent() ? employee.get() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Employee> fetchAll() {
|
||||
return keyValueTemplate.findAll(Employee.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Employee employee) {
|
||||
keyValueTemplate.update(employee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Integer id) {
|
||||
keyValueTemplate.delete(id, Employee.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Employee> getSortedListOfEmployeesBySalary() {
|
||||
KeyValueQuery query = new KeyValueQuery();
|
||||
query.setSort(new Sort(Sort.Direction.DESC, "salary"));
|
||||
return keyValueTemplate.find(query, Employee.class);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.spring.data.keyvalue.services.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.spring.data.keyvalue.repositories.EmployeeRepository;
|
||||
import com.baeldung.spring.data.keyvalue.services.EmployeeService;
|
||||
import com.baeldung.spring.data.keyvalue.vo.Employee;
|
||||
|
||||
@Service("employeeServicesWithRepository")
|
||||
public class EmployeeServicesWithRepository implements EmployeeService {
|
||||
|
||||
@Autowired
|
||||
EmployeeRepository employeeRepository;
|
||||
|
||||
|
||||
@Override
|
||||
public void save(Employee employee) {
|
||||
employeeRepository.save(employee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Employee> fetchAll() {
|
||||
return employeeRepository.findAll();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee get(Integer id) {
|
||||
return employeeRepository.findById(id).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Employee employee) {
|
||||
employeeRepository.save(employee);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Integer id) {
|
||||
employeeRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public Iterable<Employee> getSortedListOfEmployeesBySalary() {
|
||||
throw new RuntimeException("Method not supported by CRUDRepository");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.baeldung.spring.data.keyvalue.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.keyvalue.annotation.KeySpace;
|
||||
|
||||
@KeySpace("employees")
|
||||
public class Employee implements Serializable {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String department;
|
||||
|
||||
private String salary;
|
||||
|
||||
public Employee(Integer id, String name, String department, String salary) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.department = department;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
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 getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void setDepartment(String department) {
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public String getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(String salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", department='" + department + '\'' +
|
||||
", salary='" + salary + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package com.baeldung.spring.data.keyvalue.services.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.keyvalue.core.KeyValueTemplate;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.data.keyvalue.SpringDataKeyValueApplication;
|
||||
import com.baeldung.spring.data.keyvalue.services.EmployeeService;
|
||||
import com.baeldung.spring.data.keyvalue.vo.Employee;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringDataKeyValueApplication.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class EmployeeServicesWithKeyValueTemplateTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("employeeServicesWithKeyValueTemplate")
|
||||
EmployeeService employeeService;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("keyValueTemplate")
|
||||
KeyValueTemplate keyValueTemplate;
|
||||
|
||||
static Employee employee1;
|
||||
|
||||
static Employee employee2;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
employee1 = new Employee(1, "Karan", "IT", "5000");
|
||||
employee2 = new Employee(2, "Jack", "HR", "2000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1_whenEmployeeSaved_thenEmployeeIsAddedToMap() {
|
||||
employeeService.save(employee1);
|
||||
assertEquals(keyValueTemplate.findById(1, Employee.class).get(), employee1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() {
|
||||
Employee employeeFetched = employeeService.get(1);
|
||||
assertEquals(employeeFetched, employee1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3_whenEmployeesFetched_thenEmployeesAreReturnedFromMap() {
|
||||
List<Employee> employees = (List<Employee>)employeeService.fetchAll();
|
||||
assertEquals(employees.size(), 1);
|
||||
assertEquals(employees.get(0), employee1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test4_whenEmployeeUpdated_thenEmployeeIsUpdatedToMap() {
|
||||
employee1.setName("Pawan");
|
||||
employeeService.update(employee1);
|
||||
assertEquals(keyValueTemplate.findById(1, Employee.class).get().getName(),"Pawan");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test5_whenSortedEmployeesFetched_thenEmployeesAreReturnedFromMapInOrder() {
|
||||
employeeService.save(employee2);
|
||||
List<Employee> employees = (List<Employee>)employeeService.getSortedListOfEmployeesBySalary();
|
||||
assertEquals(employees.size(), 2);
|
||||
assertEquals(employees.get(0), employee1);
|
||||
assertEquals(employees.get(1), employee2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test6_whenEmployeeDeleted_thenEmployeeIsRemovedMap() {
|
||||
employeeService.delete(1);
|
||||
assertEquals(keyValueTemplate.findById(1, Employee.class).isPresent(), false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.baeldung.spring.data.keyvalue.services.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.data.keyvalue.SpringDataKeyValueApplication;
|
||||
import com.baeldung.spring.data.keyvalue.repositories.EmployeeRepository;
|
||||
import com.baeldung.spring.data.keyvalue.services.EmployeeService;
|
||||
import com.baeldung.spring.data.keyvalue.vo.Employee;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringDataKeyValueApplication.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class EmployeeServicesWithRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("employeeServicesWithRepository")
|
||||
EmployeeService employeeService;
|
||||
|
||||
@Autowired
|
||||
EmployeeRepository employeeRepository;
|
||||
|
||||
static Employee employee1;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
employee1 = new Employee(1, "Karan", "IT", "5000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1_whenEmployeeSaved_thenEmployeeIsAddedToMap() {
|
||||
employeeService.save(employee1);
|
||||
assertEquals(employeeRepository.findById(1).get(), employee1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() {
|
||||
Employee employeeFetched = employeeService.get(1);
|
||||
assertEquals(employeeFetched, employee1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3_whenEmployeesFetched_thenEmployeesAreReturnedFromMap() {
|
||||
List<Employee> employees = (List<Employee>)employeeService.fetchAll();
|
||||
assertEquals(employees.size(), 1);
|
||||
assertEquals(employees.get(0), employee1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test4_whenEmployeeUpdated_thenEmployeeIsUpdatedToMap() {
|
||||
employee1.setName("Pawan");
|
||||
employeeService.update(employee1);
|
||||
assertEquals(employeeRepository.findById(1).get().getName(),"Pawan");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test5_whenEmployeeDeleted_thenEmployeeIsRemovedMap() {
|
||||
employeeService.delete(1);
|
||||
assertEquals(employeeRepository.findById(1).isPresent(), false);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Required;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
|
||||
@DependsOn
|
||||
public class Bike implements Vehicle {
|
||||
|
||||
private String color;
|
||||
|
||||
@Required
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Biker {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("bike")
|
||||
private Vehicle vehicle;
|
||||
|
||||
@Autowired
|
||||
public Biker(@Qualifier("bike") Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setVehicle(@Qualifier("bike") Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
@DependsOn("engine")
|
||||
public class Car implements Vehicle {
|
||||
|
||||
@Autowired
|
||||
private Engine engine;
|
||||
|
||||
@Autowired
|
||||
public Car(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setEngine(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public Engine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Lazy
|
||||
public class CarMechanic {
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CarUtility {
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Driver {
|
||||
|
||||
@Autowired
|
||||
private Vehicle vehicle;
|
||||
|
||||
@Autowired
|
||||
public Driver(Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setVehicle(Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
public Vehicle getVehicle() {
|
||||
return vehicle;
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = 10000)
|
||||
@Scheduled(cron = "0 * * * * MON-FRI")
|
||||
public void checkVehicle() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
public class Engine {
|
||||
|
||||
@Value("8")
|
||||
private int cylinderCount;
|
||||
|
||||
@Value("${engine.fuelType}")
|
||||
private String fuelType;
|
||||
|
||||
public Engine() {
|
||||
this(8);
|
||||
}
|
||||
|
||||
public Engine(@Value("8") int cylinderCount) {
|
||||
this.cylinderCount = cylinderCount;
|
||||
}
|
||||
|
||||
@Value("8")
|
||||
public void setCylinderCount(int cylinderCount) {
|
||||
this.cylinderCount = cylinderCount;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
public interface Vehicle {
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@Controller
|
||||
@RequestMapping(value = "/vehicles", method = RequestMethod.GET)
|
||||
public class VehicleController {
|
||||
|
||||
@CrossOrigin
|
||||
@ResponseBody
|
||||
@RequestMapping("/hello")
|
||||
public String hello() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
@RequestMapping("/home")
|
||||
public String home() {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public void saveVehicle(@RequestBody Vehicle vehicle) {
|
||||
}
|
||||
|
||||
@RequestMapping("/{id}")
|
||||
public Vehicle getVehicle(@PathVariable("id") long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping
|
||||
public Vehicle getVehicleByParam(@RequestParam("id") long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping("/buy")
|
||||
public Car buyCar(@RequestParam(defaultValue = "5") int seatCount) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public void onIllegalArgumentException(IllegalArgumentException exception) {
|
||||
}
|
||||
|
||||
@PostMapping("/assemble")
|
||||
public void assembleVehicle(@ModelAttribute("vehicle") Vehicle vehicle) {
|
||||
}
|
||||
|
||||
@ModelAttribute("vehicle")
|
||||
public Vehicle getVehicle() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
// @SpringBootApplication
|
||||
public class VehicleFactoryApplication {
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// SpringApplication.run(VehicleFactoryApplication.class, args);
|
||||
// }
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.annotations")
|
||||
@ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
|
||||
@ImportResource("classpath:/annotations.xml")
|
||||
@PropertySource("classpath:/annotations.properties")
|
||||
@Lazy
|
||||
@EnableAutoConfiguration
|
||||
@EnableAsync
|
||||
@EnableScheduling
|
||||
public class VehicleFactoryConfig {
|
||||
|
||||
@Bean
|
||||
@Lazy(false)
|
||||
public Engine engine() {
|
||||
return new Engine();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class VehicleRepository {
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class VehicleRestController {
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class VehicleService {
|
||||
|
||||
@Async
|
||||
public void repairCar() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
engine.fuelType=petrol
|
9
spring-mvc-java/src/main/resources/annotations.xml
Normal file
9
spring-mvc-java/src/main/resources/annotations.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean class="com.baeldung.annotations.Bike" name="bike">
|
||||
<property name="color" value="green" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
@ -7,6 +7,12 @@ public class LoginForm {
|
||||
public LoginForm() {
|
||||
}
|
||||
|
||||
public LoginForm(String username, String password) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package org.baeldung.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.interceptors.RestTemplateLoggingInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestClientConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
|
||||
|
||||
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
|
||||
if (CollectionUtils.isEmpty(interceptors)) {
|
||||
interceptors = new ArrayList<ClientHttpRequestInterceptor>();
|
||||
}
|
||||
interceptors.add(new RestTemplateLoggingInterceptor());
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
return restTemplate;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.baeldung.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor {
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
||||
logRequest(body);
|
||||
ClientHttpResponse response = execution.execute(request, body);
|
||||
logResponse(response);
|
||||
response.getHeaders().add("Foo", "bar");
|
||||
return response;
|
||||
}
|
||||
|
||||
private void logRequest(byte[] body) {
|
||||
String payLoad = StringUtils.EMPTY;
|
||||
if (body.length > 0) {
|
||||
payLoad = new String(body, StandardCharsets.UTF_8);
|
||||
}
|
||||
System.out.println("Request Body > " + payLoad);
|
||||
}
|
||||
|
||||
private void logResponse(ClientHttpResponse response) throws IOException {
|
||||
String payLoad = StringUtils.EMPTY;
|
||||
long contentLength = response.getHeaders()
|
||||
.getContentLength();
|
||||
if (contentLength != 0) {
|
||||
payLoad = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
|
||||
}
|
||||
System.out.println("Response Body > " + payLoad);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.baeldung.resttemplate;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.baeldung.config.RestClientConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RestClientConfig.class)
|
||||
public class RestTemplateIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenRestTemplate_whenRequested_thenLogAndModifyResponse() {
|
||||
LoginForm loginForm = new LoginForm("userName", "password");
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<LoginForm> requestEntity = new HttpEntity<LoginForm>(loginForm, headers);
|
||||
|
||||
ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://httpbin.org/post", requestEntity, String.class);
|
||||
|
||||
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
|
||||
assertThat(responseEntity.getHeaders()
|
||||
.get("Foo")
|
||||
.get(0), is(equalTo("bar")));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user