Repository: deltaspike Updated Branches: refs/heads/master 42a7ddf01 -> 1054cd83c
DELTASPIKE-1165 Added support for Streams within a repository. Added logic to handle Optional and Stream for delegate queries. New Java 8 test module for testing with Java 8 features. Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/1054cd83 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/1054cd83 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/1054cd83 Branch: refs/heads/master Commit: 1054cd83c64e016245304b9965f3495fceb48f14 Parents: 42a7ddf Author: John D. Ament <[email protected]> Authored: Sun Jun 5 00:43:24 2016 -0400 Committer: John D. Ament <[email protected]> Committed: Mon Jun 6 20:57:23 2016 -0400 ---------------------------------------------------------------------- .../deltaspike/core/util/OptionalUtil.java | 5 +- .../apache/deltaspike/core/util/StreamUtil.java | 77 ++++++ .../deltaspike/core/util/ClassUtilsTest.java | 64 +++++ .../deltaspike/core/util/StreamUtilTest.java | 61 +++++ .../data/impl/builder/DelegateQueryBuilder.java | 17 +- .../builder/result/QueryProcessorFactory.java | 19 ++ .../impl/handler/EntityRepositoryHandler.java | 14 + deltaspike/modules/data/pom.xml | 9 + deltaspike/modules/data/test-java8/pom.xml | 257 +++++++++++++++++++ .../data/test/java8/entity/Simple.java | 139 ++++++++++ .../data/test/java8/repo/SimpleRepository.java | 38 +++ .../data/test/java8/repo/SimpleRepository2.java | 34 +++ .../data/test/java8/test/Java8Test.java | 150 +++++++++++ .../test/java8/util/EntityManagerProducer.java | 30 +++ .../data/test/java8/util/TestDeployments.java | 62 +++++ .../resources-glassfish/test-persistence.xml | 30 +++ .../test/resources-openejb/test-persistence.xml | 31 +++ .../resources-weblogic/test-persistence.xml | 31 +++ .../test/resources-wildfly/test-persistence.xml | 28 ++ documentation/src/main/asciidoc/data.adoc | 23 ++ 20 files changed, 1116 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/OptionalUtil.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/OptionalUtil.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/OptionalUtil.java index ada1ef2..c906af4 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/OptionalUtil.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/OptionalUtil.java @@ -30,7 +30,8 @@ public abstract class OptionalUtil private static Class<?> optionalClass; private static Method optionalMethod; - static { + static + { try { optionalClass = Class.forName("java.util.Optional"); @@ -51,7 +52,7 @@ public abstract class OptionalUtil public static boolean isOptionalReturned(Method method) { - return optionalSupported && method.getReturnType().isAssignableFrom(optionalClass); + return optionalSupported && optionalClass.isAssignableFrom(method.getReturnType()); } public static Object wrap(Object input) http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/StreamUtil.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/StreamUtil.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/StreamUtil.java new file mode 100644 index 0000000..97314ae --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/StreamUtil.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.core.util; + +import javax.enterprise.inject.Typed; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collection; + +@Typed() +public abstract class StreamUtil +{ + private static boolean streamSupported = true; + private static Class<?> streamClass; + private static Method streamMethod; + + static + { + try + { + streamClass = Class.forName("java.util.stream.Stream"); + streamMethod = Collection.class.getMethod("stream"); + } + catch (Exception e) + { + streamSupported = false; + streamClass = null; + streamMethod = null; + } + } + + public static boolean isStreamSupported() + { + return streamSupported; + } + + public static boolean isStreamReturned(Method method) + { + return isStreamSupported() && streamClass.isAssignableFrom(method.getReturnType()); + } + + public static Object wrap(Object input) + { + if (!isStreamSupported() || input == null) + { + return input; + } + try + { + return streamMethod.invoke(input); + } + catch (IllegalAccessException e) + { + } + catch (InvocationTargetException e) + { + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/ClassUtilsTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/ClassUtilsTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/ClassUtilsTest.java new file mode 100644 index 0000000..5f288a1 --- /dev/null +++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/ClassUtilsTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.core.util; + +import junit.framework.Assert; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; + +public class ClassUtilsTest +{ + @Test + public void shouldNotRetrieveMethodBasedOnSameReturnType() throws Exception + { + Method m = InnerTwo.class.getMethod("getCollection"); + Method method = ClassUtils.extractPossiblyGenericMethod(InnerOne.class, m); + Assert.assertEquals(InnerOne.class.getMethod("getCollection"), method); + } + + @Test + public void shouldRetrieveMethodBasedOnCastableType() throws Exception + { + Method m = InnerThree.class.getMethod("getCollection"); + Method method = ClassUtils.extractPossiblyGenericMethod(InnerOne.class, m); + Assert.assertEquals(InnerOne.class.getMethod("getCollection"), method); + } + + private static class InnerOne { + public List<Integer> getCollection() { + return null; + } + } + + private static class InnerTwo { + public Collection<Integer> getCollection() { + return null; + } + } + + private static class InnerThree { + public Object getCollection() { + return null; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/StreamUtilTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/StreamUtilTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/StreamUtilTest.java new file mode 100644 index 0000000..8c49f9d --- /dev/null +++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/core/util/StreamUtilTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.core.util; + +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; + +public class StreamUtilTest +{ + @Before + public void isEnabled() + { + Assume.assumeTrue(StreamUtil.isStreamSupported()); + } + + @Test + public void shouldIdentifyStreamReturnType() throws Exception + { + Method empty = ArrayList.class.getMethod("stream"); + Assert.assertTrue(StreamUtil.isStreamReturned(empty)); + } + + @Test + public void shouldReturnEmptyWhenGivenNull() throws Exception + { + Object wrapped = StreamUtil.wrap(null); + Assert.assertNull(wrapped); + } + + @Test + public void shouldReturnAStreamWhenGivenACollection() throws Exception + { + Object wrapped = StreamUtil.wrap(Arrays.asList("a","b")); + Class<?> streamClass = Class.forName("java.util.stream.Stream"); + Assert.assertTrue(streamClass.isAssignableFrom(wrapped.getClass())); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java index c97dc14..87218b3 100644 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java @@ -20,6 +20,7 @@ package org.apache.deltaspike.data.impl.builder; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Collection; import java.util.Set; import javax.enterprise.context.ApplicationScoped; @@ -32,6 +33,8 @@ import javax.persistence.PersistenceException; import org.apache.deltaspike.core.api.provider.BeanProvider; import org.apache.deltaspike.core.util.ClassUtils; +import org.apache.deltaspike.core.util.OptionalUtil; +import org.apache.deltaspike.core.util.StreamUtil; import org.apache.deltaspike.data.api.QueryInvocationException; import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext; import org.apache.deltaspike.data.impl.meta.MethodType; @@ -54,7 +57,19 @@ public class DelegateQueryBuilder extends QueryBuilder DelegateQueryHandler delegate = selectDelegate(context); if (delegate != null) { - return invoke(delegate, context); + Object result = invoke(delegate, context); + if (result instanceof Collection && StreamUtil.isStreamReturned(context.getMethod())) + { + return StreamUtil.wrap(result); + } + else if (OptionalUtil.isOptionalReturned(context.getMethod())) + { + return OptionalUtil.wrap(result); + } + else + { + return result; + } } } catch (PersistenceException e) http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java index 470cbfe..9a72dca 100644 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java @@ -25,6 +25,7 @@ import javax.persistence.NoResultException; import javax.persistence.Query; import org.apache.deltaspike.core.util.OptionalUtil; +import org.apache.deltaspike.core.util.StreamUtil; import org.apache.deltaspike.data.api.Modifying; import org.apache.deltaspike.data.api.QueryResult; import org.apache.deltaspike.data.api.SingleResultType; @@ -69,6 +70,10 @@ public final class QueryProcessorFactory { return new ListQueryProcessor(); } + if (streams()) + { + return new StreamQueryProcessor(); + } if (isModifying()) { return new ExecuteUpdateQueryProcessor(returns(Void.TYPE)); @@ -89,6 +94,11 @@ public final class QueryProcessorFactory return method.getReturnType().isAssignableFrom(clazz); } + private boolean streams() + { + return StreamUtil.isStreamReturned(method); + } + private static final class ListQueryProcessor implements QueryProcessor { @Override @@ -107,6 +117,15 @@ public final class QueryProcessorFactory } } + private static final class StreamQueryProcessor implements QueryProcessor + { + @Override + public Object executeQuery(Query query, CdiQueryInvocationContext context) + { + return StreamUtil.wrap(query.getResultList()); + } + } + private static final class SingleResultQueryProcessor implements QueryProcessor { @Override http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java index 46e496a..2c731f8 100755 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java @@ -18,6 +18,7 @@ */ package org.apache.deltaspike.data.impl.handler; +import org.apache.deltaspike.core.util.OptionalUtil; import org.apache.deltaspike.data.api.EntityRepository; import org.apache.deltaspike.data.api.Query; import org.apache.deltaspike.data.impl.builder.QueryBuilder; @@ -120,6 +121,19 @@ public class EntityRepositoryHandler<E, PK extends Serializable> } } + public Object findOptional(PK primaryKey) + { + Object found = null; + try + { + found = findBy(primaryKey); + } + catch (Exception e) + { + } + return OptionalUtil.wrap(found); + } + @Override public List<E> findBy(E example, SingularAttribute<E, ?>... attributes) { http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/pom.xml b/deltaspike/modules/data/pom.xml index 7c92b5e..be1cc86 100755 --- a/deltaspike/modules/data/pom.xml +++ b/deltaspike/modules/data/pom.xml @@ -66,6 +66,15 @@ <module>test-ee7</module> </modules> </profile> + <profile> + <id>java8.tests</id> + <activation> + <jdk>1.8</jdk> + </activation> + <modules> + <module>test-java8</module> + </modules> + </profile> </profiles> </project> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/pom.xml b/deltaspike/modules/data/test-java8/pom.xml new file mode 100644 index 0000000..996ca18 --- /dev/null +++ b/deltaspike/modules/data/test-java8/pom.xml @@ -0,0 +1,257 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>data-module-project</artifactId> + <groupId>org.apache.deltaspike.modules</groupId> + <version>1.7.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>deltaspike-data-module-test-java8</artifactId> + <packaging>jar</packaging> + + <name>Apache DeltaSpike Data-Module Tests with Java 8</name> + + <properties> + <deploy.skip>true</deploy.skip> + </properties> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <argLine>-Xms128m -Xmx1024m -XX:MaxPermSize=256m</argLine> + </configuration> + </plugin> + </plugins> + </pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-deploy-plugin</artifactId> + <configuration> + <skip>${deploy.skip}</skip> <!-- we don't deploy our test-modules upstream --> + </configuration> + </plugin> + <plugin> + <groupId>org.bsc.maven</groupId> + <artifactId>maven-processor-plugin</artifactId> + <version>2.0.7</version> + <executions> + <execution> + <id>process-test</id> + <goals> + <goal>process-test</goal> + </goals> + <phase>generate-test-sources</phase> + </execution> + </executions> + <configuration> + <processors> + <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> + </processors> + </configuration> + <dependencies> + <!-- this part of Hibernate is Apache License 2.0, thus O.K. for us. --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <version>1.2.0.Final</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + + <dependencies> + + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jpa_2.1_spec</artifactId> + <version>1.0-alpha-1</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jta_1.2_spec</artifactId> + <version>1.0-alpha-1</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.deltaspike.modules</groupId> + <artifactId>deltaspike-data-module-api</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.deltaspike.modules</groupId> + <artifactId>deltaspike-data-module-impl</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.deltaspike.core</groupId> + <artifactId>deltaspike-core-impl</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.deltaspike.modules</groupId> + <artifactId>deltaspike-partial-bean-module-impl</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.deltaspike.modules</groupId> + <artifactId>deltaspike-jpa-module-impl</artifactId> + <scope>test</scope> + </dependency> + + <!-- Tests --> + + <dependency> + <groupId>org.jboss.shrinkwrap.resolver</groupId> + <artifactId>shrinkwrap-resolver-impl-maven</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.jboss.shrinkwrap.descriptors</groupId> + <artifactId>shrinkwrap-descriptors-impl-javaee</artifactId> + <scope>test</scope> + </dependency> + + </dependencies> + + <profiles> + <profile> + <id>wildfly-build-managed</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-wildfly</directory> + </testResource> + </testResources> + </build> + </profile> + <profile> + <id>wildfly-managed</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-wildfly</directory> + </testResource> + </testResources> + </build> + </profile> + <profile> + <id>wildfly-remote</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-wildfly</directory> + </testResource> + </testResources> + </build> + </profile> + <profile> + <id>tomee-build-managed</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-openejb</directory> + </testResource> + </testResources> + </build> + </profile> + <profile> + <id>tomee7-build-managed</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-openejb</directory> + </testResource> + </testResources> + </build> + </profile> + <profile> + <id>glassfish-build-managed-4</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-glassfish</directory> + </testResource> + </testResources> + </build> + </profile> + <profile> + <id>wls-remote-12c</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-weblogic</directory> + </testResource> + </testResources> + </build> + </profile> + <profile> + <id>wls-managed-12c</id> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/resources-weblogic</directory> + </testResource> + </testResources> + </build> + </profile> + </profiles> +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/entity/Simple.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/entity/Simple.java b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/entity/Simple.java new file mode 100644 index 0000000..b262ce2 --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/entity/Simple.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.data.test.java8.entity; + +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +@NamedQueries({ + @NamedQuery(name = Simple.BY_NAME_LIKE, + query = "select e from Simple e where e.name like ?1"), + @NamedQuery(name = Simple.BY_NAME_ENABLED, + query = "select s from Simple s where s.name = ?1 and s.enabled = ?2 order by s.id asc"), + @NamedQuery(name = Simple.BY_ID, + query = "select s from Simple s where s.id = :id and s.enabled = :enabled") +}) +@Table(name = "SIMPLE_TABLE") +public class Simple +{ + public static final String BY_NAME_LIKE = "simple.byNameLike"; + public static final String BY_NAME_ENABLED = "simple.byNameAndEnabled"; + public static final String BY_ID = "simple.byId"; + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + private Long id; + + private String name; + private String camelCase; + private Boolean enabled = Boolean.TRUE; + private Integer counter = Integer.valueOf(0); + @Temporal(TemporalType.TIMESTAMP) + private Date temporal; + + protected Simple() + { + } + + public Simple(String name) + { + this.name = name; + } + + 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 Boolean getEnabled() + { + return enabled; + } + + public void setEnabled(Boolean enabled) + { + this.enabled = enabled; + } + + public Integer getCounter() + { + return counter; + } + + public void setCounter(Integer counter) + { + this.counter = counter; + } + + public String getCamelCase() + { + return camelCase; + } + + public void setCamelCase(String camelCase) + { + this.camelCase = camelCase; + } + + public Date getTemporal() + { + return temporal; + } + + public void setTemporal(Date temporal) + { + this.temporal = temporal; + } + + @Override + public String toString() + { + return "Simple [id=" + id + ", name=" + name + ", camelCase=" + camelCase + ", enabled=" + enabled + + ", counter=" + counter + ", temporal=" + temporal + "]"; + } + +} + http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository.java b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository.java new file mode 100644 index 0000000..e2e61ef --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.data.test.java8.repo; + +import org.apache.deltaspike.data.api.Repository; +import org.apache.deltaspike.data.test.java8.entity.Simple; + +import java.util.Optional; +import java.util.stream.Stream; + +@Repository(forEntity = Simple.class) +public interface SimpleRepository +{ + Stream<Simple> findByName(String name); + + Optional<Simple> findOptional(Long id); + + Stream<Simple> findAll(); + + Optional<Simple> findBy(Long id); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository2.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository2.java b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository2.java new file mode 100644 index 0000000..3878ce2 --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/repo/SimpleRepository2.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.data.test.java8.repo; + +import org.apache.deltaspike.data.api.Query; +import org.apache.deltaspike.data.api.Repository; +import org.apache.deltaspike.data.api.SingleResultType; +import org.apache.deltaspike.data.test.java8.entity.Simple; + +import java.util.Optional; + +@Repository(forEntity = Simple.class) +public interface SimpleRepository2 +{ + @Query(singleResult = SingleResultType.ANY) + Optional<Simple> findByName(String name); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/test/Java8Test.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/test/Java8Test.java b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/test/Java8Test.java new file mode 100644 index 0000000..20925c1 --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/test/Java8Test.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.data.test.java8.test; + +import org.apache.deltaspike.data.test.java8.entity.Simple; +import org.apache.deltaspike.data.test.java8.repo.SimpleRepository; +import org.apache.deltaspike.data.test.java8.repo.SimpleRepository2; +import org.apache.deltaspike.test.category.WebProfileCategory; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import javax.annotation.Resource; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.transaction.UserTransaction; + +import java.util.Optional; +import java.util.stream.Stream; + +import static java.util.Collections.emptyList; +import static java.util.stream.Collectors.toList; +import static org.apache.deltaspike.data.test.java8.util.TestDeployments.initDeployment; + +@Category(WebProfileCategory.class) +@RunWith(Arquillian.class) +public class Java8Test +{ + @Deployment + public static Archive<?> deployment() + { + return initDeployment() + .addClasses(Java8Test.class, Simple.class, SimpleRepository.class, SimpleRepository2.class); + } + + @Inject + private SimpleRepository simpleRepository; + + @Inject + private SimpleRepository2 simpleRepository2; + + @PersistenceContext + private EntityManager entityManager; + + @Resource + private UserTransaction ut; + + @Before + public void setupTX() throws Exception + { + ut.begin(); + } + + @After + public void rollbackTX() throws Exception + { + ut.rollback(); + } + + @Test + public void shouldFindOptionalSimple() throws Exception + { + Simple s = new Simple("something"); + entityManager.persist(s); + + Optional<Simple> found = simpleRepository.findOptional(s.getId()); + + Assert.assertTrue(found.isPresent()); + } + + @Test + public void shouldNotFindOptionalSimpleForMissing() throws Exception + { + Optional<Simple> found = simpleRepository.findBy(-1L); + + Assert.assertFalse(found.isPresent()); + } + + @Test + public void shouldFindStreamOfSimples() + { + String name = "something"; + Simple s = new Simple(name); + entityManager.persist(s); + + Stream<Simple> found = simpleRepository.findByName(name); + + Assert.assertEquals(1, found.count()); + } + + @Test + public void shouldFindEmptyStream() + { + String name = "something"; + Simple s = new Simple(name); + entityManager.persist(s); + + Stream<Simple> found = simpleRepository.findByName("some other name"); + + Assert.assertEquals(emptyList(), found.collect(toList())); + } + + @Test + public void shouldFindAllAsStream() + { + String name = "something"; + Simple s = new Simple(name); + entityManager.persist(s); + + Stream<Simple> found = simpleRepository.findAll(); + + Assert.assertEquals(1, found.count()); + } + + @Test + public void shouldFindByNameOptional() + { + String name = "jim"; + entityManager.persist(new Simple(name)); + entityManager.persist(new Simple(name)); + + Optional<Simple> found = simpleRepository2.findByName(name); + + Assert.assertTrue(found.isPresent()); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/EntityManagerProducer.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/EntityManagerProducer.java b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/EntityManagerProducer.java new file mode 100644 index 0000000..dc0a2fe --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/EntityManagerProducer.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.data.test.java8.util; + +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public class EntityManagerProducer { + @PersistenceContext + @Produces + private EntityManager entityManager; +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/TestDeployments.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/TestDeployments.java b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/TestDeployments.java new file mode 100644 index 0000000..24003ff --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/java/org/apache/deltaspike/data/test/java8/util/TestDeployments.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.deltaspike.data.test.java8.util; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.maven.Maven; + +import java.io.File; + +public class TestDeployments +{ + /** + * Create a basic deployment with dependencies, beans.xml and persistence descriptor. + * + * @return Basic web archive. + */ + public static WebArchive initDeployment() + { + WebArchive archive = ShrinkWrap + .create(WebArchive.class, "test.war") + .addAsLibraries(getDeltaSpikeDataWithDependencies()) + .addClasses(EntityManagerProducer.class) + .addAsWebInfResource("test-persistence.xml", "classes/META-INF/persistence.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); + + return archive; + } + + private static File[] getDeltaSpikeDataWithDependencies() + { + return Maven.resolver().loadPomFromFile("pom.xml").resolve( + "org.apache.deltaspike.core:deltaspike-core-api", + "org.apache.deltaspike.core:deltaspike-core-impl", + "org.apache.deltaspike.modules:deltaspike-partial-bean-module-api", + "org.apache.deltaspike.modules:deltaspike-partial-bean-module-impl", + "org.apache.deltaspike.modules:deltaspike-jpa-module-api", + "org.apache.deltaspike.modules:deltaspike-jpa-module-impl", + "org.apache.deltaspike.modules:deltaspike-data-module-api", + "org.apache.deltaspike.modules:deltaspike-data-module-impl") + .withTransitivity() + .asFile(); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/resources-glassfish/test-persistence.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/resources-glassfish/test-persistence.xml b/deltaspike/modules/data/test-java8/src/test/resources-glassfish/test-persistence.xml new file mode 100644 index 0000000..64744d4 --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/resources-glassfish/test-persistence.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> + <persistence-unit name="test"> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <jta-data-source>jdbc/__default</jta-data-source> + <properties> + <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> + <property name="eclipselink.logging.level" value="FINE"/> + <property name="eclipselink.logging.parameters" value="true" /> + </properties> + </persistence-unit> +</persistence> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/resources-openejb/test-persistence.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/resources-openejb/test-persistence.xml b/deltaspike/modules/data/test-java8/src/test/resources-openejb/test-persistence.xml new file mode 100644 index 0000000..828e4a3 --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/resources-openejb/test-persistence.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> + <persistence-unit name="test"> + <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> + <jta-data-source>testDatabase</jta-data-source> + <properties> + <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> + <property name="openjpa.Log" value="DefaultLevel=WARN, SQL=TRACE"/> + <property name="openjpa.jdbc.DBDictionary" value="hsql(SimulateLocking=true)"/> + <property name="openejb.jpa.init-entitymanager" value="true" /> + </properties> + </persistence-unit> +</persistence> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/resources-weblogic/test-persistence.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/resources-weblogic/test-persistence.xml b/deltaspike/modules/data/test-java8/src/test/resources-weblogic/test-persistence.xml new file mode 100644 index 0000000..26fd8bf --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/resources-weblogic/test-persistence.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> + <persistence-unit name="test"> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <jta-data-source>TestDS</jta-data-source> + <properties> + <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> + <property name="eclipselink.logging.level" value="FINE"/> + <property name="eclipselink.logging.parameters" value="true" /> + <property name="eclipselink.deploy-on-startup" value="true" /> + </properties> + </persistence-unit> +</persistence> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/deltaspike/modules/data/test-java8/src/test/resources-wildfly/test-persistence.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/test-java8/src/test/resources-wildfly/test-persistence.xml b/deltaspike/modules/data/test-java8/src/test/resources-wildfly/test-persistence.xml new file mode 100644 index 0000000..170def0 --- /dev/null +++ b/deltaspike/modules/data/test-java8/src/test/resources-wildfly/test-persistence.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> + <persistence-unit name="test"> + <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> + <properties> + <property name="hibernate.hbm2ddl.auto" value="create-drop"/> + </properties> + </persistence-unit> +</persistence> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1054cd83/documentation/src/main/asciidoc/data.adoc ---------------------------------------------------------------------- diff --git a/documentation/src/main/asciidoc/data.adoc b/documentation/src/main/asciidoc/data.adoc index 045d6fd..c68be3a 100644 --- a/documentation/src/main/asciidoc/data.adoc +++ b/documentation/src/main/asciidoc/data.adoc @@ -901,6 +901,29 @@ annotations, the `singleResult` attribute can be overridden with the This option will not throw an exception. +=== Java 8 Semantics + +Repositories support returning instances of `java.util.Optional` and `java.util.stream.Stream` for any method. + +[source,java] +----------------------------------------------------------------------- +@Repository(forEntity = Person.class) +public interface PersonRepository +{ + + Optional<Person> findBySsn(String ssn); + + Stream<Person> findByLocation(String location); + +} +----------------------------------------------------------------------- + +Queries returning `Optional<T>` will behave like `SingleResultType.OPTIONAL`, if the data is present, return the single +result, otherwise return `Optional.empty()`. You can override this by using `SingleResultType.ANY` which takes the first +result of the list, or else `empty()`. + +Queries returning `Stream<T>` act as a simple wrapper for `query.getResultList().stream()` to give back the results. + == Transactions If you call any method expression, `@Query`-annotated method or a method
