Merge branch 'masterEugen'
This commit is contained in:
@@ -39,3 +39,4 @@ This tutorials project is being built **[>> HERE](https://rest-security.ci.cloud
|
||||
|
||||
- [Apache Maven Standard Directory Layout](http://www.baeldung.com/maven-directory-structure)
|
||||
- [Apache Maven Tutorial](http://www.baeldung.com/maven)
|
||||
- [Designing a User Friendly Java Library](http://www.baeldung.com/design-a-user-friendly-java-library)
|
||||
|
||||
+1
-3
@@ -1,10 +1,8 @@
|
||||
package com.baeldung.linkedlist;
|
||||
package com.baeldung.algorithms.middleelementlookup;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.baeldung.linkedlist.Node;
|
||||
|
||||
public class MiddleElementLookup {
|
||||
|
||||
public static Optional<String> findMiddleElementLinkedList(LinkedList<String> linkedList) {
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.linkedlist;
|
||||
package com.baeldung.algorithms.middleelementlookup;
|
||||
|
||||
public class Node {
|
||||
private Node next;
|
||||
+6
-4
@@ -1,11 +1,13 @@
|
||||
package com.baeldung.linkedlist;
|
||||
package algorithms;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import com.baeldung.algorithms.middleelementlookup.MiddleElementLookup;
|
||||
import com.baeldung.algorithms.middleelementlookup.Node;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class MiddleElementLookupUnitTest {
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package com.baeldung.algorithms.analysis;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnalysisRunnerLiveTest {
|
||||
|
||||
int n = 10;
|
||||
int total = 0;
|
||||
|
||||
@Test
|
||||
public void whenConstantComplexity_thenConstantRuntime() {
|
||||
|
||||
System.out.println("**** n = " + n + " ****");
|
||||
System.out.println();
|
||||
|
||||
// Constant Time
|
||||
System.out.println("**** Constant time ****");
|
||||
|
||||
System.out.println("Hey - your input is: " + n);
|
||||
System.out.println("Running time not dependent on input size!");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLogarithmicComplexity_thenLogarithmicRuntime() {
|
||||
// Logarithmic Time
|
||||
System.out.println("**** Logarithmic Time ****");
|
||||
for (int i = 1; i < n; i = i * 2) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLinearComplexity_thenLinearRuntime() {
|
||||
// Linear Time
|
||||
System.out.println("**** Linear Time ****");
|
||||
for (int i = 0; i < n; i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNLogNComplexity_thenNLogNRuntime() {
|
||||
// N Log N Time
|
||||
System.out.println("**** nlogn Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j < n; j = j * 2) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenQuadraticComplexity_thenQuadraticRuntime() {
|
||||
// Quadratic Time
|
||||
System.out.println("**** Quadratic Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCubicComplexity_thenCubicRuntime() {
|
||||
// Cubic Time
|
||||
System.out.println("**** Cubic Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
for (int k = 1; k <= n; k++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j + " and " + k);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExponentialComplexity_thenExponentialRuntime() {
|
||||
// Exponential Time
|
||||
System.out.println("**** Exponential Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= Math.pow(2, n); i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorialComplexity_thenFactorialRuntime() {
|
||||
// Factorial Time
|
||||
System.out.println("**** Factorial Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <=
|
||||
|
||||
factorial(n); i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
}
|
||||
|
||||
static int factorial(int n) {
|
||||
if (n == 0 || n == 1)
|
||||
return 1;
|
||||
else
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
}
|
||||
@@ -52,4 +52,4 @@
|
||||
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
|
||||
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
|
||||
- [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table)
|
||||
|
||||
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
|
||||
|
||||
@@ -38,7 +38,7 @@ public class UnsignedArithmeticUnitTest {
|
||||
assertEquals(1, Integer.divideUnsigned(negative, positive));
|
||||
|
||||
assertEquals(-1, negative % positive);
|
||||
assertEquals(1, Integer.divideUnsigned(negative, positive));
|
||||
assertEquals(1, Integer.remainderUnsigned(negative, positive));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -25,3 +25,4 @@
|
||||
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
|
||||
- [A Guide to Java 9 Modularity](http://www.baeldung.com/java-9-modularity)
|
||||
- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional)
|
||||
- [Java 9 java.lang.Module API](http://www.baeldung.com/java-9-module-api)
|
||||
|
||||
@@ -156,3 +156,7 @@
|
||||
- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight)
|
||||
- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern)
|
||||
- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern)
|
||||
- [The Thread.join() Method in Java](http://www.baeldung.com/java-thread-join)
|
||||
- [Guide to the super Java Keyword](http://www.baeldung.com/java-super)
|
||||
- [Guide to the this Java Keyword](http://www.baeldung.com/java-this)
|
||||
- [Jagged Arrays In Java](http://www.baeldung.com/java-jagged-arrays)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
public class Public {
|
||||
public Public() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in the same package or subclass.
|
||||
SuperPublic.defaultMethod(); // Available in the same package.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
public class SubClass extends SuperPublic {
|
||||
public SubClass() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in the same package or subclass.
|
||||
SuperPublic.defaultMethod(); // Available in the same package.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
//Only public or default access modifiers are permitted
|
||||
public class SuperPublic {
|
||||
// Always available from anywhere
|
||||
static public void publicMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " publicMethod()");
|
||||
}
|
||||
|
||||
// Available within the same package
|
||||
static void defaultMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " defaultMethod()");
|
||||
}
|
||||
|
||||
// Available within the same package and subclasses
|
||||
static protected void protectedMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " protectedMethod()");
|
||||
}
|
||||
|
||||
// Available within the same class only
|
||||
static private void privateMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " privateMethod()");
|
||||
}
|
||||
|
||||
// Method in the same class = has access to all members within the same class
|
||||
private void anotherPrivateMethod() {
|
||||
privateMethod();
|
||||
defaultMethod();
|
||||
protectedMethod();
|
||||
publicMethod(); // Available in the same class only.
|
||||
}
|
||||
}
|
||||
|
||||
// Only public or default access modifiers are permitted
|
||||
class SuperDefault {
|
||||
public void publicMethod() {
|
||||
System.out.println(this.getClass().getName() + " publicMethod()");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherPublic {
|
||||
public AnotherPublic() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherSubClass extends SuperPublic {
|
||||
public AnotherSubClass() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in subclass. Let's note different package.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherSuperPublic {
|
||||
public AnotherSuperPublic() {
|
||||
SuperPublic.publicMethod(); // Available everywhere. Let's note different package.
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,18 @@ package com.baeldung.extension;
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Extension {
|
||||
//Instead of file name we can also specify full path of a file eg. /baeldung/com/demo/abc.java
|
||||
public String getExtensionByApacheCommonLib(String filename) {
|
||||
return FilenameUtils.getExtension(filename);
|
||||
}
|
||||
|
||||
public String getExtensionByStringHandling(String filename) {
|
||||
String fileExtension = "";
|
||||
if (filename.contains(".") && filename.lastIndexOf(".") != 0) {
|
||||
fileExtension = filename.substring(filename.lastIndexOf(".") + 1);
|
||||
}
|
||||
return fileExtension;
|
||||
public Optional<String> getExtensionByStringHandling(String filename) {
|
||||
return Optional.ofNullable(filename)
|
||||
.filter(f -> f.contains("."))
|
||||
.map(f -> f.substring(filename.lastIndexOf(".") + 1));
|
||||
}
|
||||
|
||||
public String getExtensionByGuava(String filename) {
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.baeldung.extension;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ExtensionUnitTest {
|
||||
private Extension extension = new Extension();
|
||||
|
||||
@@ -16,8 +18,9 @@ public class ExtensionUnitTest {
|
||||
@Test
|
||||
public void getExtension_whenStringHandle_thenExtensionIsTrue() {
|
||||
String expectedExtension = "java";
|
||||
String actualExtension = extension.getExtensionByStringHandling("Demo.java");
|
||||
Assert.assertEquals(expectedExtension, actualExtension);
|
||||
Optional<String> actualExtension = extension.getExtensionByStringHandling("Demo.java");
|
||||
Assert.assertTrue(actualExtension.isPresent());
|
||||
actualExtension.ifPresent(ext -> Assert.assertEquals(expectedExtension,ext));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+90
-32
@@ -9,6 +9,13 @@
|
||||
<name>deltaspike</name>
|
||||
<description>A starter Java EE 7 webapp which uses DeltaSpike</description>
|
||||
<url>http://wildfly.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache License, Version 2.0</name>
|
||||
@@ -16,12 +23,12 @@
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>redhat-repository-techpreview</id>
|
||||
<url>https://maven.repository.redhat.com/techpreview/all/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
@@ -47,6 +54,13 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.deltaspike.distribution</groupId>
|
||||
<artifactId>distributions-bom</artifactId>
|
||||
<version>${deltaspike.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -160,14 +174,12 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.deltaspike.modules</groupId>
|
||||
<artifactId>deltaspike-data-module-api</artifactId>
|
||||
<version>${deltaspike.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.deltaspike.modules</groupId>
|
||||
<artifactId>deltaspike-data-module-impl</artifactId>
|
||||
<version>${deltaspike.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -184,6 +196,71 @@
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.deltaspike.modules</groupId>
|
||||
<artifactId>deltaspike-test-control-module-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.deltaspike.modules</groupId>
|
||||
<artifactId>deltaspike-test-control-module-impl</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.deltaspike.cdictrl</groupId>
|
||||
<artifactId>deltaspike-cdictrl-weld</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.weld.se</groupId>
|
||||
<artifactId>weld-se-core</artifactId>
|
||||
<version>${weld.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss</groupId>
|
||||
<artifactId>jandex</artifactId>
|
||||
<version>1.2.5.Final-redhat-1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.197</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Needed for running tests (you may also use TestNG) -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Others -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.5</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -226,28 +303,6 @@
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<!-- The default profile skips all tests, though you can tune it to run
|
||||
just unit tests based on a custom pattern -->
|
||||
<!-- Seperate profiles are provided for running all tests, including Arquillian
|
||||
tests that execute in the specified container -->
|
||||
<id>default</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
|
||||
<!-- An optional Arquillian testing profile that executes tests in your
|
||||
@@ -273,15 +328,18 @@
|
||||
resources, i.e. build is platform dependent! -->
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<querydsl.version>3.7.4</querydsl.version>
|
||||
<deltaspike.version>1.7.2</deltaspike.version>
|
||||
<deltaspike.version>1.8.2</deltaspike.version>
|
||||
<!-- JBoss dependency versions -->
|
||||
<wildfly.maven.plugin.version>1.0.2.Final</wildfly.maven.plugin.version>
|
||||
<!-- Define the version of the JBoss BOMs we want to import to specify
|
||||
tested stacks. -->
|
||||
<jboss.bom.version>8.2.2.Final</jboss.bom.version>
|
||||
<jboss.bom.version>8.2.1.Final</jboss.bom.version>
|
||||
<weld.version>2.1.2.Final</weld.version>
|
||||
<!-- other plugin versions -->
|
||||
<war.plugin.version>2.6</war.plugin.version>
|
||||
<apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
*/
|
||||
package baeldung.controller;
|
||||
|
||||
import baeldung.model.Member;
|
||||
import baeldung.service.MemberRegistration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.enterprise.inject.Model;
|
||||
import javax.enterprise.inject.Produces;
|
||||
@@ -24,9 +27,6 @@ import javax.faces.context.FacesContext;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import baeldung.model.Member;
|
||||
import baeldung.service.MemberRegistration;
|
||||
|
||||
// The @Model stereotype is a convenience mechanism to make this a request-scoped bean that has an
|
||||
// EL name
|
||||
// Read more about the @Model stereotype in this FAQ:
|
||||
@@ -34,11 +34,9 @@ import baeldung.service.MemberRegistration;
|
||||
@Model
|
||||
public class MemberController {
|
||||
|
||||
@Inject
|
||||
private FacesContext facesContext;
|
||||
@Inject private FacesContext facesContext;
|
||||
|
||||
@Inject
|
||||
private MemberRegistration memberRegistration;
|
||||
@Inject private MemberRegistration memberRegistration;
|
||||
|
||||
@Produces
|
||||
@Named
|
||||
|
||||
@@ -1,29 +1,18 @@
|
||||
package baeldung.data;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.enterprise.inject.Default;
|
||||
import javax.enterprise.inject.Disposes;
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.PersistenceUnit;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EntityManagerProducer {
|
||||
@PersistenceUnit(unitName = "primary")
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
|
||||
@Produces
|
||||
@Default
|
||||
@PersistenceContext(unitName = "primary") private EntityManager entityManager;
|
||||
|
||||
@RequestScoped
|
||||
@Produces
|
||||
public EntityManager create() {
|
||||
return this.entityManagerFactory.createEntityManager();
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
public void dispose(@Disposes @Default EntityManager entityManager) {
|
||||
if (entityManager.isOpen()) {
|
||||
entityManager.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@
|
||||
*/
|
||||
package baeldung.data;
|
||||
|
||||
import baeldung.model.Member;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.enterprise.event.Observes;
|
||||
@@ -25,13 +27,10 @@ import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import java.util.List;
|
||||
|
||||
import baeldung.model.Member;
|
||||
|
||||
@RequestScoped
|
||||
public class MemberListProducer {
|
||||
|
||||
@Inject
|
||||
private MemberRepository memberRepository;
|
||||
@Inject private MemberRepository memberRepository;
|
||||
|
||||
private List<Member> members;
|
||||
|
||||
|
||||
@@ -18,7 +18,10 @@ package baeldung.data;
|
||||
|
||||
import baeldung.model.Member;
|
||||
import baeldung.model.QMember;
|
||||
import org.apache.deltaspike.data.api.*;
|
||||
import org.apache.deltaspike.data.api.AbstractEntityRepository;
|
||||
import org.apache.deltaspike.data.api.EntityManagerConfig;
|
||||
import org.apache.deltaspike.data.api.Query;
|
||||
import org.apache.deltaspike.data.api.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -35,6 +38,9 @@ public abstract class MemberRepository extends AbstractEntityRepository<Member,
|
||||
|
||||
public List<Member> findAllOrderedByNameWithQueryDSL() {
|
||||
final QMember member = QMember.member;
|
||||
return jpaQuery().from(member).orderBy(member.email.asc()).list(member);
|
||||
return jpaQuery()
|
||||
.from(member)
|
||||
.orderBy(member.email.asc())
|
||||
.list(member);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ import javax.inject.Inject;
|
||||
|
||||
public class QueryDslRepositoryExtension<E> implements QueryDslSupport, DelegateQueryHandler {
|
||||
|
||||
@Inject
|
||||
private QueryInvocationContext context;
|
||||
@Inject private QueryInvocationContext context;
|
||||
|
||||
@Override
|
||||
public JPAQuery jpaQuery() {
|
||||
|
||||
@@ -2,29 +2,20 @@ package baeldung.data;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.enterprise.inject.Default;
|
||||
import javax.enterprise.inject.Disposes;
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.PersistenceUnit;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SecondaryEntityManagerProducer {
|
||||
@PersistenceUnit(unitName = "secondary")
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
|
||||
@PersistenceContext(unitName = "secondary") private EntityManager entityManager;
|
||||
|
||||
@Produces
|
||||
@Default
|
||||
@RequestScoped
|
||||
@SecondaryPersistenceUnit
|
||||
public EntityManager create() {
|
||||
return this.entityManagerFactory.createEntityManager();
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
public void dispose(@Disposes @Default EntityManager entityManager) {
|
||||
if (entityManager.isOpen()) {
|
||||
entityManager.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package baeldung.data;
|
||||
|
||||
import baeldung.model.User;
|
||||
import org.apache.deltaspike.data.api.FirstResult;
|
||||
import org.apache.deltaspike.data.api.MaxResults;
|
||||
import org.apache.deltaspike.data.api.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@Repository(forEntity = User.class)
|
||||
public abstract class SimpleUserRepository {
|
||||
public abstract Collection<User> findAll();
|
||||
|
||||
public abstract Collection<User> findAllOrderByFirstNameAsc(@FirstResult int start, @MaxResults int size);
|
||||
|
||||
public abstract Collection<User> findTop2OrderByFirstNameAsc();
|
||||
|
||||
public abstract Collection<User> findFirst2OrderByFirstNameAsc();
|
||||
|
||||
public abstract List<User> findAllOrderByFirstNameAsc();
|
||||
|
||||
public abstract List<User> findAllOrderByFirstNameAscLastNameDesc();
|
||||
|
||||
public abstract User findById(Long id);
|
||||
|
||||
public abstract Collection<User> findByFirstName(String firstName);
|
||||
|
||||
public abstract User findAnyByLastName(String lastName);
|
||||
|
||||
public abstract Collection<User> findAnyByFirstName(String firstName);
|
||||
|
||||
public abstract Collection<User> findByFirstNameAndLastName(String firstName, String lastName);
|
||||
|
||||
public abstract Collection<User> findByFirstNameOrLastName(String firstName, String lastName);
|
||||
|
||||
public abstract Collection<User> findByAddress_city(String city);
|
||||
|
||||
public abstract int count();
|
||||
|
||||
public abstract void remove(User user);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package baeldung.data;
|
||||
|
||||
import baeldung.model.User;
|
||||
import org.apache.deltaspike.data.api.AbstractEntityRepository;
|
||||
import org.apache.deltaspike.data.api.Query;
|
||||
import org.apache.deltaspike.data.api.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@Repository
|
||||
public abstract class UserRepository extends AbstractEntityRepository<User, Long> {
|
||||
|
||||
public List<User> findByFirstName(String firstName) {
|
||||
return typedQuery("select u from User u where u.firstName = ?1")
|
||||
.setParameter(1, firstName)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public abstract List<User> findByLastName(String lastName);
|
||||
|
||||
@Query("select u from User u where u.firstName = ?1")
|
||||
public abstract Collection<User> findUsersWithFirstName(String firstName);
|
||||
|
||||
@Query(value = "select * from User where firstName = ?1", isNative = true)
|
||||
public abstract Collection<User> findUsersWithFirstNameNative(String firstName);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package baeldung.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@Entity
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String street;
|
||||
private String city;
|
||||
private String postCode;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getPostCode() {
|
||||
return postCode;
|
||||
}
|
||||
|
||||
public void setPostCode(String postCode) {
|
||||
this.postCode = postCode;
|
||||
}
|
||||
}
|
||||
@@ -16,22 +16,16 @@
|
||||
*/
|
||||
package baeldung.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import org.hibernate.validator.constraints.Email;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.Digits;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import javax.validation.constraints.Size;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import org.hibernate.validator.constraints.Email;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import java.io.Serializable;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@Entity
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package baeldung.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
@OneToOne private Address address;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
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 Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">
|
||||
</beans>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user