Skip to content

Commit

Permalink
Fixed: DELTASPIKE-910 Add EntityRepository.getPrimaryKey(E entity)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsoro committed May 25, 2015
1 parent 1238dba commit 4de2fd0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,12 @@ public interface EntityRepository<E, PK extends Serializable> extends Deactivata
*/
Long countLike(E example, SingularAttribute<E, ?>... attributes);

/**
* Return the id of the entity. Returns null if the entity does not yet have an id.
* @param entity entity instance
*
* @return id of the entity
*/
PK getPrimaryKey(E entity);

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.deltaspike.data.impl.property.Property;
import org.apache.deltaspike.data.impl.property.query.NamedPropertyCriteria;
import org.apache.deltaspike.data.impl.property.query.PropertyQueries;
import org.apache.deltaspike.data.impl.util.EntityUtils;
import org.apache.deltaspike.data.spi.DelegateQueryHandler;
import org.apache.deltaspike.data.spi.QueryInvocationContext;

Expand Down Expand Up @@ -165,6 +166,12 @@ public Long countLike(E example, SingularAttribute<E, ?>... attributes)
return executeCountQuery(example, true, attributes);
}

@Override
public PK getPrimaryKey(E entity)
{
return (PK) EntityUtils.primaryKeyValue(entity);
}

@Override
@RequiresTransaction
public void remove(E entity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@

import org.apache.deltaspike.data.test.TransactionalTestCase;
import org.apache.deltaspike.data.test.domain.Simple;
import org.apache.deltaspike.data.test.domain.SimpleStringId;
import org.apache.deltaspike.data.test.domain.SimpleStringIdBuilder;
import org.apache.deltaspike.data.test.domain.Simple_;
import org.apache.deltaspike.data.test.service.ExtendedRepositoryInterface;
import org.apache.deltaspike.data.test.service.SimpleStringIdRepository;
import org.apache.deltaspike.test.category.WebProfileCategory;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.Archive;
Expand All @@ -50,13 +53,16 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
public static Archive<?> deployment()
{
return initDeployment()
.addClasses(ExtendedRepositoryInterface.class)
.addClasses(ExtendedRepositoryInterface.class, SimpleStringIdRepository.class)
.addPackage(Simple.class.getPackage());
}

@Inject
private ExtendedRepositoryInterface repo;

@Inject
private SimpleStringIdRepository repoStringId;

@Produces
@PersistenceContext
private EntityManager entityManager;
Expand Down Expand Up @@ -352,6 +358,31 @@ public void should_remove_and_flush() {
assertNull(lookup);
}

@Test
public void should_return_identify() {
// given
SimpleStringIdBuilder builder = new SimpleStringIdBuilder(entityManager);
SimpleStringId simple = builder.createSimple("SimpleId", "should_return_identify");

// when
String primaryKey = repoStringId.getPrimaryKey(simple);

// then
assertEquals("SimpleId", primaryKey);
}

@Test
public void should_return_null_identify() {
// given
SimpleStringId test = new SimpleStringId(null, "Test");

// when
String primaryKey = repoStringId.getPrimaryKey(test);

// then
assertNull(primaryKey);
}

@Override
protected EntityManager getEntityManager()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.service;

import org.apache.deltaspike.data.api.*;
import org.apache.deltaspike.data.test.domain.Simple;
import org.apache.deltaspike.data.test.domain.SimpleStringId;

import javax.persistence.EntityManager;
import java.util.List;

import static javax.persistence.LockModeType.PESSIMISTIC_WRITE;
import static org.apache.deltaspike.data.api.SingleResultType.ANY;
import static org.apache.deltaspike.data.api.SingleResultType.OPTIONAL;

@Repository
public abstract class SimpleStringIdRepository extends AbstractEntityRepository<SimpleStringId, String>
{

}

0 comments on commit 4de2fd0

Please sign in to comment.